Results 1 to 5 of 5
  1. #1
    bhavinragha is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Post simple form & Servlet help

    i have a form
    Name:"name"

    and my servlet should output
    helloname

    i know there is an error in line 39 in my servlet class ploz help

    XML Code:
    <html>
        <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
    		<h1>A simple web application</h1>
    		<form method="POST" action="HelloWorldServlet">
    			<label for="name">Enter your name </label>
    			<input type="text" id="name" name="name"/><br><br>
    			<input type="submit" value="Submit Form"/>
    			<input type="reset" value="Reset Form"/>
    		</form>
    	</body>
    
    </html>
    Java Code:
    import javax.servlet.annotation.WebServlet;
    import java.io.IOException;
    import java.io.PrintWriter;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    /**
     *
     * @author eeua9b
     */
    @WebServlet(name = "HelloWorldServlet", urlPatterns = {"/HelloWorldServlet"})
    public class HelloWorldServlet extends HttpServlet {
      
        /** 
         * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */  
        
        protected void processRequest(HttpServletRequest request, HttpServletResponse response, String name)
                throws ServletException, IOException {
            String POST = request.getParameter("name");
            
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
                // TODO output your page here
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet HelloWorldServlet</title>");  
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Servlet HelloWorldServlet at " + request.getContextPath () + "</h1>");
                //out.println("<h2>Hello " + name + "</h2>" );
                out.println("</body>");
                out.println("</html>");
                 
            } finally {            
                out.close();
            }
        }
    
        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /** 
         * Handles the HTTP <code>GET</code> method.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        /** 
         * Handles the HTTP <code>POST</code> method.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        /** 
         * Returns a short description of the servlet.
         * @return a String containing servlet description
         */
        @Override
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>
    }

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

    Default Re: simple form & Servlet help

    And the error is?
    Or are we supposed to guess...

  3. #3
    pala is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: simple form & Servlet help

    What is the error?

  4. #4
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default Re: simple form & Servlet help

    i cant figure out the error in this program
    I can and I will

  5. #5
    HowardHyde is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: simple form & Servlet help

    If you UN-comment line 39, you will get a compiler error on an undeclared variable 'name'. If you replace that line with
    out.println("<h2>Hello " + POST + "</h2>" );
    ...then it should work, because of line 27:
    String POST = request.getParameter("name");

    I recommend changing line 27 to
    String name = request.getParameter("name");
    and then line 39 will work fine as is.

    By convention, variable names are given in camelCase with leading lower-case letters; ALL_UPPER_CASE would normally be used for a final/constant value.

    Howard Hyde, author, 'Java Web Database Application Development' (JavaWebDB.com).

Similar Threads

  1. retriving data from DB to servlet form
    By mallikanala in forum New To Java
    Replies: 2
    Last Post: 06-20-2011, 11:28 AM
  2. Replies: 0
    Last Post: 10-31-2009, 09:24 AM
  3. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 04:06 PM
  4. Servlet with a Form/Dropdown
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:24 AM

Tags for this Thread

Posting Permissions

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