In this tutorial we are going to see how to set header information in servlet response with example program. Click here to read more about HTTP/1.1 Header param with Definitions
List of Response Headers
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called HttpResponseHeader as shown in figure.
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available in HttpServlet. Select appropriate method you need.
7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml
Servlet Mapping
web.xml
8. Add the required code inside doGet() method.
HttpResponseHeader.java
9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse)
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html
10.call the URL which is mapped in web.xml.
http://localhost:8080/ServletExample/HttpResponseHeader
see the page get refresh every 5 seconds.
Output
If you call the URL with Live HTTP header plug-in. see can see the following content in header.
http://localhost:8080/ServletExample/HttpResponseHeader
GET /ServletExample/HttpResponseHeader HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
refresh: 5
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 425
Date: Sun, 30 Mar 2014 02:27:54 GMT
List of Response Headers
Header
|
Description
|
Allow | This header specifies the request method that server support – GET,POST,etc |
Cache-Control | Values of this header are public,private,no-cache,etc public – Document is cacheable private – Document for single user no-cache – Document should not be cached |
Content-Disposition | The browser ask the user to save the response to disk. |
Content-Encoding | This specifies the way in which page was encoded |
Content-Language | This specifies the language which the document written. |
Content-Length | This specifies the number of bytes in the response. |
Content-Type | This specifies MIME type of response document |
Expires | This specifies the time at which the content can be considered as out of date. |
Last-Modified | This specifies when the document was last changed. |
Location | This notifies the browser of the document address. The browser automatically reconnects to this location and retrieves the new document. |
Refresh | Used to specify the time which the page get refreshed. |
Retry-After | This header can be used in conjunction with a 503 response to tell the client how soon it can repeat its request. |
Set-Cookie | This specifies cookie associated with the page |
Connection | This header instructs the browser whether to use persistent in HTTP connections or not. A value of close instructs the browser not to use persistent HTTP connections and keep-alive means using persistent connections. |
2. Create the Project called ServletExample as given below.
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called HttpResponseHeader as shown in figure.
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available in HttpServlet. Select appropriate method you need.
7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml
Servlet Mapping
<servlet> <servlet-name>Your Servlet Name</servlet-name> <servlet-class>Fully Qulaified Servlet Class Name</servlet-class> </servlet> <servlet-mapping> <servlet-name>Your Servlet Name</servlet-name> <url-pattern>/URL to Call Servlet</url-pattern> </servlet-mapping>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ServetExample</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>HttpResponseHeader</servlet-name> <servlet-class>com.javatutorialscorner.servlet.HttpResponseHeader</servlet-class> </servlet> <servlet-mapping> <servlet-name>HttpResponseHeader</servlet-name> <url-pattern>/HttpResponseHeader</url-pattern> </servlet-mapping> </web-app>
8. Add the required code inside doGet() method.
HttpResponseHeader.java
package com.javatutorialscorner.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HttpResponseHeader */ public class HttpResponseHeader extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HttpResponseHeader() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setIntHeader("Refresh", 5); response.setContentType("text/html"); PrintWriter writer = response.getWriter(); Date date = new Date(); writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet Response Header</title>" + "</head><body><h1>Java Tutorials Corner - Servlet Response Header</h1><table><tr>" + "<td>Current Date & Time : </td><td>" + date + "</td></tr></table></body></html>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse)
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html
10.call the URL which is mapped in web.xml.
http://localhost:8080/ServletExample/HttpResponseHeader
see the page get refresh every 5 seconds.
Output
If you call the URL with Live HTTP header plug-in. see can see the following content in header.
http://localhost:8080/ServletExample/HttpResponseHeader
GET /ServletExample/HttpResponseHeader HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
refresh: 5
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 425
Date: Sun, 30 Mar 2014 02:27:54 GMT
0 comments:
Post a Comment