Results 1 to 5 of 5
Thread: simple form & Servlet help
- 12-14-2011, 02:26 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
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> }
- 12-14-2011, 09:43 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: simple form & Servlet help
And the error is?
Or are we supposed to guess...
- 12-28-2011, 08:58 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Re: simple form & Servlet help
What is the error?
- 01-04-2012, 01:42 PM #4
Member
- Join Date
- Aug 2011
- Location
- INDIA
- Posts
- 64
- Rep Power
- 0
Re: simple form & Servlet help
i cant figure out the error in this program
I can and I will.gif)
- 01-27-2012, 07:42 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
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
-
retriving data from DB to servlet form
By mallikanala in forum New To JavaReplies: 2Last Post: 06-20-2011, 11:28 AM -
use Servlet submit the form to receive a form for the Chinese but it shows the garble
By dmt198704 in forum Java ServletReplies: 0Last Post: 10-31-2009, 09:24 AM -
how can i link html+servlet+xml to form servlet
By balachandarr in forum Java ServletReplies: 0Last Post: 04-15-2009, 04:06 PM -
Servlet with a Form/Dropdown
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks