package cn.lijun.request;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author lijun
* @date 2019/7/9 15:52
*/
@WebServlet(urlPatterns = "/servlet1")
public class Servlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1 向请求域中存数据
request.setAttribute("9502","我爱java");
//2 转发 获取转发器 参数是地址
RequestDispatcher dis = request.getRequestDispatcher("/servlet2");
//转发器有方法 forward
dis.forward(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}


}

 

 

 

package cn.lijun.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author lijun
* @date 2019/7/9 15:55
*/
@WebServlet(urlPatterns = "/servlet2")
public class Servlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("servlet2 "+request.getAttribute("9502"));
response.getWriter().write("servlet2");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}


}