美文网首页
Servlet学习总结(二)

Servlet学习总结(二)

作者: 羽寂 | 来源:发表于2018-08-12 22:39 被阅读0次

    学习自孤傲苍狼的博客园

    • 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的路径.

    相关文章

      网友评论

          本文标题:Servlet学习总结(二)

          本文链接:https://www.haomeiwen.com/subject/vvdmvftx.html