This article mainly introduces the introduction of ServletContext of servlet. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
In the web.xml file that configures the Servlet, some initialization parameters are often used to configure the Servlet. The general function is not to configure the Servlet in the Servlet program. A certain variable is hard-coded and passed through the outside world (such as the web.xml file), making it easy to modify. This uses the
You can see that there are two initialization parameters in this default Servlet, namely "debug=0" and "listings=false".
When the Servlet configures the
From the initialization method of the Servlet interface: init(ServletConfig config), we can know that when the server creates a Servlet object, it passes the ServletConfig object, and the ServletConfig object contains
When I first started learning Servlet, I already mentioned that one of the non-life cycle methods of the Servlet interface is the getServletConfig() method, which returns a ServletConfig object. So when we configure some information in the web.xml file of the developed Servlet:
And the program in the Servlet obtains the parameters of this configuration:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletConfig config = this.getServletConfig(); String initValue = config.getInitParameter("love"); System.out.println(initValue); }
Re-deploy the web application, and then access the Servlet in the browser, you will see the following printed on the MyEclipse console:
In the ServletConfig class, the getInitParameter(String name) method is to pass in a specific parameter name to obtain the value of the corresponding parameter, and the getInitParameterNames() method is to load all the parameter names into an Enumeration object and return it , when we have multiple parameter key-value pairs:
Traverse and output in Servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletConfig config = this.getServletConfig(); Enumeration initParams = config.getInitParameterNames(); while(initParams.hasMoreElements()) { String paramName = (String)initParams.nextElement(); String paramValue = config.getInitParameter(paramName); System.out.println(paramName+" = "+paramValue ); } }
Finally, the role of the ServletConfig object is usually used to obtain the encoding table type, obtain database connection information, obtain the configuration file (such as the web.xml file of Struts), etc.
After talking about the ServletConfig object, when we look at the methods of this object, we will find that there is another method in this method, getServletContext(), which returns a ServletContext object. This is a very special feature in Servlet. Important classes. Of course, the ServletContext object can also be obtained directly from the parent class's method.
The Web container will create a ServletContext object for each web application when it is started, and this ServletContext object represents the current web application. Because a ServletContext object represents a web application, all Servlets and other resources in the web application share a ServletContext object. At this time, we can communicate between Servlet objects through the ServletContext object. The ServletContext object is also called the Context domain object.
Let’s first look at the two ways to obtain the ServletContext object:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //两种获取ServletContext对象的方法: ServletContext context1 = this.getServletConfig().getServletContext(); ServletContext context2 = this.getServletContext(); //System.out.println(context1 == context2); //ture }
It can be obtained by first obtaining the ServletConfig object, or directly Obtained through the method of the parent class, the two methods obtain the same object (same address).
Since ServletContext represents this web application, we can use it for direct communication between Servlets, then we will create a project to transmit data between two Servlets. Create two Servlets under a [myservlet] web project: ServletDemo1 and ServletDemo2.
ServletDemo1 sets the parameter key-value pair in the ServletContext. The code is:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); context.setAttribute("lover", "LRR"); }
ServletDemo2 gets the key-value pair from ServletContext, the code is:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); System.out.println(context.getAttribute("lover")); }
在浏览器先访问ServletDemo1后(先执行ServletDemo1才能使ServletContext设置参数),再访问ServletDemo2后,MyEclipse的控制台就输出了ServletContext中设置的参数,这就达到了从一个Servlet传递数据给另一个Servlet。当然这只是ServletContext的一个小小应用。
在ServletContext类中还有getInitParameter(String name)方法或者getInitParameterNames()方法,这两个方法获取的是web应用所配置的参数(毕竟ServletContext代表web应用),就像ServletConfig中类似的方法获取的是某个Servlet中的
而对于配置web应用的参数是在web.xml文件中使用
例:在【myservlet】web工程下建立了名为ServletDemo3的Servlet,并在该web工程下的web.xml文件中添加
在ServletDemo3中的代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); String username = context.getInitParameter("username"); String password = context.getInitParameter("password"); System.out.println(username +":"+ password); }
在浏览器中访问该Servlet,如果MyEclipse的控制台能打印该信息,说明每个Servlet可以通过ServletContext对象来获取web应用的配置信息,也从侧面说明了ServletContext代表了这个web应用:
ServletContext类中的getMimeType(String file)方法用于返回该文件的MIME类型:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = "1.html"; ServletContext context = this.getServletContext(); System.out.println(context.getMimeType(filename)); }
输出:text/html。
ServletContext中的转发方法(重要)
在ServletContext对象中还有这么两个方法:getNameDispatcher(String name)(不常用)和getRequestDispatcher(String path),返回的是RequestDispatcher对象。转发有什么作用呢,举个例子,比如一个Servlet中的数据交个另一个Servlet来处理,或者Servlet将某个实时数据交给JSP来显示,虽然我们在浏览器中访问的是最开始的Servlet,但是进行转发后看到的其他web资源,而浏览器的地址栏不会改变。
注:在请求对象request对象中也有这么一个getRequestDispatcher(String path)方法,功能与ServletContext对象的这个方法一样,也可以实现转发,因此用哪个对象都行,没有区别。
例:在【myservlet】web工程下创建一个名为ServletDemo1的Servlet和一个show.jsp,
在ServletDemo1中将数据转发给show.jsp,代码为:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "Ding love LRR"; this.getServletContext().setAttribute("data", data); //将数据存至web应用的配置中 ServletContext context = this.getServletContext(); RequestDispatcher dispathcer = context.getRequestDispatcher("/show.jsp"); //通过要转发的目的地来获取转发对象 dispathcer.forward(request, response); //通过forward方法将请求对象和响应对象转发给别人处理 }
而在show.jsp中接收这个数据,并封装在HTML中:
<font size="100px" color="red"> ${data } </font>
接着我们去浏览器里访问ServletDemo1,就会看到:
虽然我们请求的ServletDemo1资源,但是由于在ServletDemo1中将请求进行了转发,所以其实服务器返回的是show.jsp的资源,但是我们浏览器地址依然会是ServletDemo1,这也是转发和重定向的区别之一。
The above is the detailed content of A detailed introduction to ServletContext of servlets in Java. For more information, please follow other related articles on the PHP Chinese website!