首先是客户端跳转
<servlet>
<servlet-name>client</servlet-name>
<servlet-class>org.lxh.servletdemo.ClientRedirectDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>client</servlet-name>
<url-pattern>/ClientRedirectDemo</url-pattern>
</servlet-mapping>
下面是服务器端跳转
<servlet>
<servlet-name>server</servlet-name>
<servlet-class>org.lxh.servletdemo.ServerRedirectDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>server</servlet-name>
<url-pattern>/ServerRedirectDemo</url-pattern>
</servlet-mapping>
package org.lxh.servletdemo;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ClientRedirectDemo extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,java.io.IOException{
req.getSession().setAttribute("name", "牛儿吃草");
req.setAttribute("info", "MLDNJAVA");
resp.sendRedirect("get_info.jsp");
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,java.io.IOException{
this.doGet(req, resp);
}
}
<servlet>
<servlet-name>client</servlet-name>
<servlet-class>org.lxh.servletdemo.ClientRedirectDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>client</servlet-name>
<url-pattern>/ClientRedirectDemo</url-pattern>
</servlet-mapping>
<%@ page language="java" contentType="text/html" pageEncoding="utf8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'use_javabean_demo01.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">
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<h2>session属性:<%=session.getAttribute("name")%></h2>
<h2><%=request.getAttribute("info")%></h2>
</body>
</html>
下面是服务器端跳转
package org.lxh.servletdemo;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ServerRedirectDemo extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,java.io.IOException{
req.getSession().setAttribute("name", "牛儿吃草");
req.setAttribute("info", "MLDNJAVA");
RequestDispatcher rd = req.getRequestDispatcher("get_info.jsp");
rd.forward(req, resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,java.io.IOException{
this.doGet(req, resp);
}
}
<servlet>
<servlet-name>server</servlet-name>
<servlet-class>org.lxh.servletdemo.ServerRedirectDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>server</servlet-name>
<url-pattern>/ServerRedirectDemo</url-pattern>
</servlet-mapping>