Java Servlet: Passing and Retrieving Parameters

1. PASSING PARAMETERS TO SERVLETS

Like command line arguments, parameters can be passed to servlets. Servlets can access those parameters and customize their tasks. This helps servlets to function differently in different situations without recompiling the source code.

There are two ways in which you can pass parameters to a servlet: using URL and using HTML form.

1.1. Passing Parameters Directly to a Servlet

Parameters can be passed to servlets directly using URL. In this case parameters and their values are attached directly to the URL when you use the URL to call the servlet. The URL and parameters are separated by ‘?’ character. The character ? says “here are the parameters”. Each parameter takes the form name=value. Multiple parameters are separated by the ‘&’ character Consider the following URL.

http://172.16.5.81:8080/net/servlet/check?login=abc&password=xyz

In this example, two parameters are passed login and password whose values are abc and xyz, respectively.

Certain special characters such as space and quote (“) are not allowed directly in the URL. These characters are encoded in the %hh format where hh is the hex code of the character. For example, %20, %22, and %7E represent the space, quote, and tilde (~) characters, respectively.

1.2. Passing Parameters Directly to a Servlet

One of the disadvantages of passing parameters using URLs is that users can view the parameter names with their values passed. So, it is not safe to send sensitive information using this method. Moreover, the number of parameters that we can pass is also limited. HTML forms can be used to pass parameters in a convenient way. The following HTML code can be used to pass the same information as the previous example.

<form method=’post’ action=’http://172.16.5.81:8080/net/servlet/check’>

Login:<input type=’text’ name=’login’><br />

Password:<input type=’password’ name=’password’><br/>

<input type=’submit’ value=’Login’>

</form>

The POST method is used here. In this case, the values of all form fields are embedded in the body of the HTTP request message and hence cannot be viewed directly. However, servlets can retrieve them using the usual way.

The servlet URL used in action attribute is an absolute URL. A relative URL may also be used as follows:

action=’servlet/check’

The URL serviet/check is a URL relative to the current URL (i.e. the URL of the page from which this servlet is invoked). The actual URL may be found by appending this path to the current URL. For example, if http://172.i6.5.8i:8080/net/iogin.htm calls this servlet, the absolute

URL of the servlet is http://172.16.5.81:8080/net/servlet/check

2. RETRIEVING PARAMETERS

The three methods of the ServletRequest interface may be used to retrieve parameters. Their signatures are as follows:

public abstract java.lang.String getParameter(java.lang.String);

public abstract java.lang.String[] getParameterValues(java.lang.String); 

public abstract java.util.Enumeration getParameterNames();

The getParameter() method returns the value of the specified parameter. Make sure that the parameter has exactly one value. If there are multiple parameters with the same name, use the getParameterValues() method, which returns an array of string values of the specified parameter.

The getParameterNames() method returns all the parameters as an enumeration. The values of the individual parameters can be obtained by iterating through the enumeration and using the getParameter() method for each of these parameters. The following example shows how to retrieve values of two parameters, login and password, and return back to the client.

// GetParameterServlet.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

public class GetParameterServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws

IOException, ServletException {

response.setContentType(”text/html”);

PrintWriter out = response.getWriter();

out.println(”<html><head><title>Hello World Servlet</title></head>”);

out.println(”<body>”);

String login = request.getParameter(“login”);

String password = request.getParameter(“password”);

out.println(”<h2> login:” + login + ”<br>password:” + password + ”</h2>”);

out.println(”</body></html>”);

}

}

Compile the servlet, and put the generated GetParameterServlet.class file in the $tomcat_home\ webapps\net\WEB-INF\classes directory. Make the following entry in the web.xml file, and restart the web server.

<servlet>

<servlet-name>GetParameter</servlet-name>

<servlet-class>GetParameterServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>GetParameter</servlet-name>

<url-pattern>/servlet/check</url-pattern>

</servlet-mapping>

Now an html file (call it login.html) is as follows:

<!–login.html–>

<html>

<body>

<form method=’post’ action=’servlet/check’>

Login:<input type=’text’ name=’login’><br />

Password:<input type=’password’ name=’password’><br/>

<input type=’submit’ value=’Login’>

</form>

</body>

</html>

Put this html file in the following directory:

$TOMCAT_HOME\webapps\net

a

We can now call the above servlet using this html file or using the following URL:

http://172.16.5.81:8080/net/servlet/check?login=abc&password=xyz

The form output is shown in Figure 20.8 (i), while a sample response from the servlet is shown in Figure 20.8 (ii).

In practice, servlets hardly return the parameter with their values back to the client. Typically, they consult the database for further processing. In our case, the servlet will possibly verify the login name and password information against the information stored in the database and depending upon the result, either allow or disallow the user to login.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *