浏览器地址栏不变
转发是服务器内部行为 浏览器不知道 转发不需要写web应用名
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);
}
}
重定向:浏览器两次请求
浏览器地址栏变
重定向可以到外网去
重定向需要写web应用名
package cn.kgc.send;
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/8 16:08
*/
@WebServlet(urlPatterns = "/servlet1")
public class Servlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 接受浏览器的请求 对;浏览器做出响应 重定向 302
// 注意 重定向后 不要再写代码了 没有意义 不能多次重定向
// response.setStatus(302);
// response.setHeader("location","/day04_web/servlet2");
// sun公司提供了简化的方法
response.sendRedirect("/day04_web/servlet2");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package cn.kgc.send;
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/8 16:11
*/
@WebServlet(urlPatterns = "/servlet2")
public class Servlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("我是servlet2");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}