Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java Servlets
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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 parametrs 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

 

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.