Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
 
Book Must Read
 
 

Request object

 
 
Print   Email
 
     
 

Request Object in JSP is most usable object. This request object take information from the user and send to server, then this request is proceed by server and response back to user. The process starts when we write address of any page in browser and press enter to page. Request object can be used in

Query String

When using query String, data is passed to server in form of URL string. This URL string is visible on browser and anyone can see it. E.g

http://www.domainName.com/concerts.jsp?id=9&type=Exhibition

This is query string after page name. id is key and 9 is value. This query String either self made or can send by form property. This can get with request.getParameter(“Key”) method. This will give us 9.

Query String self made example in JSP

queryString.jsp

<%@ page  language="java" import="java.sql.*" %>
<html>
<head>
<title>Query String in JSP</title>
</head>

<body>
<a href="queryString.jsp?id=9&name=joe">This is data inside query string</a>
<%
String queryString=request.getQueryString();
out.print("<br>"+queryString);
%>
</body>
</html>

We can get single id and value instead of using getQueryString method of request.

Query String example with getParameter

queryStringParameter.jsp

 
<%@ page  language="java" import="java.sql.*" %>
<html>
<head>
<title>Query String in JSP</title>
</head>

<body>
<a href="queryString.jsp?id=9&name=joe">This is data inside query string</a>
<%
String queryStringVariable1=request.getParameter("id");
String queryStringVariable2=request.getParameter("name");
out.print("<br>"+queryStringVariable1);
out.print("<br>"+queryStringVariable2);
%>
</body>
</html>

Form
Form is most important part of request object in JSP. It takes value from user and sends it to server for further processing. Suppose we have a registration form, and value in field have to save in database. We need to get these values from browse and get in server side; once we get this value in java, we can save in database through JDBC or any database object. Form in html has two properties get or post method, get method send information to server in query string. Post method doesn’t send data in query string in to server. When data is sent to server it is not visible on browser. Post method as compare to get method is slower. Get method has a limit of string length in some kilobytes.

Form Example in JSP with request object

formGetMethod.jsp

 
<%@ page  language="java" import="java.sql.*" %>
<%
String queryStringVariable1=request.getParameter("name");
String queryStringVariable2=request.getParameter("className");
%>
<html>
<head>
<title>Query String in JSP</title>
</head>

<body>
<%

out.print("<br>Field 1 :"+queryStringVariable1);
out.print("<br>Field 2 :"+queryStringVariable2);

%>

<form method="get" name="form1">
Name <input type="text" name="name"> <br><br>
Class <input type="text" name="className"> <br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Request object with Check box form in JSP

formGetParameterValues.jsp

 
<%@ page  language="java" import="java.sql.*" %>
<%
String queryStringVariable1=request.getParameter("name");
String[] queryStringVariable2=request.getParameterValues("className");
%>
<html>
<head>
<title>Query String in JSP</title>
</head>

<body>
<%
try{
    out.print("<br>Field 1 :"+queryStringVariable1);
    
    for(int i=0;i<=queryStringVariable2.length;i++){
     out.print("<br>Check box Field "+i+" :"+queryStringVariable2[i]);
    }
}
catch(Exception e)
{
 e.printStackTrace();
}

%>
<br>
<br>
<form method="get" name="form1">
Name <input type="text" name="name"> <br><br>
 class 1
 <input type="checkbox" name="className" value="c1">
 class 2 <input type="checkbox" name="className" value="c2"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Request object with radio button form in JSP

formGetRadio.jsp

 
<%@ page  language="java" import="java.sql.*" %>
<%
String inputVariable=request.getParameter("name");
String[] radioVariable=request.getParameterValues("className");
%>
<html>
<head>
<title>Query String in JSP</title>
</head>

<body>
<%
try{
    out.print("<br>Field 1 :"+inputVariable);
    
    for(int i=0;i<=radioVariable.length;i++){
     out.print("<br>Check box Field "+i+" :"+radioVariable[i]);
    }
}
catch(Exception e)
{
 e.printStackTrace();
}

%>
<br>
<br>
<form method="get" name="form1">
Name <input type="text" name="name"> <br><br>
 class 1
 <input type="radio" name="className" value="c1"> 
 class 2 <input type="radio" name="className" value="c2"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Cookies
