过滤器:
1,写一个普通的类实现filter接口
2,实现其中的doFilter方法,这里写业务
3,在web.xml中配置过虑器
用户登录:
1.写好登录的dao,service类和相应方法;
2.前台写一个form,包含帐号和密码两个input;提交时url写查询登录用户的servlet的url
3.后台拿到前台传过来的参数,以此为条件进行查询
此处只写重点
LoginDaoImpl
package dao.impl;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import util.DbUtil;
import dao.LoginDao;
import entity.U;
public class LoginDaoImpl implements LoginDao {
QueryRunner qr = DbUtil.getQueryRunner();
@Override
public U login(U u) {
String sql="select*from u where name=? and password=?";
U uu=new U();
try {
uu=qr.query(sql, new BeanHandler<U>(U.class),u.getName(),u.getPassword());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return uu;
}
}
LoginServiceImpl
package service.impl;
import service.LoginService;
import dao.LoginDao;
import dao.impl.LoginDaoImpl;
import entity.U;
public class LoginServiceImpl implements LoginService {
private LoginDao loginDao=new LoginDaoImpl();
@Override
public U login(U u) {
U uu = loginDao.login(u);
return uu;
}
}
LoginServlet
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.LoginService;
import service.impl.LoginServiceImpl;
import entity.U;
public class LoginServlet extends HttpServlet {
private LoginService loginService=new LoginServiceImpl();
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
String password = request.getParameter("password");
try {
U u=new U();
u.setName(name);
u.setPassword(password);
U uu = loginService.login(u);
if(uu!=null){
request.getSession().setAttribute("u", uu);
request.getRequestDispatcher("/empList").forward(request, response);
}else{
request.getSession().setAttribute("message", "用户名或密码错误,请重试");
response.sendRedirect(request.getContextPath()+"/userlogin.jsp");
}
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect(request.getContextPath()+"/error.jsp");
}
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
LoginFilter.java
package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//登录过滤器
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
//设置字符编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//拿到请求的uri,如;/servletTest/login
String uri = request.getRequestURI();
System.out.println("uri======"+uri);
//拿到uri中第二个“/”的下标
int index = uri.lastIndexOf("/");
System.out.println("/的下标是:"+index);
//截取从第二个“/”开始到最后的所有字符,判断当前访问的是哪个方法
String method = uri.substring(index+1);
System.out.println("当前访问的方法---"+method);
//如果是登录请求或者访问登录页面,则放行
if("login".equals(method)||"userlogin.jsp".equals(method)||"".equals(method)){
//放行
System.out.println("已放行");
chain.doFilter(request,response);
}else{//不是登录请求
//拿到session,判断session中有没有保存的登录信息
System.out.println("不放行");
Object obj = request.getSession().getAttribute("u");
if(obj!=null){
//session中有保存的用户信息,证明已登录,放行
chain.doFilter(request, response);
}else{
//否则就是没登录,不予放行
request.setAttribute("message", "请先登录");
request.getRequestDispatcher("/userlogin.jsp").forward(request, response);
}
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
@Override
public void destroy() {
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletTest</display-name>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<!-- 对所有的请求拦截判断 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>count</servlet-name>
<servlet-class>servlet.CountServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>addEmp</servlet-name>
<servlet-class>servlet.AddEmpServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>deleteEmp</servlet-name>
<servlet-class>servlet.DeleteEmpServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>findUpdEmp</servlet-name>
<servlet-class>servlet.FindUpdEmpServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>updateEmp</servlet-name>
<servlet-class>servlet.UpdateEmpServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>findEmp</servlet-name>
<servlet-class>servlet.FindEmpServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>empList</servlet-name>
<servlet-class>servlet.FindEmpsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>count</servlet-name>
<url-pattern>/count</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>addEmp</servlet-name>
<url-pattern>/addEmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>deleteEmp</servlet-name>
<url-pattern>/deleteEmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>findUpdEmp</servlet-name>
<url-pattern>/findUpdEmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>updateEmp</servlet-name>
<url-pattern>/updateEmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>findEmp</servlet-name>
<url-pattern>/findEmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>empList</servlet-name>
<url-pattern>/empList</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>userlogin.jsp</welcome-file>
</welcome-file-list>
</web-app>
userlogin.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户登录页面</title>
</head>
<body>
<form action="/servletTest/login" method="post">
<h2>用户登录</h2>
帐号:<input type="text" name="name"/><p>
密码:<input type="text" name="password"/><p>
<input type="submit" value="提交"/><p>
${message }
</form>
</body>
</html>