Results 1 to 5 of 5
  1. #1
    Mr.A is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Question Form processing in Java Servlets

    Hello all,

    I am new to servlets. Being a core front end developer. I was introduced to Servlets and now they have gone further too far.
    Here's the question :
    1)I should collect information from a form and then display it to the user(The fields he entered). I should use doGet() to display the form and when the submit button is clicked I should use doPost() to display the information. And also form validation is done on the server.
    2)The next step is to save the information that user entered in a permanent data store. I should also provide functionality to read the data store, present them to the user and let the user sort them by fields(such as by name, place date, etc...). It can be implemented in two servlets or one and also I can save the data either in plain file, XML file or whatever I choose.

    I have the first part here which is executed correctly and I am unable to do the 2nd part how much ever I try. Here's the 1st part:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package x;

    /**
    *
    * @author A
    */
    // Import Servlet Libraries
    import javax.servlet.*;
    import javax.servlet.http.*;

    // Import Java Libraries
    import java.io.*;
    import java.util.*;
    import java.lang.*;

    // formHandler class
    // Generic form handler -- feed back all the parameters and values
    // that a client inputs from an HTML form.
    // Note: the "name" of the submit button in the form must be "submit"
    // (ignore case) or the servlet will print out "submit" parameter.


    // CONSTRUCTOR: no constructor specified (default)
    //
    // **************** Methods description *******************************
    // void doPost () --> Main method for gathering data and sending back
    // void doGet () --> Not used.
    //************************************************** *********************
    public class H extends HttpServlet
    {
    @Override
    public void doGet (HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {

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

    out.println ("<HTML>");
    out.println ("<HEAD>");
    out.println("<title> Form</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<form name=\"resto\" method=\"POST\" action=\"");
    out.println("http://apps-swe.smu.lse.edu:8080/swe/servlet/x.H\">");
    out.println("<fieldset><legend>Restaurant feedback form</legend>");
    out.println("<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" width=\"60%\" >");
    out.println("<tr>");
    out.println("<td>");
    out.println("Name:</td>");
    out.println("<td>");
    out.println("<input type=text size=20 name=Name Autofocus /></td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Location:</td>");
    out.println("<td>");
    out.println("<input type=text size=20 name=Location />");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Type of food:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=text size=20 name=Type />");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Visit date:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=date name=Visited />");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Favorite dish:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=text size=30 name=Favorite />");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Budget:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=radio name=Budget value=cheap />");
    out.println("Cheap");
    out.println("<input type=radio name=Budget value=average />");
    out.println("Average");
    out.println("<input type=radio name=Budget value=expensive />");
    out.println("Expensive");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Distance from home:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=number name=Distance/>");
    out.println("Miles");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Review:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=radio name=Ratings value=1 />");
    out.println("1");
    out.println("<input type=radio name=Ratings value=2 />");
    out.println("2");
    out.println("<input type=radio name=Ratings value=3 />");
    out.println("3");
    out.println("<input type=radio name=Ratings value=4 />");
    out.println("4");
    out.println("<input type=radio name=Ratings value=5 />");
    out.println("5");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Overall Comments");
    out.println("</td>");
    out.println("<td>");
    out.println("<textarea name=Comments id=\"comm\" >");
    out.println("</textarea>");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("<input type=submit name=submit value=submit />");
    out.println("<input type=reset name=reset value=reset />");
    out.println("</td>");
    out.println("</tr>");
    out.println("</table>");
    out.println("</fieldset>");
    out.println("</form>");
    out.println("<br><br><br><br><br><br><br><br><br>< br><br><br><br><br><br><br><br><br><br>");
    out.println("<font size=-2><em>&copy Swe , A, S12345</em></font>");
    out.println("</body>");
    out.println("</html>");

    }//end doGet()
    /** ************************************************** ********
    * doPost()
    * gathering data and sending back to brower
    ************************************************** ********** */
    @Override
    public void doPost (HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {
    // first, set the "content type" header of the response
    //Get the response's PrintWriter to return text to the client.

    String errMsg = "";
    String name = req.getParameter("Name");
    String location = req.getParameter("Location");
    String type = req.getParameter("Type");
    String visited = req.getParameter("Visited");
    String favorite = req.getParameter("Favorite");
    String budget = req.getParameter("Budget");
    String distance = req.getParameter("Distance");
    String rating = req.getParameter("Ratings");
    String comment = req.getParameter("Comments");

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    PrintWriter toClient = res.getWriter ();
    String para;
    Enumeration paraNames = req.getParameterNames ();
    // Now let's do some validation
    out.println("<html><head>");
    out.println("<title> Form Servlet </title>");
    out.println("</head><body topmargin=0 leftmargin=0>");
    if(name.length() == 0)
    errMsg += "<br><p style=\"color:red\">Name has not been supplied!</P>";

    if(location.length() == 0)
    errMsg += "<p style=\"color:red\">Please enter the location!<br>";
    if(budget == null)
    errMsg += "<p style=\"color:red\">Please select the budget!<br>";
    if(rating == null)
    errMsg += "<p style=\"color:red\">Please rate<br>";
    if(type.length() == 0)
    errMsg += "<p style=\"color:red\">Please enter the type of food<br>";
    if(favorite.length() == 0)
    errMsg += "<p style=\"color:red\">Please enter the item which you liked!<br>";

    // Process the data.
    // end validation
    if(errMsg.length() != 0) {
    out.println("<HTML><head><title>Error Form</title></head>");
    out.println("<body>");
    out.println("<p><b>Sorry! there were errors. Error message is<b> </p>" + errMsg);
    out.println("<h2>Restaurant feedback form</h2>");
    out.println("<form name=\"resto\" method=\"POST\" action=\"");
    out.println("http://apps-swe.smu.lse.edu:8080/swe/servlet/x.H\">");
    out.println("<fieldset><legend>form</legend>");
    out.println("<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" width=\"60%\" >");
    out.println("<tr>");
    out.println("<td>");
    out.println("Name: </td>");
    out.println("<td>");
    out.println("<input type=text size=20 name=Name value="+name+" ></td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Location:</td>");
    out.println("<td>");
    out.println("<input type=text size=20 name=Location value="+location+" >");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Type of food:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=text size=20 name=Type value="+type+" >");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Visit date:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=date name=Visited >");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Favorite dish:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=text size=30 name=Favorite value="+favorite+" >");
    out.println("</td>");
    out.println("</tr>");
    if("cheap".equals(budget)){
    out.println("<tr>");
    out.println("<td>");
    out.println("Budget:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=radio name=Budget value=cheap checked >");
    out.println("Cheap");
    out.println("<input type=radio name=Budget value=average >");
    out.println("Average");
    out.println("<input type=radio name=Budget value=expensive >");
    out.println("Expensive");
    out.println("</td>");
    out.println("</tr>");
    }
    else if("expensive".equals(budget))
    {
    out.println("<tr>");
    out.println("<td>");
    out.println("Budget:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=radio name=Budget value=cheap />");
    out.println("Cheap");
    out.println("<input type=radio name=Budget value=average />");
    out.println("Average");
    out.println("<input type=radio name=Budget value=expensive checked />");
    out.println("Expensive");
    out.println("</td>");
    out.println("</tr>");
    }
    else if("average".equals(budget)){
    out.println("<tr>");
    out.println("<td>");
    out.println("Budget:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=radio name=Budget value=cheap />");
    out.println("Cheap");
    out.println("<input type=radio name=Budget value=average checked />");
    out.println("Average");
    out.println("<input type=radio name=Budget value=expensive />");
    out.println("Expensive");
    out.println("</td>");
    out.println("</tr>");
    }
    else {
    out.println("<tr>");
    out.println("<td>");
    out.println("Budget:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=radio name=Budget value=cheap />");
    out.println("Cheap");
    out.println("<input type=radio name=Budget value=average />");
    out.println("Average");
    out.println("<input type=radio name=Budget value=expensive />");
    out.println("Expensive");
    out.println("</td>");
    out.println("</tr>");
    }
    out.println("<tr>");
    out.println("<td>");
    out.println("Distance from home:");
    out.println("</td>");
    out.println("<td>");
    out.println("<input type=number name=Distance value="+distance+" >");
    out.println("Miles");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Review:");
    out.println("</td>");
    if("1".equals(rating))
    {
    out.println("<td>");
    out.println("<input type=radio name=Ratings value=1 checked/>");
    out.println("1");
    out.println("<input type=radio name=Ratings value=2 />");
    out.println("2");
    out.println("<input type=radio name=Ratings value=3 />");
    out.println("3");
    out.println("<input type=radio name=Ratings value=4 />");
    out.println("4");
    out.println("<input type=radio name=Ratings value=5 />");
    out.println("5");
    out.println("</td>");
    }
    else if("2".equals(rating))
    {
    out.println("<td>");
    out.println("<input type=radio name=Ratings value=1 />");
    out.println("1");
    out.println("<input type=radio name=Ratings value=2 checked/>");
    out.println("2");
    out.println("<input type=radio name=Ratings value=3 />");
    out.println("3");
    out.println("<input type=radio name=Ratings value=4 />");
    out.println("4");
    out.println("<input type=radio name=Ratings value=5 />");
    out.println("5");
    out.println("</td>");
    }
    else if("3".equals(rating))
    {
    out.println("<td>");
    out.println("<input type=radio name=Ratings value=1 />");
    out.println("1");
    out.println("<input type=radio name=Ratings value=2 />");
    out.println("2");
    out.println("<input type=radio name=Ratings value=3 checked/>");
    out.println("3");
    out.println("<input type=radio name=Ratings value=4 />");
    out.println("4");
    out.println("<input type=radio name=Ratings value=5 />");
    out.println("5");
    out.println("</td>");
    }
    else if("4".equals(rating))
    {
    out.println("<td>");
    out.println("<input type=radio name=Ratings value=1 />");
    out.println("1");
    out.println("<input type=radio name=Ratings value=2 />");
    out.println("2");
    out.println("<input type=radio name=Ratings value=3 />");
    out.println("3");
    out.println("<input type=radio name=Ratings value=4 checked />");
    out.println("4");
    out.println("<input type=radio name=Ratings value=5 />");
    out.println("5");
    out.println("</td>");
    }
    else if("5".equals(rating))
    {
    out.println("<td>");
    out.println("<input type=radio name=Ratings value=1 />");
    out.println("1");
    out.println("<input type=radio name=Ratings value=2 />");
    out.println("2");
    out.println("<input type=radio name=Ratings value=3 />");
    out.println("3");
    out.println("<input type=radio name=Ratings value=4 />");
    out.println("4");
    out.println("<input type=radio name=Ratings value=5 checked/>");
    out.println("5");
    out.println("</td>");
    }
    else{

    out.println("<td>");
    out.println("<input type=radio name=Ratings value=1 />");
    out.println("1");
    out.println("<input type=radio name=Ratings value=2 />");
    out.println("2");
    out.println("<input type=radio name=Ratings value=3 />");
    out.println("3");
    out.println("<input type=radio name=Ratings value=4 />");
    out.println("4");
    out.println("<input type=radio name=Ratings value=5 />");
    out.println("5");
    out.println("</td>");
    }

    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("Overall Comments");
    out.println("</td>");
    out.println("<td>");
    out.println("<textarea name=Comments id=\"comm\" >");
    out.println("</textarea>");
    out.println("</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>");
    out.println("<input type=submit name=submit value=submit />");
    out.println("<input type=reset name=reset value=reset />");
    out.println("</td>");
    out.println("</tr>");
    out.println("</table>");
    out.println("</fieldset>");
    out.println("</form>");
    out.println("<br><br><br><br><br><br><br><br><br>< br><br><br>");
    out.println("<font size=-2><em>&copy Swe , A, S12345</em></font>");
    out.println("</body>");
    out.println("</html>");

    } else{
    toClient.println ("<CENTER><H2>Sample Form Handler</H2></CENTER>");
    toClient.println ("<P>");
    toClient.println ("</P>");
    toClient.println ("");
    toClient.println ("<P>");
    toClient.println ("<TABLE cellSpacing=1 cellPadding=1 width=\"75%\" border=1 bgColor=blue>");
    toClient.println ("");
    toClient.println (" <TR BGColor=\"red\">");
    toClient.println (" <TH Align=\"center\"><STRONG>Parameter</STRONG></TD>");
    toClient.println (" <TH Align=\"center\"><STRONG>Value</STRONG></TD>");
    toClient.println (" </TR>");

    while (paraNames.hasMoreElements ())
    { // For each parameter name.
    para = (String) paraNames.nextElement ();
    if (!para.equalsIgnoreCase ("submit"))
    {
    toClient.println (" <TR>");
    toClient.println (" <TD style=\"WIDTH: 20%\" width=\"20%\"><STRONG>"
    + para + "</STRONG></TD>");

    String[] values = req.getParameterValues (para);

    if (values != null && !values[0].equals(""))
    toClient.println (" <TD>" + values[0] + "</TD></TR>");
    else
    toClient.println (" <TD>&nbsp;</TD></TR>");

    for (int i = 1; i < values.length; i++)
    {
    if (!values[i].equals(""))
    {
    toClient.println (" <TR>");
    toClient.println (" <TD style=\"WIDTH: 20%\" width=\"20%\">&nbsp;</TD>");
    toClient.println (" <TD>" + values[i] + "</TD></TR>");
    }
    }
    }
    }
    toClient.println ("</TABLE>");
    }
    toClient.println ("");
    toClient.println("<br><br><br><br><br><br><br><br> <br><br><br><br><br><br><br><br><br><br><br>");
    toClient.println("<font size=-2><em>&copy Swe 642, Akshay Gummadi, G00808625</em></font>");
    toClient.println ("</BODY>");
    toClient.println ("</HTML>");

    toClient.println ("");

    // Close the writer; the response is done.
    toClient.close();
    } //end of doPost()
    /**
    *
    * @param req
    * @param res
    * @throws ServletException
    * @throws IOException
    */

    }//end formhandler


    PLEASE HELP

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Form processing in Java Servlets

    A gazillion lines of unformatted code
    No specific question

    Good luck getting help.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Form processing in Java Servlets

    For part 2 what have you tried?
    No point posting loads of irrelevant code.
    Also, when posting code please wrap it in [code] tags [/code]. Many people won't bother to read unformatted code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Mr.A is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Form processing in Java Servlets

    I am stuck not understanding what to do! How do I save the information to a file?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Form processing in Java Servlets

    Decide on a format and then use some form of writer to output it?

    You could create a class that represents that data and create an object of that class, populating it with the data from the form, then simply serialize that out using an ObjectOutputStream, as another possibility.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Servlets class not teaching servlets
    By tsky in forum Java Servlet
    Replies: 5
    Last Post: 04-26-2011, 10:47 PM
  2. Replies: 1
    Last Post: 05-27-2010, 09:48 PM
  3. form and results on same page using servlets
    By perplexingtrax in forum New To Java
    Replies: 3
    Last Post: 04-02-2009, 06:04 PM
  4. Java Servlets
    By kondalrao in forum Java Servlet
    Replies: 0
    Last Post: 04-14-2008, 11:12 AM
  5. 1 form 2 servlets
    By sandor in forum Java Servlet
    Replies: 2
    Last Post: 01-22-2008, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •