日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在Servlet中实现页面转发

發布時間:2025/3/20 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Servlet中实现页面转发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Servlet中實現頁面轉發,使用的是RequestDispatcher對象的forward()方法,可以在Servlet中通過forward()方法將當前的請求轉發到其他Web組件


index.jsp頁面----<form>中的action屬性值為Servlet類在web.xml中配置的URL,提交方式為post

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="forward" 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>

新建ForwardServlet的Servlet類,在doPost()方法中獲得表單請求信息

public class ForwardServlet extends HttpServlet{public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{this.doPost(request, response);}public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{request.setCharacterEncoding("utf-8"); //設置請求的字符編碼格式String name=request.getParameter("name");String pwd=request.getParameter("pwd");if((name!=null&&!name.equals(""))&&(pwd!=null&&!pwd.equals(""))){if(name.equals("zj")&&pwd.equals("123")){//使用RequestDispatcher對象將頁面請求轉發到success.jsp頁面request.getRequestDispatcher("success.jsp").forward(request, response);}else{request.getRequestDispatcher("error.jsp").forward(request, response);}}} }

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>

在Servlet的配置文件web.xml中配置RedirectServlet類

<?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" version="3.1"><display-name>ServletTest</display-name><servlet><servlet-name>ForwardServlet</servlet-name><servlet-class>com.cn.zj.Servlet.ForwardServlet</servlet-class></servlet><servlet-mapping><servlet-name>ForwardServlet</servlet-name><url-pattern>/forward</url-pattern></servlet-mapping></web-app>

總結

以上是生活随笔為你收集整理的在Servlet中实现页面转发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。