Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Request Object In JSP 
 

This section illustrates more about the JSP implicit object called Request object. This object retrieves values whatever client passes to the server by an HTTP request.

 

Request Object In JSP

                         

This section illustrates more about the JSP implicit object called Request object. This object retrieves values whatever client passes to the server by an HTTP request. Let if you submit the html form with some data to be send to the server, is received by the HTTP request object of the JSP. To access the HTML form data we use the request object and it's several methods like getParameter().

All the things like headers, cookies or parameters are sent from the client browser to the server during an HTTP request object which most common use of the request object is to obtain parameter or query string values. you can illustrates more about the request object for parsing form data by using the following program when you will direct copy and paste this in your JSP application.

Methods of the request object is explained one by one as follows:

request.getParameter(String name):
This is the method, used for getting the value of the HTML form attribute. This method returns the string type value i.e. the value of the specified field of an HTML form. This method takes a string type parameter which is the name of the attribute of the html which value has to be retrieved through the request object.

request.getParameterNames():
This is the method of the request object used for getting the enumeration of the attributes of the html form. These values are later retrieved through the java programming language by enumerating the retrieved enumerated data by the method of the request object.

request.getParameterValues(String name):
This is the method of the request object used for getting the string array containing all of the values which are contained by the request parameter. This method takes a String type parameter which is the name of the field of the html form. This method is used where the array of controls of the HTML lies. All the control of the HTML form contains same name and then makes a control array.

request.getCookies():
This is the method of the request object used for getting all the cookies existed in the HTTP request object.

request.getQueryString():
This is the method of the request object used for getting the query string which is the values with the attribute of the html from where the jsp page is referenced.

request.getRequestURI():
This is the method of the request object used for getting the URI of the current page.

request.getHeaderNames():
This method returns the enumerator of all HTTP header names means this method retrieves name of all the headers in enumeration form that is enumerated and get all the name one by one by using the Enumeration.nextElement() up to the last element of the enumeration.

request.getHeader(String hdr):
This is the method return the value of the HTTP header like query string or the URL. This method takes a string parameter which is the header name retrieved by the method getHeaderNames() of the request object that gives all the HTTP header names in the enumeration form which has to be converted into string form later for getting the value of the value of the HTTP header from if one by one.

request.getAttribute(String):
This method is used for getting the value of the attribute which is set through the setAttribute(String attributeName) method of the request object. This method takes a string type parameter written in double quotes ("") i.e. the attribute name that is specified in the page where the value of the attribute is set with the attribute name is to be retrieved either in the current page or any other page by passing the value the attribute name through the request object by using dispatcher method.

request.getAttributeNames():
Above method is used for retrieving all the attributes name in the current session of the page. This method returns the enumerated data which is to be retrieved later by enumerating.

request.setAttribute(String, object):
Above method sets the value of the attribute for the request which is retrieved later either in the current JSP page or the another JSP page by passing the request object through the dispatcher method. The set value of the attribute is retrieved by the getAttribute(String) method of the request object.

request.removeAttribute(String):
This method removes the attribute. This method takes a string type parameter i.e. the attribute name that has to be removed. After applying this method you can't access the attribute. If you specify the method for getting the value of the attribute what has removed, you will get the null value.

This section provides two file for the best explanation about the request object in JSP. These files are as follows:

Above mentioned html file is used for the constructing a form with a file username and another field is the password. When you submit the form you will redirect to the above mentioned JSP file that will show the entered username and the password in a table.

Here is the code the html file:

<html>
<head><title>Request Object In JSP.</title></head>
  <body>
   <form action="RequestObjectInJSP.jsp" method="post">
	<table border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td>User Name: </td>
		<td><input type="text" size="20" name="txtUserName" />
	  </tr>
	  <tr>
		<td>Password: </td>
		<td><input type="password" size="20" name="txtPassword" />
	  </tr>
	  <tr>
	        <td>&nbsp;</td>
		<td><input type="submit" value="Submit" name="B1" /></td>
	  </tr>
	</table>
   </form>
  </body>
</html>

Output for the html file:

Here is the code of the JSP file:

<%@page import="java.util.*" %>
<%
	String username, password;
	if(request.getParameter("txtUserName") == null)
		username = "";
	else
		username = request.getParameter("txtUserName");
	
	if(request.getParameter("txtPassword") == null)
		password = "";
	else
		password = request.getParameter("txtPassword");
%>
<table align="center" bgcolor="ffff00" border="1" cellspacing=
"0" cellpadding="0">
	<tr>
		<td align><b>Your User Name: </b></td>
		<td><%=username %><br/></td>
	</tr>
	<tr>
		<td><b>Your Password: </b></td>
		<td><%=password %></td>
	</tr>
</table>

Output for the above JSP code:

Download html file.

Download jsp file.

                         

» View all related tutorials
Related Tags: c io sed page tag if for example to exam ci e use in m ad age xa xamp s

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.

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

Current Comments

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

Hello,

I'm using JSP. I have a problem: I'm not able to fetch whole string after double quotes(").
Eg.
txtSurvName = rs.getString("SurvName");

Here the values of SurvName like 15" Panel, 21" Panel etc.
But using request.getParameter("txtSurvName"), I won't get whole values but after double quotes("), it skips rest of the string.(like 15" only).

So how should I get whole string???
thank you

Posted by susan on Monday, 04.28.08 @ 08:57am | #58030

Hello,

I'm using JSP. I have a problem: I'm not able to fetch whole string after double quotes(").
Eg.
txtSurvName = rs.getString("SurvName");

Here the values of SurvName like 15" Panel, 21" Panel etc.
But using request.getParameter("txtSurvName"), I won't get whole values but after double quotes("), it skips rest of the string.(like 15" only).

So how should I get whole string???

Thank U,
Jalpa

Posted by Jalpa on Thursday, 08.2.07 @ 16:36pm | #22491

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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 | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.