在Servlet中实现页面转发
生活随笔
收集整理的這篇文章主要介紹了
在Servlet中实现页面转发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Servlet中實現頁面轉發,使用的是RequestDispatcher對象的forward()方法,可以在Servlet中通過forward()方法將當前的請求轉發到其他Web組件
index.jsp頁面----<form>中的action屬性值為Servlet類在web.xml中配置的URL,提交方式為post
新建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中实现页面转发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 打开指定大小的新窗口
- 下一篇: 在Servlet中实现页面重定向