i am doing a project which need to use servlet to call a few methods in a class and display those method on the broswer. and i am not sure how to link the methods from the java file to my servlet
package qm.minipas;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class test extends HttpServlet {
Database database;
public void init(ServletConfig config) throws ServletException {
super.init(config);
database = FlatfileDatabase.getInstance();
}
public void destroy() {
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html"
java.io.PrintWriter out = response.getWriter();
out.println("<html>"
out.println("<head>"
out.println("<title>Servlet</title>"
out.println("</head>"
out.println("<body>"
out.println("this is my class wossname"+FlatfileDatabase.getInstance()); //not sure if is right
out.println("this is my method"+FlatfileDatabase.getAll()); //not sure if is right
out.println("</body>"
out.println("</html>"
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
but i dont know how to link the method to my servlet, anybody can give me an example for this ??
The class and method which i need to link to the servlet are showed below:
public class FlatfileDatabase implements Database {
private FlatfileDatabase()
throws DatabaseException,IOException {
File file=new File(DATAFILE);
if(file.exists()) {
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
List temp=new Vector();
String line;
int count=0;
PatientRecord next;
while((line=br.readLine())!=null) {
temp.add(next=PatientRecord.internalise(line));
++count;
}
if(DEBUG)
System.err.println(
"Database read, "+count+" records extracted"
patientRecords=temp;
br.close();
fr.close();
}
else
patientRecords=new Vector();
}
public static Database getInstance() {
return singleton;
}
public Collection getAll() {
return Collections.unmodifiableCollection(patientRecords);
}
public Collection getInpatients() {
Collection selection=new ArrayList();
synchronized(patientRecords) {
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(next.isInpatient())
selection.add(next);
}
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByAdmissionDate(Date dateOfAdmission) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(dateOfAdmission.equals(next.getDateOfAdmission()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByAdmissionDates(Date from,Date to)
throws IllegalArgumentException {
if(to.before(from))
throw new IllegalArgumentException("End date must not be before start date"
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
Date nextAD=next.getDateOfAdmission();
if((nextAD.after(from)||nextAD.equals(from))
&&(nextAD.before(to)||nextAD.equals(to)))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByDischargeDates(Date from,Date to)
throws IllegalArgumentException {
if(to.before(from))
throw new IllegalArgumentException("End date must not be before start date"
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
Date nextAD=next.getDateOfDischarge();
if(nextAD==null)
continue;
if((nextAD.after(from)||nextAD.equals(from))
&&(nextAD.before(to)||nextAD.equals(to)))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByConsultant(String consultant) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(consultant.equalsIgnoreCase(next.getConsultant()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByDischargeDate(Date dateOfDischarge) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(dateOfDischarge.equals(next.getDateOfDischarge()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getBySurname(String surname) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(surname.equalsIgnoreCase(next.getSurname()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByWard(String ward) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(ward.equalsIgnoreCase(next.getWard()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
package qm.minipas;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class test extends HttpServlet {
Database database;
public void init(ServletConfig config) throws ServletException {
super.init(config);
database = FlatfileDatabase.getInstance();
}
public void destroy() {
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html"
java.io.PrintWriter out = response.getWriter();
out.println("<html>"
out.println("<head>"
out.println("<title>Servlet</title>"
out.println("</head>"
out.println("<body>"
out.println("this is my class wossname"+FlatfileDatabase.getInstance()); //not sure if is right
out.println("this is my method"+FlatfileDatabase.getAll()); //not sure if is right
out.println("</body>"
out.println("</html>"
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
but i dont know how to link the method to my servlet, anybody can give me an example for this ??
The class and method which i need to link to the servlet are showed below:
public class FlatfileDatabase implements Database {
private FlatfileDatabase()
throws DatabaseException,IOException {
File file=new File(DATAFILE);
if(file.exists()) {
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
List temp=new Vector();
String line;
int count=0;
PatientRecord next;
while((line=br.readLine())!=null) {
temp.add(next=PatientRecord.internalise(line));
++count;
}
if(DEBUG)
System.err.println(
"Database read, "+count+" records extracted"
patientRecords=temp;
br.close();
fr.close();
}
else
patientRecords=new Vector();
}
public static Database getInstance() {
return singleton;
}
public Collection getAll() {
return Collections.unmodifiableCollection(patientRecords);
}
public Collection getInpatients() {
Collection selection=new ArrayList();
synchronized(patientRecords) {
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(next.isInpatient())
selection.add(next);
}
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByAdmissionDate(Date dateOfAdmission) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(dateOfAdmission.equals(next.getDateOfAdmission()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByAdmissionDates(Date from,Date to)
throws IllegalArgumentException {
if(to.before(from))
throw new IllegalArgumentException("End date must not be before start date"
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
Date nextAD=next.getDateOfAdmission();
if((nextAD.after(from)||nextAD.equals(from))
&&(nextAD.before(to)||nextAD.equals(to)))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByDischargeDates(Date from,Date to)
throws IllegalArgumentException {
if(to.before(from))
throw new IllegalArgumentException("End date must not be before start date"
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
Date nextAD=next.getDateOfDischarge();
if(nextAD==null)
continue;
if((nextAD.after(from)||nextAD.equals(from))
&&(nextAD.before(to)||nextAD.equals(to)))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByConsultant(String consultant) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(consultant.equalsIgnoreCase(next.getConsultant()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByDischargeDate(Date dateOfDischarge) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(dateOfDischarge.equals(next.getDateOfDischarge()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getBySurname(String surname) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(surname.equalsIgnoreCase(next.getSurname()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}
public Collection getByWard(String ward) {
List selection=new ArrayList();
for(Iterator i=patientRecords.iterator(); i.hasNext() {
PatientRecord next=(PatientRecord) i.next();
if(ward.equalsIgnoreCase(next.getWard()))
selection.add(next);
}
return Collections.unmodifiableCollection(selection);
}