【Servlet 实战】请求转发 / 重定向核心解析 + 完整登录功能实现(附源码 + 运行截图)

发布时间:2026/5/17 8:03:58

【Servlet 实战】请求转发 / 重定向核心解析 + 完整登录功能实现(附源码 + 运行截图) 一、Servlet 核心跳转技术请求转发 vs 重定向Servlet 作为 Java Web 开发的基础核心请求转发forward和重定向sendRedirect是页面跳转的两大核心技术。在 Java Web 开发中请求转发和重定向的底层原理、使用场景差异显著选错方式会直接导致功能异常。1. 请求转发核心特点服务器内部跳转、浏览器地址栏不变、一次 HTTP 请求、可通过 request 域共享数据适用场景Servlet 之间的内部数据传递、页面跳转无需改变地址栏实战代码ServletReq1.java数据存入 转发WebServlet(/req1) public class ServletReq1 extends HttpServlet{ Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ req.setAttribute(phone,137224645465); req.getRequestDispatcher(req2).forward(req,resp); } }ServletReq2.java数据取出 响应WebServlet(/req2) public class ServletReq2 extends HttpServlet { Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { String phone req.getAttribute(phone).toString(); resp.setContentType(text/html;charsetUTF-8); PrintWriter out resp.getWriter(); out.println(h1我的电话号码是:phone/h1); } }运行后会弹出浏览器弹窗在网址后加上/req1地址栏仍为 /req1页面显示req2的响应内容成功获取到转发传递的手机号控制台无额外日志仅一次请求。2. 重定向核心特点浏览器二次请求、地址栏跳转为目标地址、两次 HTTP 请求、无法通过 request 域传递数据适用场景登录成功 / 失败跳转、表单提交后跳转避免重复提交、跳转到外部网站实战代码RespServlet1.java发起重定向WebServlet(/resp1) public class RespServlet extends HttpServlet { Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{ System.out.println(这是response1); PrintWriter out resp.getWriter(); out.println(h11111111/h1); resp.sendRedirect(resp2); } }RespServlet2.java接收二次请求WebServlet(/resp2) public class RespServlet2 extends HttpServlet { Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { System.out.println(这是response2); PrintWriter outresp.getWriter(); out.println(h12222222/h1); } }运行效果运行后会弹出浏览器弹窗在网址后加上/req1地址栏自动跳转为 /resp2页面显示2222222控制台依次输出resp1和resp2的日志两次独立请求。3. 核心区别对比表此为ai生成表格二、Servlet 进阶实战完整用户登录功能实现基于上述跳转技术实现登录页面→参数获取→非空校验→账号密码匹配→结果跳转的全链路登录功能包含前端页面和后端验证代码可直接复制运行。整体流程login.html登录页→ 表单提交 →loginServlet.java后端验证→ 成功→重定向success.html/ 失败→重定向fail.html1. 前端登录页面login.htmlform actionloginServlet methodGET 用户名: input typetext nameusernamebr/ 密码: input typetext namepasswordbr/ input typesubmit value提交/ /form2. 后端验证 ServletloginServlet.java核心逻辑获取前端参数→非空校验→账号密码匹配固定为 root/root→根据结果重定向对应页面放置在src/main/java/包名/下。WebServlet(/loginServlet) public class loginServlet extends HttpServlet { Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException { String usernamereq.getParameter(username); String passwordreq.getParameter(password); if (username null || passwordnull){ System.out.println(用户名或密码为空); }else if (username.equals(root) password.equals(root)) { resp.sendRedirect(success.html); }else{ System.out.println(登录失败); resp.sendRedirect(fail.html); } } }3. 登录结果页面success.html/fail.html均放置在webapp根目录下添加简单样式提升页面体验同时提供重新登录入口。登录成功页success.html!DOCTYPE html html langzh-CN head meta charsetUTF-8 title登录成功/title style * { margin: 0; padding: 0; box-sizing: border-box; font-family: Microsoft Yahei, sans-serif; } body { background-color: #f0f9ff; display: flex; justify-content: center; align-items: center; min-height: 100vh; } .success-box { background: white; padding: 40px 60px; border-radius: 12px; box-shadow: 0 4px 20px rgba(0, 163, 255, 0.2); text-align: center; } .success-icon { font-size: 60px; color: #52c41a; margin-bottom: 20px; } h1 { color: #262626; font-size: 28px; margin-bottom: 10px; } p { color: #595959; font-size: 16px; margin-bottom: 30px; } .btn { display: inline-block; padding: 10px 30px; background-color: #1890ff; color: white; border-radius: 6px; text-decoration: none; transition: background 0.3s; } .btn:hover { background-color: #40a9ff; } /style /head body div classsuccess-box div classsuccess-icon✓/div h1登录成功/h1 p欢迎回来您已成功登录系统/p a hreflogin.html classbtn返回首页/a /div /body /html登录失败页fail.html!DOCTYPE html html langzh-CN head meta charsetUTF-8 title登录失败/title style * { margin: 0; padding: 0; box-sizing: border-box; font-family: Microsoft Yahei, sans-serif; } body { background-color: #fff2f0; display: flex; justify-content: center; align-items: center; min-height: 100vh; } .fail-box { background: white; padding: 40px 60px; border-radius: 12px; box-shadow: 0 4px 20px rgba(255, 77, 79, 0.2); text-align: center; } .fail-icon { font-size: 60px; color: #ff4d4f; margin-bottom: 20px; } h1 { color: #262626; font-size: 28px; margin-bottom: 10px; } p { color: #595959; font-size: 16px; margin-bottom: 30px; } .btn { display: inline-block; padding: 10px 30px; background-color: #ff4d4f; color: white; border-radius: 6px; text-decoration: none; transition: background 0.3s; } .btn:hover { background-color: #ff7875; } /style /head body div classfail-box div classfail-icon✕/div h1登录失败/h1 p用户名或密码错误请重新输入/p a hreflogin.html classbtn重新登录/a /div /body /html4. 运行效果展示登录页面访问http://localhost:8080/项目名/login.html显示带样式的登录表单可输入用户名和密码成功场景输入root/root点击登录地址栏跳转为success.html显示绿色成功提示失败场景输入错误账号如admin/123或空值地址栏跳转为fail.html显示红色失败提示控制台打印错误日志。sendRedirect标红的核心原因sendRedirect方法会抛出IOException受检异常而你的doGet方法没有声明抛出该异常所以 IDE 会标红报错。解决方法在方法上声明抛出异常在doGet方法后添加throws IOExceptionOverrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException { // 加上这句// 你的业务代码...resp.sendRedirect(success.html);}

相关新闻