一、 Servlet的自动加载
默认情况下,第一次访问servlet的时候,创建servlet对象。如果servlet构造函数里面的代码或者init方法里面的代码比较多,就会导致用户第一次访问servlet的时候比较慢。这个时候,我们可以改变servlet对象的创建时机:提前到加载web应用的时候。在servlet的配置信息中,加上一个<load-on-startup>
标签即可。
java
<servlet>
<servlet-name>loadOnStartup</servlet-name>
<servlet-class>com.by.servlet.LoadOnStartupServlet</servlet-class>
<!--容器是否在启动时加载该servlet,数字越小优先级越高越高-->
<load-on-startup>1</load-on-startup>
</servlet>
servlet实例:
java
public class LoadOnStartupServlet implements HttpServlet {
public LoadOnStartupServlet(){
System.out.println("LoadOnStartupServlet constructor method has run....");
}
}
这样配置之后,servlet的构造函数和init方法就会在web应用加载的时候就会执行。
二、ServletConfig对象
-
是什么
ServletConfig是javax.servlet.包下的一个接口,ServletConfig它是Servlet的一个配置对象;
ServletConfig是由tomcat容器创建,通过init方法传入给Servlet;
-
ServletConfig对象如何获取?
在GenericServlet里面定义了:
java
public ServletConfig getServletConfig() {
return this.config;
}
- 常用方法
java
getInitParameter(String parameterName); //根据参数名称获取指定的参数值
getInitParameterNames(); //获取所有的参数名称
- 需求:
获取servlet里面定义的参数
java
<servlet>
<servlet-name>demo2</servlet-name>
<servlet-class>com.by.servlet.ServletConfigServlet</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>demo2</servlet-name>
<url-pattern>/demo2</url-pattern>
</servlet-mapping>
ServletConfigServlet
java
package com.by.servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
public class ServletConfigServlet extends HttpServlet {
@Override
public void service(ServletRequest servletRequest,
ServletResponse servletResponse) throws ServletException {
//1.获取ServletConfig对象
ServletConfig servletConfig = getServletConfig();
//2.获取Servlet中的初始化参数
String username = servletConfig.getInitParameter("username");
System.out.println(username);
String password = servletConfig.getInitParameter("password");
System.out.println(password);
//3.获取ServletContext对象(域对象)
ServletContext servletContext = servletConfig.getServletContext();
}
}
思考:param参数可不可以在另外的Servlet中获取? 不能
java
public class ServletConfigServlet2 extends HttpServlet {
@Override
public void goGet(ServletRequest servletRequest,
ServletResponse servletResponse) throws ServletException {
//1.获取ServletConfig对象
ServletConfig servletConfig = getServletConfig();
//2.获取Servlet中的初始化参数
String username = servletConfig.getInitParameter("username");//不能获取
System.out.println(username);
String password = servletConfig.getInitParameter("password");//不能获取
System.out.println(password);
//3.获取ServletContext对象(域对象)
ServletContext servletContext = servletConfig.getServletContext();
}
}
三、ServletContext对象
-
是什么
ServletContext是javax.servlet包下的一个接口,又称上下文对象,是配置对象也是一个域对象;
当服务器启动时,会为服务器中的每一个web应用程序创建一个ServletContext对象;
在web应用中的servlet要想实现资源的共享,可以通过ServletContext来完成;
-
如何获取这个对象:
java
public ServletContext getServletContext() {
//获取ServletContext对象
return this.getServletConfig().getServletContext();
}
- 这个对象里面也有很多方法:
java
getInitParameter() //获取指定参数名称的全局参数值
getRealPath(String path) //获得当前项目的服务器磁盘路径
getContextPath() //获取项目的根路径
getAttribute(String parameterName) //获取ServletContext域中指定名称的参数值;
setAttribute(String paramterName,Object parameterValue) //存储参数到ServletContext域中;
removeAttribute(String parameterNam) //将ServletContext域中指定名称的参数移除;
- 需求
获取servlet里面定义的参数
java
<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>root456</param-value>
</context-param>
java
package com.by.servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
public class ServletContextServlet extends HttpServlet {
@Override
public void service(ServletRequest servletRequest,
ServletResponse servletResponse) throws ServletException {
//获取ServletContext对象
ServletContext servletContext = getServletContext();
//1.获取全局初始化参数
String username = servletContext.getInitParameter("username");
System.out.println(username);
String password = servletContext.getInitParameter("password");
System.out.println(password);
//2.获取服务器真实路径
String upload = servletContext.getRealPath("");
System.out.println(upload);
//3.获取项目的根路径
String contextPath = servletContext.getContextPath();
System.out.println(contextPath);
//4.往ServletContext域中,存储一个名称为msg的属性,值为"hello"
String str = "hello";
servletContext.setAttribute("msg",str);
}
}
思考:param参数可不可以在另外的Servlet中获取? 不能
java
package com.by.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
public class ServletContextServlet2 extends HttpServlet {
@Override
public void doGet(ServletRequest servletRequest,
ServletResponse servletResponse) throws ServletException {
//从ServletContext域中取出msg的值
ServletContext servletContext = getServletContext();
//1.获取全局初始化参数
String username = servletContext.getInitParameter("username");
System.out.println(username);
String password = servletContext.getInitParameter("password");
System.out.println(password);
//2.获得msg属性
Object msg = servletContext.getAttribute("msg");//能获取
System.out.println(msg);
}
}
java
package com.by.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
public class ServletContextServlet3 extends HttpServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException {
//1.获取ServletContext对象
ServletContext servletContext = getServletContext();
//将ServletContext域中的msg参数移除
servletContext.removeAttribute("msg");
}
}
java
<servlet>
<servlet-name>context3=</servlet-name>
<servlet-class>com.by.servlet.ServletContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>context3=</servlet-name>
<url-pattern>/context=</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>context2</servlet-name>
<servlet-class>com.by.servlet.ServletContextServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>context2</servlet-name>
<url-pattern>/context2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>context3</servlet-name>
<servlet-class>com.by.servlet.ServletContextServlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>context3</servlet-name>
<url-pattern>/context3</url-pattern>
</servlet-mapping>