Servlet学习总结(二)

学习自孤傲苍狼的博客园

  • ServletConfig
  • ServletContext

ServletConfig的学习



ServletConfig的作用是让Servlet获得初始化的数据,在servlet的web.xml中可以通过<init-param>来为servlet配置初始化参数.

<servlet>
    <servlet-name>asda</servlet-name>
    <servlet-class>servleta.asda</servlet-class>
    <init-param>
        <param-name>name</param-name>
        <param-value>aaa</param-value>
    </init-param>
    <init-param>
        <param-name>pass</param-name>
        <param-value>aaaa</param-value>
    </init-param>
        <init-param>
        <param-name>acc</param-name>
        <param-value>aaaaaaa</param-value>
    </init-param>
</servlet>

当servlet配置好之后,创建一个servlet时会自动将这些数据初始化封装到ServletConfig中,调用
init方法时,数据会传给init的config参数中,通过ServletConfig的对象就可以取得数据.

@WebServlet("/asda")
public class asda extends HttpServlet {
    private static final long serialVersionUID = 1L;
    //通过ServletConfig接收参数
    private ServletConfig config;
    //init-param配置的数据会通过init方法中的config参数来获取
    @Override
    public void init(ServletConfig config) throws ServletException {
        this.config=config;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取指定的一个数据
        String paramVal=this.config.getInitParameter("name");
        response.getWriter().print(paramVal);
        response.getWriter().print("</hr>");
        //获取所有数据
        Enumeration<String> e=config.getInitParameterNames();
        while(e.hasMoreElements()) {
            String name=e.nextElement();
            String value=config.getInitParameter(name);
            response.getWriter().println(name+"="+value);
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}


运行结果:

aaa

ccc=aaaaaaa

pass=aaaa

name=aaa

ServletContext的学习

ServletContext可以看做成一个被所有Servlet共享的全局容器,你可以通过Servletonfig.getServletContext获取ServletContext对象,来实现servlet之间的数据共享.
ServletContext的生命周期是整个web工程

实例:两个servlet通过ServletContext实现数据共享

public class ServletContextTest extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String data="傻狗!";
        //获取context实例
        ServletContext context=this.getServletConfig().getServletContext();
        //设置值到context中
        context.setAttribute("data", data);
        response.getWriter().println("aaa");

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
public class ServletContextTest2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //给获取的信息设置编码格式,这样就不会出现都是???的情况
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
                //获取context的实例
        ServletContext context=this.getServletContext();
        //获取“data”的数据
        String data=(String) context.getAttribute("data");
        response.getWriter().print(data);
        response.getWriter().println("aaa");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}


web工程都是在tomcat下运行的,他们生命周期的运作都是基于开启同一个服务所产生的,所以运行第一个后关闭再重新开启运行第二个,tomcat里就不再存有第一个的值,第二个就会报null.

使用init方法必须重写写出父类方法super.init(config);去获取config,不然就会报空指针.

最后别忘了在web.xml下添加servlet的路径.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,372评论 11 349
  • Servlet学习的大纲 servlet概念及相关接口简介 servet 执行过程 servlet映射路径 缺省s...
    奋斗的老王阅读 1,213评论 1 51
  • 在学习Servlet是找到一篇不错的文章,转载一下。学习心得,Servlet其实只是个接口,相当于是定义了一个标准...
    君子若莲阅读 1,211评论 1 16
  • 本文包括: Servlet简介关于Servlet的一些类 Servlet生命周期 ServletConfig获得初...
    廖少少阅读 3,906评论 1 67
  • 表达着不可换位的主客体之间各自存在度的差异,由此造成的二元耦合关系的错动才是“本质”的渊源或“本质的本质”
    0f5b60a713a1阅读 611评论 0 0