1 Replies - 3443 Views - Last Post: 03 March 2011 - 12:37 AM

#1 SKirk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-March 11

Directing one servlet to another

Posted 01 March 2011 - 11:42 AM

I need to use three servlets so that I can search through a file of contacts. If none are found, you can search again. If one is found, you are directed right to a page that will have all of the contact information. If more than one is found, you first get a list of all results without all the details, and each result is linked to it's own detail page with everything listed. I know that my search form displays alright, and I know the basic code of my search is right, but I'm not sure if it's functioning correctly right now. Mostly, however, I don't know how to tell it to go to another servlet. If only one result is found, I need the DisplayResults servlet to jump to DisplayDetail servlet. I've tried to figure out something using requestDispatcher, but I couldn't get it to work-- we don't get too much detail in how to code things in class, so I'm stuck trying to teach myself. Just the basic syntax of what needs to happen would be great. And am I right in saying that if I start with one servlet's doGet method, it'll need to go to the next servlet's doPost? Of course, if I'm screwing something else up and just don't know it, please let me know. Thanks!

Search
package cosc365;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.HTMLFilter;

/**
 * This is a servlet that presents a webpage with search
 *         options for someone to search through a contact file.
 */

public class SearchServlet extends HttpServlet {
	Scanner file;
	Contact contact = new Contact();
	List<Contact> results = new ArrayList<Contact>();
	List<Contact> contacts = new ArrayList<Contact>();
	int resultsArrayCount = 0;
	Contact[] resultsArray = new Contact[30];
	int i;

	public void init(ServletConfig config) {
	
		// read file
		try {
			String fileName = config.getInitParameter("contacts");

			file = new Scanner(config.getServletContext().getResourceAsStream(
					fileName));
		} catch (Exception e) {
			throw new RuntimeException(e.toString());
		}

		try {
			file.useDelimiter("[,\\r\\n]+");
			{
				while (file.hasNext()) {
					String contactFirstName = file.next();
					String contactLastName = file.next();
					String contactCompany = file.next();
					String contactEMail = file.next();
					String contactPhone = file.next();

					contacts.add(new Contact(contactFirstName, contactLastName,
							contactCompany, contactEMail, contactPhone));

				}
			}
		}

		catch (NoSuchElementException e) {
			throw new RuntimeException(e.toString());
		}

		catch (IllegalStateException e) {
			throw new RuntimeException(e.toString());

		}

		file.close();
		config.getServletContext().setAttribute("Contacts", contacts);

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// html page with 5 search options
		out.println("<html>");
		out.println("<head>");
		out.println("<title> Search Servlet Attempt </title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<p> Search through my random list of contacts, cross your fingers, and see if this actually works.</p>");
		out.println("<form action=\"DisplayResults\" method=\"post\">");
		out.println("<table>");
		out.println("<tr>");
		out.println("<td>First Name:</td>");
		out.println("<td><input type=\"text\"  name=\"firstName\" size=\"20\"  /></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>Last Name:</td>");
		out.println("<td><input type=\"text\"  name=\"lastName\" size=\"20\"  /></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>Company:</td>");
		out.println("<td><input type=\"text\"  name=\"company\" size=\"20\"  /></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>E-Mail:</td>");
		out.println("<td><input type=\"text\"  name=\"eMail\" size=\"20\"  /></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>Phone Number:</td>");
		out.println("<td><input type=\"text\"  name=\"phone\" size=\"20\"  /></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td><input type=\"submit\" value=\"Search\" /><br/></td>");
		out.println("</tr>");
		out.println("<table>");
		out.println("</form>");
		out.println("</body>");
		out.println("</html>");

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
	
	}
}



DisplayResults

package cosc365;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import util.HTMLFilter;

public class DisplayResultsServlet extends HttpServlet {
	List<Contact> contacts;
	List<Contact> results = new ArrayList<Contact>();

