我先创建了一个servelt test1
public class test1 extends HttpServlet {
public void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
final String student=request.getParameter("student");
final String teacher=request.getParameter("teacher");
//response.setHeader("Refresh", "1");
this.getServletContext().setAttribute("111",student+teacher);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
然后我又新建了一个servlet test4
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;//引入sevlet
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class test4 extends HttpServlet {
String value;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
value=(String) this.getServletContext().getAttribute("111");//可以写在另一个servlet,也可以是同一个servlet的另一种方法中。
response.getOutputStream().write(value.getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
//response.getOutputStream().write(value.getBytes());
//doGet(request, response);
}
}