JSP
JSP Elements - JSP Declaration, JSP Syntax, JSP Expression, JSP Comments
In this tutorial, we will be learning the basic tags of JSP and how to add comments into JSP....
JSP actions which use constructs in XML syntax to control the behavior of the servlet engine. We will learn more in detail about various JSP Action elements like client request, server response, HTTP status codes.
In this tutorial, you will learn-
Different headers are described below:
Header | Description | Example |
---|---|---|
Accept | It specifies MIME types that browser or other clients can handle | Image/png or image/jpeg |
Accept-charset | It uses the character set used by the browser to display the information | ISO-8859-1 |
Accept- Encoding | It specifies type of encoding handled by the browser | Gzip or compress |
Accept-language | It specifies clients specified language | En,en_us |
Authorization | Header used by clients when trying to access password protected web pages | |
Connection | It indicates whether client can handle persistent HTTP connections(browser can retrieve multiple files) | Keep-alive |
Content-length | Applicable to post requests. It gives size of post data of bytes | |
Cookie | Returns cookie to server(those which were previously sent to the browser) | |
Host | Specifies the host and port of the original URL | |
If modified since | It indicates that it requires only a page if it has been changed or modified | |
If unmodified since | It indicates that it requires a page only if it has not been changed or modified | |
Referrer | Indicates URL of referring URL page | |
User-agent | Identifies browser or client making request | |
Following methods are used to read the HTTP header in JSP page:
Example:
In the example below, we are using different methods using request object
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.* java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Client Request Guru JSP</title> </head> <body> <h2>Client Request Guru JSP</h2> <table border="1"> <tr> <th>guru header</th><th>guru header Value(s)</th> </tr> <% HttpSession gurusession = request.getSession(); out.print("<tr><td>Session Name is </td><td>" +gurusession+ "</td.></tr>"); Locale gurulocale = request.getLocale (); out.print("<tr><td>Locale Name is</td><td>" +gurulocale + "</td></tr>"); String path = request.getPathInfo(); out.print("<tr><td>Path Name is</td><td>" +path+ "</td></tr>"); String lpath = request.get(); out.print("<tr><td>Context path is</td><td>" +lipath + "</td></tr>"); String servername = request.getServerName(); out.print("<tr><td>Server Name is </td><td>" +servername+ "</td></tr>"); int portname = request.getServerPort(); out.print("<tr><td>Server Port is </td><td>" +portname+ "</td></tr>"); Enumeration hnames = request.getHeaderNames(); while(hnames.hasMoreElements()) { String paramName = (String)hnames.nextElement(); out.print ("<tr><td>" + paramName + "</td>" ); String paramValue = request.getHeader(paramName); out.println("<td> " + paramValue + "</td></tr>"); } %>
Explanation of the code:
Code Line 17: Using request object, we are getting the session object of that particular session, and we get the object value of that session
Code Line 19: Using request object, we are getting locale of that particular session i.een_US locale for that JSP.
Code Line 21: Using request object, we are getting path info for that JSP. In this case, it is null as there is no path for URL mentioned.
Code Line 23: Using request object, we are getting context path, i.e., root path
Code Line 25: Using request object, we are getting the server name.
Code Line 27: Using request object, we are getting server port.
Code Line 29-35: Using request object, we are getting header names which come out as enumeration, and hence we get all header values in the header names.
In this, we get all header values as a cookie, host, connection, accept language, accept encoding.
When you execute the above code, you get the following output:
Output:
We are getting the series of values like session name, locale name, path name, server name, port name, host, context path and all the header values of that JSP.
Response headers are mentioned below:
Header | Description |
---|---|
Allow | It specifies the request methods like GET, POST that server is requesting |
Cache-control | The response document can be cached. It can be public, private and no cache. No cache specifies that document should not be cached |
Connection | It instructs whether the browser should use savedHTTPConnections or not. Close value represents that browser should not use persistent in HTTPConnections and "keep-alive" means using persistent connections |
Content-disposition | To ask the user whether to save the response to disk or not |
Content-encoding | Page has to be encoded during transmission |
Content-length | Number of bytes in the response |
Content type | It specifies the MIME type of response |
Expires | Specifies till when the content should be considered out of date and should not be cached |
Last modified | It indicates when the document was last modified |
Location | It should be included with all responses which have status code has 300 as status code |
Refresh | It specifies how to find the updated page. |
Retry-after | It can be used with 503 response to tell client of how soon it can repeat request |
Set-cookie | Specifies the cookie associated with the page |
Following are the methods using response object:
Example:
In this example, we are covering different methods getLocale,flushbuffer, getWriter, get ContentType, setIntHeader.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.* java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Action Response</title> </head> <body> <center> <h2>Guru Response</h2> <% Locale lcl = response.getLocale(); out.println("Locale is : " + lcl + "\n"); response.flushBuffer(); PrintWriter output = response.getWriter(); output.println("This is from writer object"); String type = response.getContentType(); out.println("The content type : " + type + "\n"); // Set refresh,autoload time as 5 seconds response.setIntHeader("Refresh", 5); //Get current time Date dt = new Date(); out.println("Today's date is : " +dt.toString() + "\n"); %> </center> </body> </html>
Explanation of the code:
Code Line 13: Using response object, we get locale object of this JSP session
Code Line 15: Using response object, flushbuffer is used to force the buffer content into client
Code Line 16: Using response object, we get writer object which get output in the output stream
Code Line18: Using response object, we get content type i.e. MIME type of response object
Code Line 21: Using response object, it is used to autoload in every 5 seconds as 5 is set as the second parameter
When you execute the above code, you get the following output:
Output:
The codes fall in following 5 categories:
Some of the common status codes are below:
Some of its methods are listed below:
It sets the status code whichever we want to set in that JSP Page.This will give us the message of status code which has been set
It generates 302 response along with the location header giving URL of the new document
It sends the status code along with the short message and it is formatted inside HTML document.
Example:
In this example, we are sending error to JSP page explicitly.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Status Code</title> </head> <body> <% response.sendError(404,"Guru Page Not Found"); %> </body> </html>
Explanation of the code:
Code Line 10: Using response object we are sending the error to a page with two parameters.
If you execute the above code, you get the following output:
Output:
Here we get error code as 404, which was sent from the code and also displays"Guru Page not found" message seen in the output.
Summary:
In this tutorial, we will be learning the basic tags of JSP and how to add comments into JSP....
Download PDF 1) Explain what is Maven? How does it work? Maven is a project management tool. It...
Download PDF 1) Explain JSP and tell its uses. JSP stands for Java Server Pages. It is a...
What is JSP Implicit object? JSP implicit objects are created during the translation phase of JSP...
The database is used for storing various types of data which are huge and has storing capacity in...
What is Expression Language (EL)? Expression Language (EL) is mechanism that simplifies the...