ServletContext对象的使用

ServletContext:Servlet上下文,代表当前整个应用程序。在jsp中就是application。

一、概述

ServletContext:Servlet上下文。

当WEB服务器启动时,会为每一个WEB应用程序(webapps下的每个目录就是一个应用程序)创建一块共享的存储区域
ServletContext也叫做“公共区域”,也就是同一个WEB应用程序中,所有的Servlet和JSP都可以共享同一个区域。
ServletContext在WEB服务器启动时创建,服务器关闭时销毁。

二、相关方法的使用

@WebServlet(name = "ApplicationServlet", value = "/as")
public class ApplicationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取servletcontext(application对象)
        //方式1【推荐】
        ServletContext application1 = this.getServletContext();
        //方式2
        ServletContext application2 = request.getServletContext();
        //方式3
        ServletContext application3 = this.getServletConfig().getServletContext();
        //方式4
        ServletContext application4 = request.getSession().getServletContext();

        //共享区域,可以保存共享信息
        application1.setAttribute("appname", "个人网站");
        application1.setAttribute("version", "2.0");
        application1.setAttribute("author", "张三");

        //在其他servlet都可以读到
        Object appname = application1.getAttribute("appname");
        System.out.println(appname);

        //获取真实路径,Out文件夹下的真实目录【重要】
        String realPath = application1.getRealPath("/");
        //System.out.println(realPath);

        //获取其他信息
        //服务器信息
        String serverInfo = application1.getServerInfo();
        
        //获取上下文路径【重点,应用程序名称】
        String contextPath = application1.getContextPath();
        //打印结果:/0902web1,这是固定的位置,可以用作动态的跟路径
        System.out.println(contextPath);
        //用于重定向,两种书写方式,获取该路径
        //注意该路径是/0902web1,只有重定向在其后加上"/islogin"可以访问,但是转发的"/"包含了一次"/0902web1"不能直接追加
        //request.getRequestDispatcher("/islogin").forward(request, response);
        response.sendRedirect(this.getServletContext().getContextPath() + "/islogin");
        response.sendRedirect(request.getContextPath() + "/islogin");

        //通过xml配置servlet上下文参数,共享数据,读取
        System.out.println(application1.getInitParameter("appname1"));
        System.out.println(application1.getInitParameter("version1"));
    }

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

在xml文件中配置servlet上下文参数

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!--配置servlet上下文参数-->
    <context-param>
        <param-name>appname1</param-name>
        <param-value>购物网站</param-value>
    </context-param>
    <context-param>
        <param-name>version1</param-name>
        <param-value>3.0</param-value>
    </context-param>
</web-app>

三、案例——ServletContext统计Servlet访问次数

@WebServlet(name = "CountServlet", value = "/countservlet")
public class CountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        ServletContext application = this.getServletContext();
        Integer count;
        //上锁,处理并发
        synchronized (this) {
            count = (Integer) application.getAttribute("count");
            if (count == null) {
                count = 1; //第一次赋值
            } else {
                count++;
            }
            application.setAttribute("count", count);
        }
        System.out.println("访问次数:" + count);
        response.getWriter().println("<h2>当前访问次数:</h2>" + count);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值