application作用范围是一次应用程序范围有效
1、客户端重定向
class1
import javax.servlet.ServletContext;
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;
//演示application保存作用域(demo05和demo06)
@WebServlet("/demo05")/*这一语句写这里,web.xml则不需要要配置,否则要配置web.xml文件*/
public class Demo05Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向application保存作用域保存数据
//ServletContext : Servlet上下文
ServletContext application = request.getServletContext();
application.setAttribute("application","application客户端重定向");
//2.客户端重定向
response.sendRedirect("demo06");
}
}
class2
import javax.servlet.ServletContext;
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;
@WebServlet("/demo06")/*这一语句写这里,web.xml则不需要要配置,否则要配置web.xml文件*/
public class Demo06Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取application保存作用域保存的数据,key为uname
ServletContext application = request.getServletContext() ;
Object unameObj = application.getAttribute("application");
System.out.println("unameObj = " + unameObj);
}
}
2、服务器内部转发
class1
import javax.servlet.ServletContext;
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;
//演示application保存作用域(demo05和demo06)
@WebServlet("/demo05")/*这一语句写这里,web.xml则不需要要配置,否则要配置web.xml文件*/
public class Demo05Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向application保存作用域保存数据
//ServletContext : Servlet上下文
ServletContext application = request.getServletContext();
application.setAttribute("application","application服务器内部转发");
//2.服务器端转发
request.getRequestDispatcher("demo06").forward(request,response);
}
}
class2
import javax.servlet.ServletContext;
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;
@WebServlet("/demo06")/*这一语句写这里,web.xml则不需要要配置,否则要配置web.xml文件*/
public class Demo06Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取application保存作用域保存的数据,key为uname
ServletContext application = request.getServletContext() ;
Object unameObj = application.getAttribute("application");
System.out.println("unameObj = " + unameObj);
}
}
如果要配置xml文件,查看服务器端转发和客户端转发_qq_46053741的博客-CSDN博客
配置方法类似