Chapter: Java The Complete Reference - Applying Java - Introducing Servlets

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Reading Servlet Parameters

The ServletRequest interface includes methods that allow you to read the names and values of parameters that are included in a client request.

Reading Servlet Parameters

 

The ServletRequest interface includes methods that allow you to read the names and values of parameters that are included in a client request. We will develop a servlet that illustrates their use. The example contains two files. A web page is defined in PostParameters.html, and a servlet is defined in PostParametersServlet.java.

The HTML source code for PostParameters.html is shown in the following listing. It defines a table that contains two labels and two text fields. One of the labels is Employee and the other is Phone. There is also a submit button. Notice that the action parameter of the form tag specifies a URL. The URL identifies the servlet to process the HTTP POST request.

<html>

 

<body>

 

<center>

 

<form name="Form1" method="post"

 

action="http://localhost:8080/examples/servlets/

 

servlet/PostParametersServlet">

 

<table>

 

<tr>

 

<td><B>Employee</td>

 

<td><input type=textbox name="e" size="25" value=""></td> </tr>

 

<tr>

 

<td><B>Phone</td>

 

<td><input type=textbox name="p" size="25" value=""></td> </tr>

 

</table>

 

<input type=submit value="Submit"> </body>

 

</html>

 

The source code for PostParametersServlet.java is shown in the following listing. The service( ) method is overridden to process client requests. The getParameterNames( ) method returns an enumeration of the parameter names. These are processed in a loop. You can see that the parameter name and value are output to the client. The parameter value is obtained via the getParameter( ) method.

 

import java.io.*; import java.util.*; import javax.servlet.*;

 

public class PostParametersServlet extends GenericServlet {

 

public void service(ServletRequest request, ServletResponse response)

 

throws ServletException, IOException {

 

// Get print writer.

 

PrintWriter pw = response.getWriter();

 

     Get enumeration of parameter names. Enumeration e = request.getParameterNames();

 

     Display parameter names and values. while(e.hasMoreElements()) {

 

String pname = (String)e.nextElement(); pw.print(pname + " = ");

 

String pvalue = request.getParameter(pname); pw.println(pvalue);

}

 

pw.close();

 

}

Compile the servlet. Next, copy it to the appropriate directory, and update the web.xml file, as previously described. Then, perform these steps to test this example:

 

            Start Tomcat (if it is not already running).

 

            Display the web page in a browser.

 

            Enter an employee name and phone number in the text fields.

 

            Submit the web page.

 

After following these steps, the browser will display a response that is dynamically generated by the servlet.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.