开发者

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();
        }
    } 


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>");


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:

  • JSP info page
  • Servlets info page
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