两种跳转方式
1)请求转发Forward——URL不跳转
登陆错误时候显示错误界面
2)请求重定向Redirect——URL跳转
案例:登陆后跳转网站首页
loginError.jsp
<%--
Created by IntelliJ IDEA.
User: 18582
Date: 2020/3/11
Time: 16:17
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>
</body>
<center>
<h2>用户登陆错误界面</h2>
<%=request.getAttribute("errorMessage")%>
</center>
</html>
LoginSerblet.java
package net.pp.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 = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = "chuchu";
String userPass = "123456";
String name = request.getParameter("name");
String passWord = request.getParameter("passWord");
//请求转发
if(!name.equals(userName)){
//账户不存在
request.setAttribute("errorMessage", "账户不存在");
request.getRequestDispatcher("loginError.jsp").forward(request, response);
}else if(!passWord.equals(userPass)){
//密码错误
request.setAttribute("errorMessage", "密码错误");
request.getRequestDispatcher("loginError.jsp").forward(request, response);
}else{
//重定向——跳转后注意,访问地址加上项目地址
// 法一
// response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
// response.setHeader("location", "https://www.baidu.com/");
//法二
response.sendRedirect("/hello/home.html");
}
}
}
结果如下:
3.自动刷新
index.jsp
<%--
Created by IntelliJ IDEA.
User: 18582
Date: 2020/3/9
Time: 17:50
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>
RefreshServlet.java
package net.pp.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 = "RefreshServlet", urlPatterns = "/refresh")
public class RefreshServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//JSP中实现自动刷新
String message = "<meta http-equiv='refresh' content='3;url=/hello/home.html'>3秒后自动跳转到首页,如果没有跳转,请点击<a href='/hello/home.html'>跳转链接</a>";
request.setAttribute("message", message);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
//自动刷新
private void RefreshDemo(HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=utf-8");
response.setHeader("refresh", "3;url='/hello/home.html'");
response.getWriter().print("3s后自动刷新");
}
}
结果是:
来源:CSDN
作者:爱吃串串的瘦子
链接:https://blog.csdn.net/weixin_41988628/article/details/104800177