Cookies use request object to get values through in cookie file. It uses request.getCookies() method to get cookie and return in cookie[] array.

 

Server variable


Server variable give server information or client information. Suppose we need to get remote client IP address, browser, operating system, Context path, Host name any thing related to server can be get using request object in JSP.

Server variable example in JSP

serverVariable.jsp

 
<%@ page contentType="text/html; charset=utf-8" language="java" %>
<html>
<head>
<title>Server Variable in JSP</title>
</head>


<body>
Character Encoding : <b><%=request.getCharacterEncoding()%></b><br />

Context Path : <strong><%=request.getContextPath()%></strong><br />

Path Info : <strong><%=request.getPathInfo()%></strong><br />

Protocol with version: <strong><%=request.getProtocol()%></strong><br />

Absolute Path file:<b><%=request.getRealPath("serverVariable.jsp")%></b><br/>

Client Address in form of IP Address:<b><%=request.getRemoteAddr()%></b><br/>

Client Host : <strong><%=request.getRemoteHost()%></strong><br />

URI : <strong><%=request.getRequestURI()%></strong><br />

Scheme in form of protocol : <strong><%=request.getScheme()%></strong><br />

Server Name : <strong><%=request.getServerName()%></strong><br />

Server Port no : <strong><%=request.getServerPort()%></strong><br />

Servlet path current File name : <b><%=request.getServletPath()%></b><br />
</body>
</html>

Properties and method of Request objects

Method Return Description
equals(Object obj) Objects Matching objects
getAttribute(String str) Objects Returns the String associated with name in the request scope
getAttributeNames() Enumeration an Enumeration of attribute names
getAuthType() String  
getCharacterEncoding() String Returns character encoding for page in request MIME type charset=iso-8859-1
getContentLength() int get length of content body in request
getContentType() String  
getContextPath() String  
getCookies() Cookie[]  
getDateHeader(String str) long get request header with given name and date value
getHeader(String str) String get request header with given name and value
getHeaderNames() Enumeration  
getHeaders(String str) Enumeration  
getInputStream() ServletInputStream  
getIntHeader(String str) int  
getLocalAddr(String str) String  
getLocale(String str) Locale  
getLocales() Enumeration  
getLocalName() String  
getLocalPort() int  
getMethod() String  
getParameter(String str) String  
getParameterMap() Map  
getParameterNames() Enumeration  
getParameterValues(String str) String  
getPathInfo() String  
getPathTranslated() String  
getProtocol() String  
getQueryString() String  
getReader() BufferedReader  
(D) getRealPath() String  
getRemoteAddr() String  
getRemoteHost() String  
getRemotePort() int  
getRemoteUser() String  
getRequestDispatcher(String str) RequestDispatcher  
getRequestedSessionId() String  
getRequestURI() String  
getRequestURL() StringBuffer  
getScheme() String  
getServerName() String  
getServerPort() int  
getServletPath() String  
getSession() HttpSession  
getSession(boolean true|false) HttpSession  
getUserPrincipal() Principal  
hashCode() int  
isRequestedSessionIdFromCookie() boolean  
isRequestedSessionIdFromURL() boolean  
isRequestedSessionIdValid() boolean  
isSecure() boolean  
isUserInRole() boolean  
removeAttribute(String str) void  
setAttribute(String str, Object obj) void  
setCharacterEncoding(String str) void Set a character encoding for send to client request body charset=iso-8859-1
toString() String  

 

 

 

 
If You Like this Article, share this

 
 
     
   
  View Forum Post  
 
Optimization of SQL queries for better performance
Example of Between in mysql
Example of sorting in mysql table
Example of join in mysql
Example of subquery in mysql
union join in mysql with example
error too many connection in mysql
calculate second highest salary
How to use select queries with array in mysql
Radio button pre selected in struts
Exception in thread "main" java.lang.NoClassDefFoundError
tree view in jsp
SSL implement in tomcat and java certificate
WARNING: Internal error flushing the buffer in release()
DynaForm validation problem in struts not working
Accessing JavaBean in servlet or servlet in javaBean.
 
     
     
   
     
     
     
 
  Privacy Policy