Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking the class and method to the servlet

Status
Not open for further replies.

ill2000m

Programmer
Mar 25, 2003
2
0
0
GB
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(&quot;<html>&quot;);
out.println(&quot;<head>&quot;);
out.println(&quot;<title>Servlet</title>&quot;);
out.println(&quot;</head>&quot;);
out.println(&quot;<body>&quot;);
out.println(&quot;this is my class wossname&quot;+FlatfileDatabase.getInstance()); //not sure if is right

out.println(&quot;this is my method&quot;+FlatfileDatabase.getAll()); //not sure if is right
out.println(&quot;</body>&quot;);
out.println(&quot;</html>&quot;);

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 &quot;Short description&quot;;
}

}

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(
&quot;Database read, &quot;+count+&quot; records extracted&quot;);

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(&quot;End date must not be before start date&quot;);

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(&quot;End date must not be before start date&quot;);

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);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top