讲给Android程序员看的前端系列教程(40集免费视频教程+源码)
版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
请求重定向概述
有时候,客户端发起请求;但是,服务端Servlet可能无法完成全部工作。这时, 我们需要使用请求重定向来完成后续的工作。所谓请求重定向,指的是Web服务器接收到客户端的请求后让客户端重新发送指向其它资源的请求。
为了实现请求重定向,在HttpServletResponse接口中定义了sendRedirect( ) 方法。该方法用于生成302响应码和Location响应头,从而通知客户端重新访问 Location响应头中指定的URL。
图示如下:
请求重定向示例
示例描述
用户登录时如果用户名为lucy,密码为123456则登录成功并跳转至welcome页面;否则跳转至登录页面。
login页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h2 style="color: red;">本文作者:谷哥的小弟</h2>
<h2 style="color: red;">博客地址:http://blog.csdn.net/lfdfhl</h2>
<form action="/Redirect01/loginServlet" method="post">
用 名: <input type="text" name="username" /><br><br>
密 码: <input type="password" name="password"/><br><br>
<input type="submit" value="登录" />
</form>
</body>
</html>
welcome页面
<!DOCTYPE html>
<!-- 本文作者:谷哥的小弟-->
<!-- 博客地址:https://blog.csdn.net/lfdfhl-->
<html>
<head>
<meta charset="utf-8">
<title>success</title>
</head>
<body>
<h2 style="color: red;">本文作者:谷哥的小弟</h2>
<h2 style="color: red;">博客地址:http://blog.csdn.net/lfdfhl</h2>
<h2 align="center">登陆成功,欢迎来访</h2>
</body>
</html>
LoginServlet
package cn.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*
* HttpServletResponse示例:
* 请求重定向
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 4008184113098539992L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
if(username.equals("lucy")&&password.equals("123456")) {
//request.getContextPath()为:/Redirect01
System.out.println(request.getContextPath());
//登录成功则重定向到welcome页面
response.sendRedirect(request.getContextPath()+"/welcome.html");
}else {
//登录失败则重定向到login页面
response.sendRedirect(request.getContextPath()+"/login.html");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
配置文件web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Redirect01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>cn.com.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
转载:https://blog.csdn.net/lfdfhl/article/details/101643088
查看评论