web方法的重定向方式:
方式1:
response.setStatus(302);
respooen.setHeader(“location”,” 重定向路径”);
方式2:
response.sendRedirect(“重定向路径”);
注意: 方式2是方式一方法的封装.
代码如下
servlet1
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("这是servlet1");
response.sendRedirect("/gjq/Ser2");
//上面方法用来定向到ser2
}
servlet2:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("servlet2");
}
html部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/gjq/Ser1"> 重定向</a>
</body>
</html>