站点访问数统计案例:
源代码:
public class Demo17Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
Integer count =(Integer) servletContext.getAttribute("count");
if(null == count){
//第一次请求,存储一个count变量 到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);
}
}
运行效果图:
获取upload 和 myimg的真实路径
源代码:
package day51.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Demo16Servlet 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);
//获取myimg文件的服务器真实路径
String myimg = servletContext.getRealPath("upload/myimg");
System.out.println(myimg);
//ServletContext类中的getRealPath,相对于当前项目!!
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
运行效果图:
获取动态参数的名称和值:
源代码:
package day51.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
public class Demo15Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取全局初始化参数
ServletContext servletContext = getServletContext();
//动态获取参数名称
Enumeration 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);
}
}
运行效果图:
自定义Servlet的生命周期
web.xml中设置
demo05:设置为1
Demo05Servlet
day51.servlet.Demo05Servlet
1
Demo05Servlet
/demo05
demo06:设置为3
Demo06Servlet
day51.servlet.Demo06Servlet
3
Demo06Servlet
/demo06
代码:
demo05:
package day51.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Demo05Servlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println(“Demo05初始化”);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Demo05处理请求");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
public void destroy() {
System.out.println("Demo05销毁");
}
}
demo06:
package day51.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Demo06Servlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println(“Demo06初始化”);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Demo06处理请求");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
public void destroy() {
System.out.println("Demo06销毁");
}
}
运行效果:
程序终止时 销毁: