ServletContext对象的使用!!!

1、是什么
         tomcat启动时会为web应用创建一个servletContext对象,是Servlet的一个全局配置对象&域对象

2、配置对象怎么用:web.xml

        
     username
     root
ServletContext servletContext = getServletContext();
String username = servletContext.getInitParameter("username");

3、域对象怎么用

servletContext.setAttribute("msg", "sb");
Object msg = servletContext.getAttribute("msg");

4.主要方法:

getInitParameter() //获取指定参数名称的全局参数值
getRealPath(String path) //获得当前项目的服务器磁盘路径
getContextPath() //获取项目的根路径
getAttribute(String parameterName) //获取ServletContext域中指定名称的参数值;
setAttribute(String paramterName,Object parameterValue) //存储参数到ServletContext域中;
removeAttribute(String parameterNam) //将ServletContext域中指定名称的参数移除;

代码演示:

ServletContextDome:

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by.servlet;

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

/**
 * 

Project: JavaWeb-Servlet - ServeltContextDome

*

Powered by scl On 2023-12-26 17:10:19

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class ServletContextDome extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); ServletContext servletContext = getServletContext(); //匹配全局属性值,也就是在web.xml文件中配置的数据 String username = servletContext.getInitParameter("username"); String password = servletContext.getInitParameter("password"); System.out.println(username+password); //通过setAttribute()方法添加新的属性 String str="hello"; servletContext.setAttribute("aaa", str); //获取新的属性 System.out.println(servletContext.getAttribute("aaa")); } }

 ServletContextDome2:

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by.servlet;

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

/**
 * 

Project: JavaWeb-Servlet - ServeltContextDome

*

Powered by scl On 2023-12-26 17:10:19

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class ServletContextDome2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); ServletContext servletContext = getServletContext(); //匹配全局属性值,也就是在web.xml文件中配置的数据 String username = servletContext.getInitParameter("username"); String password = servletContext.getInitParameter("password"); System.out.println("第二次匹配:"+username+password); //获取第一个Dome文件中通过setAttribute()方法添加新的属性 System.out.println(servletContext.getAttribute("aaa")); } }

web.xml:


        username
        刘亦菲
    
    
        password
        521
    
    
        ServletContextDome
        com.by.servlet.ServletContextDome
    
    
        ServletContextDome
        /servletContextDome
    
    
        ServletContextDome2
        com.by.servlet.ServletContextDome2
    
    
        ServletContextDome2
        /servletContextDome2
    

你可能感兴趣的:(servlet,maven,intellij-idea)