public
void addHeader(String name, String value)
The
addHeader
method
adds a header value to the set with a given name. If there are no
headers already associated with the name, a new set is created.
Main.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>set Header</title> </head> <body> <form method="get" action="/servlet/AddHeader"> <input type="submit" value ="Get Data"> </form> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <servlet> <servlet-name>AddHeader</servlet-name> <servlet-class>AddHeader</servlet-class> </servlet> <servlet-mapping> <servlet-name>AddHeader</servlet-name> <url-pattern>/AddHeader</url-pattern> </servlet-mapping> </web-app>
AddHeader.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddHeader extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ try(PrintWriter out = res.getWriter()){ res.setContentType("text/html"); res.addHeader("Refresh", "5; URL=https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html"); try{ Thread.sleep(10000); } catch(InterruptedException e){ } } } }
Output
After
5 seconds the page redirect to '
https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html'
Note:
Using
refresh header we can instruct a web browser to automatically
refresh the current web page or frame after a given time interval.
No comments:
Post a Comment