过滤器Filter
过滤器基础
学习:https://www.jianshu.com/p/33827fba6429
编码过滤器
过滤器java
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
System.out.println("过滤前");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("过滤后");
}
@Override
public void destroy() {
System.out.println("销毁");
}
}
xml配置
<filter>
<filter-name>filter</filter-name>
<filter-class>com.yuan.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/servlet
模拟权限拦截
简单工具类
public class Name {
public static String USERNAME = "username";
}
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/servlet/login" method="get">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
loginServlet
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter(Name.USERNAME);
if(username.equals("admin")){
req.getSession().setAttribute(Name.USERNAME, username);
resp.sendRedirect("/sys/success.jsp");
}else{
resp.sendRedirect("/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
UserFilter
public class UserFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
Object attribute = req.getSession().getAttribute(Name.USERNAME);
if(attribute==null){
resp.sendRedirect("/error.jsp");
}
filterChain.doFilter(req,resp);
}
@Override
public void destroy() {
}
}
sys.successful
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登陆成功</h1>
<P><a href="/servlet/logout">注销</a> </P>
</body>
</html>
logoutServlet
public class logoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().removeAttribute(Name.USERNAME);
resp.sendRedirect("/login.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>权限不够</h1>
<p>
<a href="login.jsp">注销</a>
</p>
</body>
</html>
xml配置
<filter>
<filter-name>UserFilter</filter-name>
<filter-class>com.yuan.filter.UserFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>
监听器
监听器基础
学习:https://www.cnblogs.com/xing901022/p/4378727.html
监听在线人数
监听器java 监听在线人数,创建session时人数+1,销毁session时人数-1
public class OnlineCounterListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext context = httpSessionEvent.getSession().getServletContext();
Integer count = (Integer) context.getAttribute("count");
if(count==null){
count = new Integer(1);
}else{
int i = count.intValue();
count = new Integer(i+10);
}
context.setAttribute("count",count);
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext context = httpSessionEvent.getSession().getServletContext();
Integer count = (Integer) context.getAttribute("count");
if(count==null){
count = new Integer(0);
}else{
int i = count.intValue();
count = new Integer(i-1);
}
context.setAttribute("count",count);
}
}
xml配置
<listener>
<listener-class>com.yuan.listener.OnlineCounterListener</listener-class>
</listener>
jsp 显示数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>在线观看人数为:<span style="color: brown"><%=this.getServletConfig().getServletContext().getAttribute("count")%></span></h1>
</body>
</html>
|