servlet客户端跳转+服务器端跳转

首先是客户端跳转

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);        
    }
}






    client
    org.lxh.servletdemo.ClientRedirectDemo


    client
    /ClientRedirectDemo



<%@ page language="java" contentType="text/html" pageEncoding="utf8"%>



    
    My JSP 'use_javabean_demo01.jsp' starting page
    
    
        
    
    
  
 
  
<%
    request.setCharacterEncoding("utf-8");
%>

session属性:<%=session.getAttribute("name")%>

<%=request.getAttribute("info")%>





下面是服务器端跳转

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);        
    }
}




    server
    org.lxh.servletdemo.ServerRedirectDemo


    server
    /ServerRedirectDemo


你可能感兴趣的:(Java)