默认情况下,第一次访问servlet的时候,创建servlet对象。如果servlet构造函数里面的代码或者init方法里面的代码比较多,就会导致用户第一次访问servlet的时候比较慢。这个时候,我们可以改变servlet对象的创建时机:提前到加载web应用的时候。在servlet的配置信息中,加上一个
标签即可。
loadOnStartup
com.by.servlet.LoadOnStartupServlet
1
servlet实例:
public class LoadOnStartupServlet implements HttpServlet {
public LoadOnStartupServlet(){
System.out.println("LoadOnStartupServlet constructor method has run....");
}
}
这样配置之后,servlet的构造函数和init方法就会在web应用加载的时候就会执行。
是什么
ServletConfig是javax.servlet.包下的一个接口,ServletConfig它是Servlet的一个配置对象;
ServletConfig是由tomcat容器创建,通过init方法传入给Servlet;
ServletConfig对象如何获取?
在GenericServlet里面定义了:
public ServletConfig getServletConfig() {
return this.config;
}
常用方法
getInitParameter(String parameterName); //根据参数名称获取指定的参数值
getInitParameterNames(); //获取所有的参数名称
需求:
获取servlet里面定义的参数
demo2
com.by.servlet.ServletConfigServlet
username
root
password
root123
demo2
/demo2
ServletConfigServlet
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中获取? 不能
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是javax.servlet包下的一个接口,又称上下文对象,是配置对象也是一个域对象;
当服务器启动时,会为服务器中的每一个web应用程序创建一个ServletContext对象;
在web应用中的servlet要想实现资源的共享,可以通过ServletContext来完成;
如何获取这个对象:
public ServletContext getServletContext() {
//获取ServletContext对象
return this.getServletConfig().getServletContext();
}
这个对象里面也有很多方法:
getInitParameter() //获取指定参数名称的全局参数值
getRealPath(String path) //获得当前项目的服务器磁盘路径
getContextPath() //获取项目的根路径
getAttribute(String parameterName) //获取ServletContext域中指定名称的参数值;
setAttribute(String paramterName,Object parameterValue) //存储参数到ServletContext域中;
removeAttribute(String parameterNam) //将ServletContext域中指定名称的参数移除;
需求
获取servlet里面定义的参数
username
root
password
root456
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中获取? 不能
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);
}
}
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");
}
}
context3=
com.by.servlet.ServletContextServlet
context3=
/context=
context2
com.by.servlet.ServletContextServlet2
context2
/context2
context3
com.by.servlet.ServletContextServlet3
context3
/context3