Servlet
1. ServletConfig对象
ServletConfig是javax.servlet.包下的一个接口,ServletConfig它是Servlet的一个配置对象;
ServletConfig是由tomcat容器创建,通过init方法传入给Servlet;
作用:
- 获取Servlet的名称servlet-name标签内容,getServletName
- 获取Servlet的初始化参数,getInitParameter getInitParameterNames
- 获取ServletContext域对象
- getInitParameter(String parameterName):根据参数名称获取指定的参数值
- getInitParameterNames():获取所有的参数名称
<servlet>
<servlet-name>TestServlet11</servlet-name>
<servlet-class>com.kaka.test_01.servlet.TestServlet11</servlet-class>
<!--Servlet的初始化参数-->
<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>root123</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet11</servlet-name>
<url-pattern>/demo11</url-pattern>
</servlet-mapping>
public class TestServlet11 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取ServletConfig对象
ServletConfig servletConfig = getServletConfig();
//2.获取Servlet的名称 , 获取到<servlet-name>标签内容
String servletName = servletConfig.getServletName();
System.out.println(servletName+"在运行------------");
//3.获取Servlet中的初始化参数
String username = servletConfig.getInitParameter("username");
System.out.println(username);
String password = servletConfig.getInitParameter("password");
System.out.println(password);
//动态获取初始化参数名称
Enumeration<String> parameterNames = servletConfig.getInitParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
String parameterValue = servletConfig.getInitParameter(parameterName);
System.out.println("name : " + parameterName + " , value : " + parameterValue);
}
//4.获取ServletContext对象(域对象)
ServletContext servletContext = servletConfig.getServletContext();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
通过Servlet接口下的getServletConfig方法获取一个ServletConfig对象
2. ServletContext对象
ServletContext是javax.servlet包下的一个接口,又称上下文对象,ServletConfig对象中维护了ServletContext对象,我们可以通过ServletConfig对象来获取ServletContext对象;当服务器启动时,会为服务器中的每一个web应用程序创建一个ServletContext对象,一个ServletContext对象对应的就是一个web应用程序;在web应用中的servlet要想实现资源的共享,可以通过ServletContext来完成,ServletContext也叫做域对象;
2.1 用来存储数据
ServletContext是一个域对象,可以用来存储数据;
ServletContext域对象作用范围:在应用程序中的任何位置都能够访问;
常用方法:
- getAttribute(String parameterName) : 获取ServletContext域中指定名称的参数值;
- setAttribute(String paramterName,Object parameterValue):存储参数到ServletContext域中;
- removeAttribute(String parameterNam):将ServletContext域中指定名称的参数移除;
2.1.1 存储参数到ServletContext域中
public class TestServlet12 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = "hello";
//将msg变量存储到ServletContext域中
//获取ServletConfig对象
ServletConfig servletConfig = getServletConfig();
//获取ServletContext对象
ServletContext servletContext = servletConfig.getServletContext();
//往ServletContext域中,存储一个名称为msg的参数,值为"hello"
servletContext.setAttribute("msg",msg);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.1.2 获取ServletContext域中指定名称的参数值
public class TestServlet13 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从ServletContext域中取出msg的值
ServletContext servletContext = getServletContext();
Object msg = servletContext.getAttribute("msg");
System.out.println(msg);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.1.3 将ServletContext域中指定名称的参数移除
public class TestServlet14 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
//将ServletContext域中的msg参数移除
servletContext.removeAttribute("msg");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.2 获取全局初始化参数
会随着服务器的启动而初始化,SpringMVC框架中会有应用,在web.xml中配置的全局初始化参数,可以通过ServletContext对象获取;
- 全局初始化参数:整个应用程序,当服务器启动时,就可以有一个变量可以用,就用全局初始化参数 ;
- Servlet的初始化参数:Servlet初始化时所用携带的变量,当使用一个Servlet时,需要在Servlet初始化就有一个初始化参数可以使用;
常用方法:
- getInitParameter():获取指定参数名称的全局参数值
- getInitParameterNames():获取所有的全局初始化参数名称
<context-param>
<param-name>username</param-name>
<param-value>root456</param-value>
</context-param>
<servlet>
<servlet-name>TestServlet15</servlet-name>
<servlet-class>com.kaka.test_01.servlet.TestServlet15</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet15</servlet-name>
<url-pattern>/demo15</url-pattern>
</servlet-mapping>
public class TestServlet15 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取全局初始化参数
ServletContext servletContext = getServletContext();
//动态获取参数名称
Enumeration<String> parameterNames = servletContext.getInitParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
String parameterValue = servletContext.getInitParameter(parameterName);
System.out.println("name : " + parameterName + " , value : " + parameterValue);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.3 获取服务器真实磁盘路径
getRealPath:ServletContext类,依据当前项目去生成真实磁盘路径;
- 如:servletContext.getRealPath("upload"):"当前项目的服务器磁盘路径/upload"
- 如:servletContext.getRealPath("upload/img"):"当前项目的服务器磁盘路径/upload/img"
public class TestServlet16 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取upload的服务器真实路径
ServletContext servletContext = getServletContext();
String upload = servletContext.getRealPath("upload");
System.out.println(upload);
//获取img文件的服务器真实路径
String imgPath = servletContext.getRealPath("upload/img");
System.out.println(Path);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
9. 站点访问次数案例(ServletContext对象)
- 使用ServletContext域对象
步骤:
- 判断是否是第一次请求
- 如果是第一次请求
- 定义一个变量count=1
- 打印count变量
- 将count变量存储ServletContext域中
- 如果不是第一次请求
- 将count自加
- 打印count变量
- 将count变量再存储ServletContext域中
/**
* 统计站点访问次数
*/
public class TestServlet17 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取count变量
ServletContext servletContext = getServletContext();
//在域中拿count
Integer count = (Integer)servletContext.getAttribute("count");
if (null == count) {
//第一次请求,存储一个count=1变量到ServletContext中
count = 1;
System.out.println("站点访问次数 : "+count);
//将count变量存储到ServletContext域中
servletContext.setAttribute("count",count);
} else {
//当不是第一次请求
count++;
System.out.println("站点访问次数 : "+count);
//再将count变量存储到ServletContext域中
servletContext.setAttribute("count",count);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
10. Servlet 3.0
概念
Servlet2.5,不支持注解开发;
Servlet3.0,支持注解开发;(由注解配置来代替web.xml配置)
常用注解
@Test:单元测试
@Override:方法重写
@WebServlet:声明配置Servlet,取代了web.xml配置
@WebServlet(name = "TestServlet18",urlPatterns = "/demo18") public class Demo18Servlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Demo18Servlet处理请求"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
以上配置代码,相当于web.xml配置如下:
<servlet> <servlet-name>TestServlet18</servlet-name> <servlet-class>com.kaka.test_01.servlet.TestServlet18</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet18</servlet-name> <url-pattern>/demo18</url-pattern> </servlet-mapping>
常用属性:
name:String
- 设置Servlet名称
urlPatterns:String[]
- 设置Servlet的访问路径
loadOnStartup:int
- 设置Servlet的load-on-startup属性(随着服务器的启动而启动)
initParams:WebInitParam[]
- 设置Servlet的初始化参数
@WebServlet(name = "TestServlet01", urlPatterns = {
"/demo01","/mydemo01"},loadOnStartup = 1,
initParams = {
@WebInitParam(name = "username",value = "root"),@WebInitParam(name = "password",value = "root")})
public class TestServlet01 extends HttpServlet {
@Override
//用来监听
public void init(ServletConfig config) throws ServletException {
System.out.println("TestServlet01 init");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("TestServlet01");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}