	public void init(ServletConfig config) {
		contacts = (List<Contact>) config.getServletContext().getAttribute(
				"Contacts");
		config.getServletContext().setAttribute("Results", contacts);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// String email = request.getParameter("eMail");

		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		String company = request.getParameter("company");
		String eMail = request.getParameter("eMail");
		String phone = request.getParameter("phone");

		for (Contact contact : contacts) {
			// search through arrays
			if ((firstName == null || firstName.toLowerCase().equals(
					contact.getFirstName().toLowerCase()))
					&& (lastName == null || lastName.toLowerCase().equals(
							contact.getLastName().toLowerCase()))
					&& (company == null || company.toLowerCase().equals(
							contact.getCompany().toLowerCase()))
					&& (eMail == null || eMail.toLowerCase().equals(
							contact.getEMail().toLowerCase()))
					&& (phone == null || phone.toLowerCase().equals(
							contact.getPhone().toLowerCase()))) {

				results.add(contact);

				if (results.size() == 0) {

					// currently prints out a new search form, but it needs to
					// direct back to the Search servlet,
					// and add a line about no results.

					response.setContentType("text/html");
					PrintWriter out = response.getWriter();

					out.println("<html>");
					out.println("<head>");
					out.println("<title> Search Servlet Attempt </title>");
					out.println("</head>");
					out.println("<body>");
					out.println("<p> Sorry, no matches. Try again.</p>");
					out.println("<form action=\"/Contacts/DisplayDetail\" method=\"post\">");
					out.println("<table>");
					out.println("<tr>");
					out.println("<td>First Name:</td>");
					out.println("<td><input type=\"text\"  name=\"firstName\" size=\"20\"  /></td>");
					out.println("</tr>");
					out.println("<tr>");
					out.println("<td><input type=\"text\"  name=\"lastName\" size=\"20\"  /></td>");
					out.println("</tr>");
					out.println("<tr>");
					out.println("<td><input type=\"text\"  name=\"company\" size=\"20\"  /></td>");
					out.println("</tr>");
					out.println("<tr>");
					out.println("<td><input type=\"text\"  name=\"eMail\" size=\"20\"  /></td>");
					out.println("</tr>");
					out.println("<tr>");
					out.println("<td><input type=\"text\"  name=\"phone\" size=\"20\"  /></td>");
					out.println("</tr>");
					out.println("<tr>");
					out.println("<td><input type=\"submit\" value=\"Search\" /><br/></td>");
					out.println("</tr>");
					out.println("<table>");
					out.println("</form>");
					out.println("</body>");
					out.println("</html>");
				}

				if (results.size() == 1) {
					// needs to go directly to DisplayDetail servlet
				}

				if (results.size() > 1) {
					// needs to print out condensed info for all, with each
					// result linked to the Display Detail servlet

					response.setContentType("text/html");
					PrintWriter out = response.getWriter();
					out.println("<html>");
					out.println("<head>");
					out.println("<title> Display Contact Details </title>");
					out.println("</head>");
					out.println("<body>");
					out.println("<table>");

					for (Contact results : contacts) {

						out.println("<tr>");
						out.println("<td>First Name</td> <td>Last Name</td>  <td>E-Mail</td> ");
						out.println("<td>contacts.getFirstName()</td>");
						out.println("<td>contacts.getLastName()</td>");
						out.println("<td>contacts.getEMail()</td>");
						out.println("</tr>");
						out.println("<br />");
					}
					out.println("</table>");
					out.println("</body>");
					out.println("</html>");
				}
			}
		}
	}
}



DisplayDetail
package cosc365;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import util.HTMLFilter;

public class DisplayDetailServlet extends HttpServlet {
	List<Contact> contacts;
	List<Contact> results;

	public void init(ServletConfig config) {
		contacts = (List<Contact>) config.getServletContext().getAttribute(
				"Contacts");
		results = (List<Contact>) config.getServletContext().getAttribute(
				"Results");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		if (results.size() == 1) {
			out.println("<html>");
			out.println("<head>");
			out.println("<title> Display Contact Details </title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<table>");
			out.println("<tr>");
			out.println("<td>First Name</td> <td>Last Name</td> <td>Company</td> <td>E-Mail</td> <td>Phone Number</td>");
			out.println("</tr>");
			out.println("<tr>");
			out.println("<td>contacts.getFirstName()</td>");
			out.println("<td>contacts.getLastName()</td>");
			out.println("<td>contacts.getCompany()</td>");
			out.println("<td>contacts.getEMail()</td>");
			out.println("td>contacts.getPhone()</td>");
			out.println("</tr>");
			out.println("</table>");
			out.println("<a href=\"Contacts/Search\">New Search</a>");
			out.println("</body>");
			out.println("</html>");
		}

		else {
			out.println("<html>");
			out.println("<head>");
			out.println("<title> Display Contact Details </title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<table>");
			out.println("<tr>");
			out.println("<td>First Name</td> <td>Last Name</td> <td>Company</td> <td>E-Mail</td> <td>Phone Number</td>");
			out.println("</tr>");

			for (Contact results : contacts) {

				out.println("<tr>");
				out.println("<td>contacts.getFirstName()</td>");
				out.println("<td>contacts.getLastName()</td>");
				out.println("<td>contacts.getCompany()</td>");
				out.println("<td>contacts.getEMail()</td>");
				out.println("td>contacts.getPhone()</td>");
				out.println("</tr>");
				out.println("<br />");
			}
			out.println("</table>");
			out.println("<a href=\"Contacts/Search\">New Search</a>");
			out.println("<a href=\"Contacts/DisplayResults\">Back to Results Page</a>");
			out.println("</body>");
			out.println("</html>");
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Directing one servlet to another

#2 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: Directing one servlet to another

Posted 03 March 2011 - 12:37 AM

Transferring from one servlet to another can be done with response.sendRedirect or the forward method of RequestDispatcher (Just google this and you'll get a couple of tutorials and examples on this I'm sure.)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1