当前位置:网站首页>ServletContext learning diary 1
ServletContext learning diary 1
2022-07-02 23:02:00 【herb. dr】
Catalog
1.2 obtain ServletContext object
1.3.1 Get the real path of the project
1.3.2 Get the project context path
1.4 ServletContext characteristic
1.5 ServletContext Application scenarios
One 、ServletContext object
1.1 ServletContext summary
Global object , Also has scope , Corresponding to one Tomcat Medium Web application
When Web When the server starts , For every individual Web Application creation one Block shared storage area (ServletContext) .
ServletContext stay Web Create... When the server starts , Destroy on server shutdown .
1.2 obtain ServletContext object
GenericServlet Provides getServletContext() Method .( recommend ) this.getServletContext();
HttpServletRequest Provides getServletContext() Method .( recommend )
HttpSession Provides getServletContext() Method .

package com.ha.controller;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class ServletContextController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. adopt this.getServletContext();
ServletContext servletContext = this.getServletContext();
//2. adopt request Object acquisition
ServletContext servletContext1 = request.getServletContext();
//3. adopt session Object acquisition
HttpSession session = request.getSession();
ServletContext servletContext2 = session.getServletContext();
System.out.println(servletContext);
System.out.println(servletContext1);
System.out.println(servletContext2);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

<?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"
metadata-complete="true">
<servlet>
<servlet-name>ctx</servlet-name>
<servlet-class>com.ha.controller.ServletContextController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ctx</servlet-name>
<url-pattern>/ctx</url-pattern>
</servlet-mapping>
</web-app>

1.3 ServletContext effect
1.3.1 Get the real path of the project
Get the real path of the current project published on the server
String realpath=servletContext.getRealPath("/");
1.3.2 Get the project context path
Gets the current project context path ( Application name )
System.out.println(servletContext.getContextPath()); // Context path ( Application name )
System.out.println(request.getContextPath());


1.3.3 Global container
ServletContext Have scope , Data can be stored in a global container
Store the data : servletContext.setAttribute("name",value);
get data : servletContext.getAttribute("name");
Remove data : servletContext.removeAttribute("name");


1.4 ServletContext characteristic
Uniqueness : One application corresponds to one individual ServletContext.
Life cycle : As long as the container is not closed or the application is not uninstalled ,ServletContext Just one Direct existence .
1.5 ServletContext Application scenarios
ServletContext Count the number of visits to the current project
package com.qf.counter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "CounterController",value = "/counterController")
public class CounterController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. obtain ServletContext object
ServletContext servletContext = request.getServletContext();
//2. Get counter
Integer counter = (Integer) servletContext.getAttribute("counter");
if(counter == null){
counter = 1;
servletContext.setAttribute("counter",counter);
}else{
counter++;
servletContext.setAttribute("counter",counter);
}
System.out.println("counter:"+counter);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
边栏推荐
- 最小生成树 Minimum Spanning Tree
- MySQL查询附近的数据.并按距离进行排序.
- [leetcode] there are duplicate elements [217]
- 【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
- Introduction and response to high concurrency
- 海思3559万能平台搭建:在截获的YUV图像上旋转操作
- The kth largest element in the [leetcode] array [215]
- Go语言sqlx库操作SQLite3数据库增删改查
- AES高級加密協議的動機闡述
- [LeetCode] 反转字符串【344】
猜你喜欢

【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
![[羊城杯2020]easyphp](/img/12/da28f738e50e625b0a66a94af3703d.png)
[羊城杯2020]easyphp

MySQL reset password, forget password, reset root password, reset MySQL password
![[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM](/img/5d/c23ec16df6ce8d78207b635f59dc20.png)
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM

世界环境日 | 周大福用心服务推动减碳环保

地方经销商玩转社区团购模式,百万运营分享

手写ORM(对象关系映射)增删改查

最小生成树 Minimum Spanning Tree

Successfully changed Splunk default URL root path
![[Solved] Splunk: Cannot get username when all users are selected“](/img/13/1e824c8005701e21fc5b4e73308d53.png)
[Solved] Splunk: Cannot get username when all users are selected“
随机推荐
[LeetCode] 回文数【9】
[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
E-commerce system microservice architecture
编辑卡顿
数据分析学习记录(二)---响应曲面法及Design-Expert的简单使用
Rails 3 activerecord: sort by association count - rails 3 activerecord: order by count on Association
Learning records of data analysis (II) -- simple use of response surface method and design expert
xshell配置xforward转发火狐浏览器
[LeetCode] 存在重复元素【217】
从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
全面解析分享购商业模式逻辑?分享购是如何赋能企业
Jerry's charge unplugged, unable to touch the boot [chapter]
地方经销商玩转社区团购模式,百万运营分享
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
LeetCode 968. Monitor binary tree
悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
Performance optimization - rigorous mode
P1007 single log bridge
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!