转发request的应用
1.index.jsp代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <%-- Created by IntelliJ IDEA. User: Administrator Date: 2023-01-15 Time: 11:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title> 登录 </title> </head> <body> <div style= "text-align: center" > <%--这里表单表示的意思,以post方式提交表单,提交到我们的login请求--%> <%--${pageContext.request.contextPath}代表当前项目--%> <form action= "${pageContext.request.contextPath}/login" method= "post" > 用户名:<input type= "text" name= "username" ><br> 密码:<input type= "password" name= "password" ><br> 爱好: <input type= "checkbox" name= "hobby" >女孩</input> <input type= "checkbox" name= "hobby" >代码</input> <input type= "checkbox" name= "hobby" >唱歌</input> <input type= "checkbox" name= "hobby" >电影</input> <br> <input type= "submit" > </form> </div> </body> </html> |
2.成功提示页面success.jsp
1 2 3 4 5 6 7 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <body> <h2>成功!</h2> </body> </html> |
3.代码LoginServlet.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.yin; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username=req.getParameter( "username" ); String password=req.getParameter( "password" ); String [] hobbies=req.getParameterValues( "hobby" ); //req.getParameter("hobby"); System. out .println( "============================" ); System. out .println(username+ ":" +password); System. out .println( "============================" ); //通过请求转发,/代表当前的web应用 req.getRequestDispatcher( "/success.jsp" ).forward(req,resp); //req.getRequestDispatcher(req.getContextPath()+"/success.jsp").forward(req,resp); resp.setCharacterEncoding( "utf-8" ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
4.web.xml代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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>req</servlet-name> <servlet- class >com.yin.LoginServlet</servlet- class > </servlet> <servlet-mapping> <servlet-name>req</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」