ServletContext全局对象:
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletConfig config = this.getServletConfig();//创建ServletConfig对象
System.out.println(config.getInitParameter("className"));
System.out.println(config.getInitParameter("url"));
ServletContext servletContext = this.getServletContext();
// servletContext.getAttribute()//得到对象中的值 通过name的
// servletContext.setAttribute();//加入参数 键值对
// servletContext.removeAttribute(); 删除参数
// servletContext.getAttributeNames(); 得到对象中的参数名称
System.out.println("servletContext.getRealPath(\"/WEB-INF\") = " + servletContext.getRealPath("/WEB-INF"));
//获得文件的绝对路径
System.out.println("servletContext.getContextPath() = "
+ servletContext.getContextPath());
//得到工程路径如果工程在root路径下那就是空的
// this.getServletConfig().getServletContext() 获得SerivetContext对象方式
// this.getServletContext() 获得SerivetContext对象方式
// req.getServletContext() 获得SerivetContext对象方式
// req.getSession().getServletContext() 获得SerivetContext对象方式
}
}
过滤器:
所有资源访问之前被调用对象
1.数据库加载
2.权限验证
3.数据检查
4.数据加工
5.数据缓存
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//@WebFilter("*")
public class AFilter implements Filter {
//过滤器实现的是一个接口
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (request.getRequestURL().toString().endsWith(".jpg")) {//判断条件
//通行
//FilterChain 中的方法
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
}
}
监听器里面有监听Session创建啊销毁啊添加啊等方法
@WebListener
public class SessionListener implements HttpSessionIdListener,
HttpSessionListener, HttpSessionAttributeListener{
public void sessionIdChanged(HttpSessionEvent var1, String var2){
System.out.println("sessionIdChanged..");
}
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("sessionCreated..");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("sessionDestroyed..");
}
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("attributeAdded..");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("attributeRemoved..");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("attributeReplaced..");
}
}