Amit Singh
Amit Singh

Reputation: 1

servlet programming

i m using these servlet codes. in processRequest method,in form those variables ans,id...i m not getting in dopost method.i m getting prob in double quote....

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

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet adminforum</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<FORM METHOD=POST>");
            out.println("enter the ID no.");
            out.println("<INPUT TYPE=TEXT name=id>");
            out.println("<br>");
            out.println("enter the answer.");
            out.println("<INPUT TYPE=TEXT name=ans>");
            out.println("<br>");
            out.println("<INPUT TYPE=SUBMIT VALUE=submit>");
            out.println("</FORM>");
            out.println("</body>");
            out.println("</html>");

        } finally { 
            out.close();
        }
    } 

Upvotes: 0

Views: 2441

Answers (2)

BalusC
BalusC

Reputation: 1109532

You need to specify the servlet URL in the form action. Assuming that the desired servlet with the doPost() method is mapped in web.xml on an URL pattern of /servleturl and that the current request URL is in the same path, then you need to change the form action as follows:

<form action="servleturl" method="post">

Unrelated to the concrete problem, although a lot of 90's-dated servlet tutorials teaches servlets that way, this approach is in real world considered poor practice. HTML code should go in JSP files and Java code should go in Java (Servlet) classes. I'd suggest to get yourself through our wiki pages regarding the subjects:

Upvotes: 1

no.good.at.coding
no.good.at.coding

Reputation: 20371

Points of note:

  • Unless you're using Servlets for the very first time and are trying to understand the technology, you really shouldn't be using a servlet to output HTML - a JSP is better suited for this.
  • While HTML is case-insensitive, XML (and hence XHTML) is not. Which means that <form> and <FORM> are different elements. It is now convention to use lower-case for all elements. So you should NOT be using <INPUT>; use <input> instead. The same goes for attributes (value not VALUE).
  • Note that you can use either double quotes " or single quotes ' for attributes in X(HT)ML.

And if I understand you correctly, you're saying that you're unable to read the fields from the form when you submit it and you suspect this is because you're unable to add the double quotes to the string literal you're writing out here. That's a simple matter of escaping the double quotes within the string literal:

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet adminforum</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<FORM METHOD=\"POST>\"");
        out.println("enter the ID no.");
        out.println("<INPUT TYPE=\"TEXT\" name=\"id\">");
        out.println("<br>");
        out.println("enter the answer.");
        out.println("<INPUT TYPE=\"TEXT\" name=\"ans>\"");
        out.println("<br>");
        out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"submit\">");
        out.println("</FORM>");
        out.println("</body>");
        out.println("</html>");

Or if you'd rather use single quotes (which also makes it easier to write the Java code):

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet adminforum</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<FORM METHOD='POST>'");
        out.println("enter the ID no.");
        out.println("<INPUT TYPE='TEXT' name='id'>");
        out.println("<br>");
        out.println("enter the answer.");
        out.println("<INPUT TYPE='TEXT' name='ans>'");
        out.println("<br>");
        out.println("<INPUT TYPE='SUBMIT' VALUE='submit'>");
        out.println("</FORM>");
        out.println("</body>");
        out.println("</html>");

Upvotes: 2

Related Questions