import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hello extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType( text/html ); PrintWriter out = res.getWriter(); String name = req.getParameter( name ); out.println( ); out.println(
Hello, + name + ); out.println( ); out.println( Hello, + name); out.println( ); } public String getServletInfo() { return A servlet that knows the name of the person to whom it’s + saying hello ; } } This servlet is nearly identical to the HelloWorldservlet. The most important change is that it now calls req.getParameter( name )to find out the name of the user and that it then prints this name instead of the harshly impersonal (not to mention overly broad) “World”. The getParameter()method gives a servlet access to the parameters in its query string. It returns the parameter’s decoded value or nullif the parameter was not specified. If the parameter was sent but without a value, as in the case of an empty form field, getParameter()returns the empty string. This servlet also adds a getServletInfo()method. A servlet can override this method to return descriptive information about itself, such as its purpose, author, version, and/or copyright. It’s akin to an applet’s getAppletInfo(). The method is used primarily for putting explanatory information into a web server administration tool. You’ll notice we won’t bother to include it in future examples because it is clutter for learning. The servlet’s output looks something like what is shown in Figure 2-5. Figure 2-5. The Hello servlet using form data Handling POST Requests
Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services
This entry was posted
on Saturday, January 13th, 2007 at 2:19 pm and is filed under servlet.
You can follow any responses to this entry through the RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.