在Servlet中实现页面重定向
生活随笔
收集整理的這篇文章主要介紹了
在Servlet中实现页面重定向
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現頁面重定向主要應用HTTPServletResponse對象的sendRedirect()方法,與頁面轉發的forward()不同,使用forward()方法時,會將當前正在處理的請求轉發到其他web組件(Servlet,JSP,HTML)。而sendRedirect()方法不會轉發請求,只是頁面跳轉
新建RedirectServlet的Servlet類,繼承HTTPServlet類,在該類的doPost()方法中判斷用戶名和密碼是否正確,不正確會使用sendRedirect()方法將頁面重定向到錯誤頁
index.jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>實現頁面重定向</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">table{font-size:12px;font-family: 隸書;color:gray;border: 1px green solid;}input{font-size:12px;font-family: 隸書;color:gray;}</style></head><body><form action="redirect" method="post"><table align="center"><tr><td>用戶名:</td><td><input type="text" name="name" /></td></tr><tr><td>密碼:</td><td><input type="password" name="pwd" /></td></tr><tr><td colspan="2"><input type="submit" value="登 錄" /></td></tr></table></form></body> </html>success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'success.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">table{font-size:12px;font-family: 隸書;color:gray;border: 1px green solid;}input{font-size:12px;font-family: 隸書;color:gray;}</style></head><body><table align="center"><tr><td><font color="green">恭喜您【<%=request.getParameter("name")%>】,登錄成功!</font></td></tr></table></body> </html>error.jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'error.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><table align="center"><tr><td><font color="red">登錄失敗!</font></td></tr></table></body> </html>web.xml配置
<servlet><servlet-name>RedirectServlet</servlet-name><servlet-class>com.cn.zj.Servlet.RedirectServlet</servlet-class></servlet><servlet-mapping><servlet-name>RedirectServlet</servlet-name><url-pattern>/redirect</url-pattern></servlet-mapping>HTTPServletResponse對象的sendRedirect()方法一律返回狀態代碼為302的響應結果,瀏覽器接收到這種響應結果后,立即自動請求訪問重定向的目標web組件,客戶端最后接受到的是目標web組件的響應結果
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的在Servlet中实现页面重定向的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Servlet中实现页面转发
- 下一篇: 在Servlet中处理表单提交的数据