- LoginFilter.java
package com.media.filte;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/*")
public class LoginFilter implements Filter {
String[] ignores;
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
HttpSession session = request.getSession();
String uri = request.getRequestURI();
if (session.getAttribute("username") != null || isIgnore(uri)) {
chain.doFilter(request, response);//过滤链
} else {
response.sendRedirect("login.jsp");
}
}
private boolean isIgnore(String uri) {
for (String str : ignores) {
if (uri.indexOf(str) != -1) {
return true;
}
}
return false;
}
public void init(FilterConfig config) throws ServletException {
String ignore=config.getInitParameter("ignore");
ignores=ignore.split(",");
}
}
-CharsetFilter.java
package com.media.filte;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "CharsetFilter")
public class CharsetFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse)resp;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
- wab.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>charsetFilter</filter-name>
<filter-class>com.media.filte.CharsetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>charsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>loginFliter</filter-name>
<filter-class>com.media.filte.LoginFilter</filter-class>
<init-param>
<param-name>ignore</param-name>
<param-value>/css,/js,/login.jsp,/login</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>loginFliter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- UserServlet
package com.media.servlet;
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 java.io.IOException;
@WebServlet("/user")
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
- LoginServlet
package com.media.servlet;
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;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* Servlet 注释配置信息
*/
@WebServlet(name ="LoginServlet",value = "/login")
public class LoginServlet extends HttpServlet {
@Override
public void destroy() {
super.destroy();
System.out.println("servlet 销毁");
}
/**
* post请求 会执行doPost方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
/**
* get请求 会执行doGet方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
HttpSession session=request.getSession();
if ("1".equals(request.getParameter("invalidate"))&&session.getAttribute("username")!=null){
session.invalidate();
//session.removeAttribute("username");
response.sendRedirect("login.jsp");
}
try{
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//建立数据库连接
String url="jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
//sql 模板
String sql="select * from user where username=? and password=?";
//使用PreparedStatement对象执行sql语句
PreparedStatement ps=connection.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
if(rs.next()){
//表示登陆成功
session.setAttribute("username",username);
response.sendRedirect("home.jsp");
}else{
//表示登陆失败
out.print("登录失败,3秒后自动跳转登录页面...");
response.setHeader("refresh","3;url=login.jsp");
}
}catch(Exception e){
e.printStackTrace();
}
}
}