0
点赞
收藏
分享

微信扫一扫

Day02JavaWeb【Servlet & ServletContext】 ServletContext-作用


ServletContext-作用1 ***

  • (1)什么是ServletContext
    1:ServletContext是Servlet中自带一个对象
    2:ServletContext对象时在第一次访问Servlet时,自动创建
    3:一个项目中只有一个ServletContext对象

(2) 作用1:ServletContext可以实现多个Servlet之间数据的共享

getServletContext()
setAttribute(key,value)
getAttribute(key)

Day02JavaWeb【Servlet & ServletContext】 ServletContext-作用_servletcontext


Servlet1可以往ServletContext添加键值对数,所有的Servlet都可以取

src\com\wzx\Demo01SetServlet.java

@WebServlet("/set")
public class Demo01SetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:获取ServletContext
ServletContext context = super.getServletContext();
//2:添加数据 key-value 集合 Map
context.setAttribute("name","jack");
context.setAttribute("age",22);

System.out.println("setServlet");

}
}

src\com\wzx\Demo02GetServlet.java

@WebServlet("/get")
public class Demo02GetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

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

//1.获取servletContext
ServletContext context = getServletContext();
//2.根据键获取值
String name = (String) context.getAttribute("name");
Integer age = (Integer) context.getAttribute("age");
System.out.println(name);
System.out.println(age);
}
}

访问测试

http://localhost:8080/web02_servletContext_war_exploded/set
http://localhost:8080/web02_servletContext_war_exploded/get

ServletContext-作用2

2:ServletContext可以读取web项目中文件的内容

  • src下的文件:
    ​​​InputStream is = Demo3FileServlet.class.getClassLoader().getResourceAsStream("a.txt");​
  • web目录下文件
    ​​​InputStream is = getServletContext().getResourceAsStream("a.txt");​

src\com\wzx\Demo03FileServlet.java

@WebServlet("/file")
public class Demo03FileServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:java使用类加载器去读取
//InputStream inputStream = Demo03FileServlet.class.getClassLoader().getResourceAsStream("a.txt");
InputStream inputStream = getServletContext().getResourceAsStream("b.txt");
//2:将字节流转成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = br.readLine();

response.setCharacterEncoding("utf-8");
response.getWriter().println(line);
System.out.println(line);
}
}

访问测试

http://localhost:8080/web02_servletContext_war_exploded/file

ServletContext-作用3

web\WEB-INF\web.xml

3:ServletContext可以读取整个项目的初始化数据

<context-param>
<param-name>ip</param-name>
<param-value>192.168.31.72</param-value>
</context-param>

src\com\wzx\Demo04ParamServlet.java

@WebServlet("/param")
public class Demo04ParamServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:获取ServletContext
ServletContext context = getServletContext();
//2:打印
String value = context.getInitParameter("ip");
System.out.println(value);
}
}

访问测试

http://localhost:8080/web02_servletContext_war_exploded/param

一个网站被多少用户登录访问

Day02JavaWeb【Servlet & ServletContext】 ServletContext-作用_java_02


比如 三个人 通过LoginServlet登录成功,那必须让LoginServlet 对count值三次+1

在前面的登录案例里面修改

(1)初始化count值

src\com\wzx\web\LoginServlet.java

@Override
public void init() throws ServletException {
//Servlet创建出来,ServletContext也会创建出来

//1:获取ServletContext
ServletContext context = getServletContext();
//2:添加一个count变量,默认值为0
context.setAttribute("count",0);
}

(2)每次登录成功对count值+1

src\com\wzx\web\LoginServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:获取参数

String username=request.getParameter("username");
String password=request.getParameter("password");
//2:处理参数
UserService userService = new UserService();

//2.1登录就是拿着账号,密码去数据库判断,存在就返回true,否则就返回false
boolean result = userService.login(username,password);
response.setCharacterEncoding("utf-8");
//2.2根返回结果提示用户
if(result){

//登录成功,对count值+1
//a.获取servletContext
ServletContext context = getServletContext();
//b.获取里面的count
Integer count= (Integer) context.getAttribute("count");
//c. count + 1
count += 1;
//d. 放回ServletContext
context.setAttribute("count",count);
//3:响应给浏览器
response.getWriter().println("登录成功,您是第"+count+"个登录");


}else{
response.getWriter().println("账号或者密码出错");

}



}


举报

相关推荐

0 条评论