Let the browser to refresh the web page or jump to other pages

xiaoxiao2021-04-05  255

A refresh header field is defined in the HTTP protocol to automatically refresh the page after telling the browser. After the time setting value of the Refresh header field, you can also use the semicolon (;), then specify a URL address, which will automatically access the URL address after the specified time value will automatically access the URL address.

: Hand experience: the effect of refresh header field

(1) Write a servlet that sets the Refresh header field according to the routine 5-2.

Routines 5-2 RefreshServlet.java

Import java.io. *;

Import javax.servlet. *;

Import javax.servlet.http. *;

Public Class RefreshServlet Extends Httpservlet

{

Public void service (httpservletRequest Request,

HttpservletResponse response) THROWS servletexception, ioException

{

// Review one of the following two statements, view their respective running effects

Response.setHeader ("Refresh", "2");

//Response.setheader ("refresh "," 22;url=http://www.it315.org ");

Response.Getwriter (). Println (New java.util.date ());

}

}

Compile the RefreshServlet.java source file using Compile.bat to ensure that the Class file generated after compile is stored in the / webApps / it315 / web-inf / class directory.

(2) Modify the /webapps/it315/web-inf/web.xml file, in which the corresponding position is increased as the following sections:

refreshServlet

refreshServlet

......

......

refreshServlet

/ servlet / refreshservlet

After saving the Web.xml file, restart Tomcat.

(3) Enter the following address in the browser address bar:

http: // localhost: 8080 / it315 / servlet / refreshServlet

You can see the effect of automatically refresh the web document after the browser two seconds.

(4) Comment out the statement of the first section of the RefreshServlet.java source file comment out, and cancels the comments of the second sentence of the SETHEADER method. After recompilating the refreshServlet.java source file and waits Tomcat to reload the RefreshServlet, repeat step (3) operation, you can see the running effect of http://www.it315.org after two seconds.

转载请注明原文地址:https://www.9cbs.com/read-132041.html

New Post(0)