ServletContext对象

1、ServletContext(应用程序)

得到ServletContext的两种方式:

/**
 * Servlet implementation class servletDemo
 */
@WebServlet("/servletDemo")
public class servletDemo extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servletDemo() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //得到servletcontext的第一种方式
        ServletContext context = this.getServletConfig().getServletContext();
        //第二种方式
        context = this.getServletContext();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

 2、ServletContext域(这是一个容器,说明了应用程序范围)

就比如一个人往一个应用程序里面放东西,如果这个应用程序没有退出,那么其他人也可以访问这个东西,示例如下:

ServletContextDemo1.java

 1 package test;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * Servlet implementation class ServletContextDemo1
12  */
13 @WebServlet("/ServletContextDemo1")  //注意有了这个就不需要往web.xml文件里面添加路径映射
14 public class ServletContextDemo1 extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16 
17     /**
18      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
19      */
20     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21         // TODO Auto-generated method stub
22         this.getServletContext().setAttribute("name", "bbb");
23     }
24 
25     /**
26      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
27      */
28     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29         // TODO Auto-generated method stub
30         doGet(request, response);
31     }
32 
33 }
View Code

 

ServletContextDemo2.java

 1 package test;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * Servlet implementation class ServletContextDemo2
12  */
13 @WebServlet("/ServletContextDemo2")    //注意有了这个就不需要往web.xml文件里面添加路径映射
14 public class ServletContextDemo2 extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16 
17     /**
18      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
19      */
20     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21         // TODO Auto-generated method stub
22         String name = (String) this.getServletContext().getAttribute("name");
23         System.out.println(name);
24     }
25 
26     /**
27      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
28      */
29     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30         // TODO Auto-generated method stub
31         doGet(request, response);
32     }
33 
34 }
View Code

 

先访问ServletContextDemo1后访问ServletContextDemo2控制台就会输出bbb。

中间报了一个错:[X]和 [Y] 的servlet不能映射为一个url模式(url-pattern)

这是因为使用“创建Servlet”向导创建了一个Servlet,url-pattern,然后添加了一个 @WebServlet 注释,它们可以代替到web.xml中的任何内容,但是你又在web.xml文件里面加了文件映射路径。

你应该使用一个或另一个,而不是两者。从 web.xml 中删除​​映射,然后继续使用 @WebServlet 注释。

 

2、通过getInitParameter获得给所有应用程序配置的参数

在web.xml文件里面通过context-param标签就可以给所有应用程序(也就是所有项目)添加参数。

<context-param>
      <param-name>data</param-name>
      <param-value>xxxx</param-value>
  </context-param>
  <context-param>
      <param-name>data1</param-name>
      <param-value>xxxxxxx</param-value>
  </context-param>

Java文件:

package test;

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

/**
 * Servlet implementation class ServletContextDemo2
 */
@WebServlet("/ServletContextDemo2")    //注意有了这个就不需要往web.xml文件里面添加路径映射
public class ServletContextDemo2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String name = this.getServletContext().getInitParameter("data1");
        System.out.println(name);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

3、转发

由于在Java文件里面写html代码太麻烦,例如要向浏览器写入<div color='red'></div>,那么我们还需要转成二进制流,之后再输出

write("<div color='red'></div>".getbytes()),太麻烦了,所以我们转发给jsp处理。

注意:不可以通过上面讲的ServletContext域设置参数的方式把所需数据带给jsp处理,因为应用程序只有一个servlet,其他用户使用servlet的时候可能会修改上一个用户还没来得及邮递给JSP的数据。

 

4、资源文件的访问

资源文件一般有两种,一种是properties文件,主要存放没有关系的数据。另一种是xml文件,用来存放有关系的数据

这一点看一下:在eclipse完成对Java_web项目里面资源文件的读取  博客,我分开写了,要不然篇幅太长。

 

posted @ 2020-12-05 15:10  kongbursi  阅读(128)  评论(0编辑  收藏  举报