HttpServletRequest应用之实现请求转发
声明
本文部分内容参考自其他作者原创文章,仅供个人学习留档,特此声明
参考文章链接
(1条消息) B站---【狂神说Java】JavaWeb入门到实战---笔记_夜里的雨的博客-CSDN博客_狂神说java笔记
实现请求转发
1、代码
1.用于实现后台获取登录信息及实现请求转发的LoginServlet代码
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobbies");
System.out.println("============");
//后台接收中文乱码问题
System.out.println(username);
System.out.println(password);
System.out.println(Arrays.toString(hobbies));
System.out.println("============");
System.out.println(req.getContextPath());
//通过请求转发
//这里的 / 代表当前项目的webapp
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
其中需要注意这行代码 req.getRequestDispatcher("/success.jsp").forward(req,resp);
这里的 / 代表当前项目的webapp
2.用于注册映射路径的web.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.xy.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
3.用于构造用户登录页面的index.jsp代码
<%@page contentType="text/html;" pageEncoding="UTF-8"%>
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录页面</h1>
<div>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名 : <input type="text" name="username"> <br>
密码 : <input type="password" name="password"> <br>
爱好 :
<input type="checkbox" name="hobbies" value="打游戏">打游戏
<input type="checkbox" name="hobbies" value="敲代码">敲代码
<input type="checkbox" name="hobbies" value="唱歌">唱歌
<input type="checkbox" name="hobbies" value="看电影">看电影
<br><br>
<input type="submit">
</form>
</div>
</body>
</html>
其中需要注意的是这行代码 action="${pageContext.request.contextPath}/login
${pageContext.request.contextPath}
代表当前项目的网址 http://localhost:8080/request
4.请求转发指向页面的success.jsp代码
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登陆成功</h1>
</body>
</html>
2、代码测试
-
完善用户信息并提交
-
提交后成功转向success.jsp网址,由于是请求转发,url不会被改变(307)
-
观察IDEA控制台,得到用户信息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律