Accessing the Database


Servlet and Forms

As you might expect, you can use the servlet to obtain information entered by a user on a form. This is desirable because it allows a clean separation between the presentation JSP and the business logic typically found in a servlet. Consider our login example HTML from the previous chapter:

 <HTML> <BODY>   Please enter your Name:   <form method='post' action='/hello/LoginServlet'>     <input type='text' name='username' size='35'><BR>     <input type='text' name='password' size='35'><BR>     <input type='submit'>   </form> </BODY> <HTML> 

Here we have two inputs for a username and password. Instead of the form action being a call to a JSP or HTML page, we have the name of a servlet called LoginServlet using a path of /hello. Now let's look at the code for the servlet:

 package example; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hello extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)     throws ServletException, IOException {     response.setContentType("text/html");     PrintWriter out = response.getWriter();     out.println("Username: " +       request.getParameter("username"));     out.println("Password: " +       request.getParameter("password"));   }   public void doPost(HttpServletRequest request,     HttpServletResponse response)     throws ServletException, IOException {     doGet(request, response);   } } 

Note that in the <FORM> tag the method type is POST. This means that when the form's submit button is clicked, the Web browser sends the information provided by the user to the server using a POST request. In our servlet code, the doPost() method that handles the POST request simply makes a call to the doGet() method.

Within the doGet() method, the Response object is set up for an HTML MIME type and the PrintWriter is obtained. Next, the method returns the username and password to the user. In order to get the values entered on the form, the getParameter(String) method associated with the HttpServletRequest object must be used.

The getParameter(String) method takes as a single parameter the name of the <input> tag used in the form that initially called the servlet. If you look back at the <form>, you see that both <input> tags include an attribute called name, which is used as a key value to getParameter(). The response is a String object with the value the user entered into the form. (If that value is numeric, you need to cast the value to the appropriate type.)

Before you can browse to the HTML code, you need to make a change in the web.xml file so it looks like the following:

 <web-app>   <servlet-mapping url-pattern='/hello/LoginServlet'                    servlet-class='example.LoginServlet'/> </web-app> 

With all of the configuration and source files in place, browse to the HTML page and you are presented with a form. After you enter a username and password into the form and click Submit, the LoginServlet servlet launches and returns an HTML response page with the username/password combination displayed for verification.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net