JSP Response Implicit Object

Response Implicit Object:

In JSP, request is an implicit object of type HttpServletRequest. This can be used to get request information such as parameter, header information etc. This is created by the web container for each request done. This is mainly used for modifying the response which is being sent to the browser after processing the client’s request.

 

The following are the various methods which can be used in our servlet program.

void setContentType(String type)
void sendRedirect(String address)
void addHeader(String name, String value)
void setHeader(String name, String value)
boolean containsHeader(String name)
void addCookie(Cookie value)
void sendError(int status_code, String message)
void setStatus(int statuscode)

 

void setContentType(String type):

This method sets the content type of the response sent to the client or server if response is not yet committed. This method tells browser, the type of response data by setting up the character encoding. The information sets by this method helps browser to interpret the response.

So for example if the content of the response which we are getting may be in different types say like .html, .pdf, .jpeg and etc.

So by using this method we can set the type of content which is being responded.

response.setContentType("form/html");
response.setContentType("scene/jpeg");
response.setContentType("resume/pdf");

 

void sendRedirect(String address) – 

This method  redirects the control to a new JSP page. For e.g. When the browser would detect the below statement, it would be redirected to the beginnersbook.com from the current JSP page.

 

This can be understood by the below following example wherein we have created a main.jsp and a redirect jsp (welcome.jsp) to re-direct it to the google page.

 

Please find the below example which cane clear out the things easily.

Main.jsp:

<html>
<head>
</head>
<body>
<formaction="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>

welcome.jsp:

<%@ page import = "java.util.* "%>
<html>
<body>
<%
response.sendRedirect("http://www.google.com");
%>
</body>
</html>

Output of the code when running the main.jsp file:

Response

After clicking any name and then pressing “go” then we will get the below screen with the details as expected.
Response

Response

void addHeader(String name, String value) : 

This addHeader method adds a header to the response. It’s just like adding a heading in a HTML file. For example – We can use the below statement in the method as an example. This can be changed according to the requirements in the JSP file.

 

response.addHeader("website","www.programmingtute.com");

 

void setHeader(String name, String value) :

This method sets the header value or we can say it can also override a header value with a new value. Say in the above example I have added the header value as “Programmingtute.com” but now I want to change the header value as “gmail.com” this method can be used. So this method sets as well as overrides a current value of a header.

 

response.addHeader("website","www.gmail.com");

 

boolean containsHeader(String name): 

This method returns a Boolean value true/false. As the name suggests it sends in a  Boolean value for the return type. So this method will check for a specific and if its present it will return a true value and its not then it will return a false.

So in the above example I have header named as “website” and if so I check that it will return a true but if I check www.gmail.com or www.programmingtute.com it will return a false as it checks only for the header and not is value.

 

response.containsHeader("Site");
response.containsHeader("www.gmail.com");
reponse.containsHeader("www.programmingtute.com");

So in the above example the first alone will return true other two statements will return false.

 

void addCookie(Cookie cookie) : 

 This method adds a cookie to the response. The syntax of the above is as follows:

response.addCookie(Cookiewebsite);
response.addCookie(Cookiewebsiteinfo);

 

void sendError(int status_code, String message) : 

This is used to send error response with a code and an error message. This method returns the error back to the client with the Error Code passed. If the response is already committed, then it throws an IllegalStateException exception.

response.sendError(404,"Page not found error");

 

void setStatus(int statuscode): 

This method is used to set the HTTP status to a given value.

Say for example we can set the status of the HTTP response d code to 404.

 

response.setStatus(404);

 

Lets see an example of a sample user login screen using JSP.

Main.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="verify.jsp">
UserId: <input type="text" name="userid" /> <br><br>
Password: <input type="text" name="password" /> <br><br>
<input type="submit" value="Go"/>
</form>
</body>
</html>

verify.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String userid=request.getParameter("userid");
String password=request.getParameter("password");
session.setAttribute("session-userid", userid);
if(userid.equals("programming") && password.equals("programming"))
{
response.sendRedirect("pass.jsp");
}
else
{
response.sendRedirect("fail.jsp");
}
%>
</body>
</html>

pass.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String content=(String)session.getAttribute("session-userid");
out.println("Welcome "+ content+"!!");
%>
</body>
</html>

fail.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String content=(String)session.getAttribute("session-userid");
out.println("Hi "+ content+". Id/Password is wrong. Please try Again.");
%>
</body>
</html>

When running the main.jsp file using Tomcat server, then we get the below main screen:

Response

Please type in the userid and password as we have hardcoded them in the jsp file to test if this works correctly. And then press “Go” button.
Response

Now we will see the above screen logging and processing the pass.jsp after verifying the hard coded userid and password. Now if we change the userid and password it will throw an error as below.
Response

Response

We will fine tune this JSP program to check additional validations like a proper email id and password checking with capital letters and numbers in the future tutorials where we can add these in servlet and have some fun.

Posted on July 25, 2014 in Java Server Pages

Share the Story

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top