javaweb项目网页自动刷新
一、项目结构
效果:
- 用ServletRefresh访问网页/refresh
- 然后会将请求转发到index.jsp页面
- 等待3秒后或者点击链接会自动刷新,跳转到首页home.html
二、ServletRefresh.Java
package net.test.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "ServletRefresh",urlPatterns = "/refresh")
public class ServletRefresh extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//3秒后自动跳转或者点击链接跳转首页
String message=" <meta http-equiv='refresh' content='3;/hello/home.html'>" +
"3秒后将自动跳转到首页,如果没有跳转,请点击" +
"<a href='/hello/home.html'>跳转链接</a>";
//设置属性
request.setAttribute("message",message);
//请求转发
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
}
三、index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/1/29
Time: 0:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
#获取属性
<%=request.getAttribute("message")%>
</body>
</html>
四、home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h3>网站首页</h3>
</body>
</html>
五、部署及运行
访问/refresh页面
3秒后或者点击链接,跳转到如下页面
还没有评论,来说两句吧...