Check if parameter exists in servlet request

In this example we will see how to check is parameter exists in servlet request.

Check if parameter exists in servlet request

Check if parameter exists in servlet request

     

In this example we will see how to check is parameter exists in servlet request. In this example given below we have used method getParamaterNames() of the ServletRequest interface  to find all the parameters name in the clients request.

getParametersNames() : This method returns an Enumeration of String objects . We can use two methods to get object names from this Enumeration.

   hasMoreElements() tests if this enumeration contains more elements.

   nextElement() returns the next element of enumeration.

We have used a jsp page that is used to send a request to a servlet that execute the request and tests that request having parameters or not. Before run this code create a new directory named "user" in the tomcat-6.0.16/webapps and paste WEB-INF directory in same directory.

show_data.jsp

<%@page language="java" session="true" contentType="text/html;
charset=ISO-8859-1" %>
<form name="frm" method="get" action="../user/GetParameter"> <b>
<font color="blue">
You can select parameters here to send request to servlet.
</font>
</b>
<table>
<tr><td> Parameter1</td>
<td><input type="checkbox" name="Parameter1" value="1"></td></tr> <tr><td>Parameter2</td><td><input type="checkbox" name="Parameter2" value="2"></td></tr>
 <tr><td>Parameter3</td><td><input type="checkbox" name="Parameter3" value="3"></td></tr>
<tr><td>Parameter4</td><td><input type="checkbox" name="Parameter4" value="4"></td></tr>
<tr><td></td> <td><input type="submit" name="submit" value="submit"/> </td></tr>
 </table>
</form>

Save this code as a .jsp file named "show_data.jsp" in the directory Tomcat-6.0.16/webapps/user/ and you can run this jsp page with following url in address bar of the browser "http://localhost:8080/user/show_data.jsp"

GetParameter.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetParameter extends HttpServlet {
  public void doGet(HttpServletRequest request,
  HttpServletResponse response)
    throws IOException, ServletException{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();    
    Enumeration parameter = request.getParameterNames();
    int count = 0;
	// check that parameter is exist or not
    while(parameter.hasMoreElements()){
		parameter.nextElement();
		count++;
	}
	if(count==1){
	  out.println("<font color=\"red\" size=\"+1\">
          No parameter in servlet request.<font><br>");	
	}
	else {    
         out.println("<b><font color='blue'>You have send "+
        (count-1) +" parameters in servlet are.</font></b>");
	}
  }
}

Compile this java code and save .class file in directory C:\apache-tomcat-6.0.16\webapps\user\WEB-INF\classes.

web.xml

<servlet>
    <servlet-name>GetParameter</servlet-name>
    <servlet-class>GetParameter</servlet-class>
</servlet> 
<servlet-mapping>
    <servlet-name>GetParameter</servlet-name>
    <url-pattern>/GetParameter</url-pattern>
</servlet-mapping>

This is web .xml file use to map servlet. When run jsp page in the browser.....

User select checkboxes to select parameters send to servlet as a request ....

After select text boxes when click on submit button.....

Download Source Code