Servlet
Servlet的生命周期
1、从第一次调用到服务器关闭。
2、如果Servlet在web.xml中配置了load-on-startup,生命周期为从服务器启动到服务器关闭。
注意:
init方法是对Servlet进行初始化的一个方法,会在Servlet第一次加载进行存储时执行
destory方法是在Servlet被销毁时执行,也就是服务器关闭时。
代码示例:
public class ServletLife extends HttpServlet {
//第一次加载时会执行
@Override
public void init() throws ServletException {
System.out.println("Servlet初始化完成");
}
//每次加载都会执行
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("Servlet life");
System.out.println("Servlet life");
}
@Override
public void destroy() {
System.out.println("我被销毁了......");
}
}
运行结果:
第一次运行
第一次以上运行
Servlet中的四个对象:ServletConfig、ServletContext,request、response
1、ServletConfig
/**
* ServletConfig Servlet配置
*/
System.out.println("ServletConfig....................");
//获取InitParameter
ServletConfig sc = this.getServletConfig();
System.out.println(sc.getInitParameter("username"));
//InitParameterNames
Enumeration<String> e = sc.getInitParameterNames();
while(e.hasMoreElements()){
String key = e.nextElement();//name
String val = sc.getInitParameter(key);//value
System.out.println(key+"="+val);
}
//ServletName
String str = this.getServletConfig().getServletName();
System.out.println(str);
System.out.println(this.getServletName());
System.out.println("..........................");
运行结果:
2、ServletContext
/**
* ServletContext Servlet环境
*/
System.out.println("ServletContext....................");
ServletContext sct = this.getServletContext();
//ServletContext sct1 = this.getServletConfig().getServletContext();
System.out.println("ServletContext = "+sct.getInitParameter("username"));
Enumeration<String> e2 = sct.getInitParameterNames();
while(e2.hasMoreElements()){
String key = e2.nextElement();
String val = sct.getInitParameter(key);
System.out.println(key+"="+val);
}
System.out.println("..........................");
运行结果:
3、request
/**
* request 请求
*/
System.out.println("request....................");
request.setCharacterEncoding("UTF-8");
System.out.println(request.getParameter("username"));
System.out.println(request.getParameter("password"));
System.out.println("uri: "+request.getRequestURI());//统一资源标识符
System.out.println("url: "+request.getRequestURL());//统一资源定位符
System.out.println("http协议http/1.1: "+request.getProtocol());
System.out.println("http: "+request.getScheme());
System.out.println("端口: "+request.getServerPort());
System.out.println("项目名称ContextPath: "+request.getContextPath());
System.out.println("servlet路径ServletContext: "+request.getServletPath());
System.out.println("所有请求参数: "+request.getQueryString());
System.out.println("远程主机地址: "+request.getRemoteAddr());
System.out.println("请求头: "+request.getHeader("Accept-Language"));
request.getSession().getAttribute("");
request.getSession().setAttribute("", "");
request.setAttribute("", "");
运行结果:
4、response
/**
* response 响应
*/
System.out.println(response.getStatus());
System.out.println("response....................");
response.setHeader("Refresh", "3");//设置每隔3秒就自动刷新一次
response.setStatus(302);//状态码302就代表重定向
response.setHeader("location", "http://www.baidu.com");
response.sendRedirect("");
response.setContentType("text/html;charset=utf-8");
System.out.println(response.getStatus());
运行结果: