java 继承filter类过滤登录

public class Admin {

private int id;
private String userName;
private String pwd;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;

}

}


public class Employee {
private int empId;
private String empName;
private int dept_id;

public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
}

public interface IAdminDao {

Admin gatAdminByAdminName(Admin admin);

}


public interface IEmployeeDao {
List<Employee> getEmployees();

}


public class AdminDao implements IAdminDao {
@Override
public Admin gatAdminByAdminName(Admin admin) {
try {
return JDBCUtils.getQueryRunner().query("select * from Admin where userName=? and pwd=?", new BeanHandler<Admin>(Admin.class),admin.getUserName(),admin.getPwd());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}


}


public class EmployeeDao implements IEmployeeDao {
@Override
public List<Employee> getEmployees() {
try {
return JDBCUtils.getQueryRunner().query("select * from employee", new BeanListHandler<Employee>(Employee.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

}


public interface IAdminService {
Admin gatAdminByAdminName(Admin admin);

}


public interface IEmployeeService {
List<Employee> getEmployees();

}


public class AdminService implements IAdminService {
IAdminDao adminDao =new AdminDao();
@Override
public Admin gatAdminByAdminName(Admin admin) {
return adminDao.gatAdminByAdminName(admin);
}

}


public class EmployeeService implements IEmployeeService {
IEmployeeDao employeeDao=new EmployeeDao();
@Override
public List<Employee> getEmployees() {
return employeeDao.getEmployees();
}
}


public class ListServlet extends HttpServlet {
private String uri="";
IEmployeeService employeeService=new EmployeeService();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//获取员工列表
List<Employee> employees = employeeService.getEmployees();
request.setAttribute("employess", employees);
uri="/list.jsp";
} catch (Exception e) {
e.printStackTrace();
uri="/error.jsp";
}
request.getRequestDispatcher(uri).forward(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}


public class LoginServlet extends HttpServlet {
private String uri="";
private IAdminService adminService=new AdminService();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
request.setCharacterEncoding("utf-8");
String username=request.getParameter("userName");
String pwd=request.getParameter("pwd");
Admin admin=new Admin();
admin.setUserName(username);
admin.setPwd(pwd);
Admin Admins = adminService.gatAdminByAdminName(admin);
if (Admins!=null) {
request.getSession().setAttribute("userinfo", admin);
uri="/ListServlet";
}
else {
uri="/login.jsp";
}
} catch (Exception e) {
e.printStackTrace();
uri="error.jsp";
}
//跳转
request.getRequestDispatcher(uri).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}

c3p0-config.xml文件配置

<c3p0-config>
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/day15?characterEncoding=utf8</property>
    <property name="user">root</property>
    <property name="password">root</property>
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">10</property>
  </default-config>

<!--
  <named-config name="dumbTestConfig">
    <property name="maxStatements">200</property>
    <property name="jdbcUrl">jdbc:test</property>
    <user-overrides user="poop">
      <property name="maxStatements">300</property>
    </user-overrides>
   </named-config>
-->

</c3p0-config>


web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>gz.itcast.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>gz.itcast.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ListServlet</servlet-name>
<servlet-class>gz.itcast.servlet.ListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ListServlet</servlet-name>
<url-pattern>/ListServlet</url-pattern>
</servlet-mapping>

</web-app>


过滤器类:

public class LoginFilter implements Filter {
@Override
public void destroy() {

}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
String uri="";
//转化为 HttpServlet
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)res;

//获取请求地址
String requestURI = request.getRequestURI();
String subUrl=requestURI.substring(requestURI.lastIndexOf("/")+1,requestURI.length());

//是登录地址放行
if (subUrl.equals("login.jsp")||subUrl.equals("Login")) {
chain.doFilter(request, response);
}
else {
//不是登录地址获取是否登录
HttpSession session = request.getSession();
if (session!=null) {
//获取登录值
Object attribute = session.getAttribute("userinfo");
if (attribute!=null) {
chain.doFilter(request, response);
}else {
uri="login.jsp";
}
}else {
uri="login.jsp";
}
request.getRequestDispatcher(uri).forward(request, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {

}

}

login.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>


<body>
   <form name="form" action="${pageContext.request.contextPath}/Login" method="post">
<table align="center" border="1" cellpadding="5"
cellspacing="0">
<tr>
<td>用户名:</td></br>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密码:</td></br>
<td><input type="password" name="pwd"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="亲,点我登录"/></td>
</tr>
</table>
</form>
</body>

</html>

list.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>员工查询</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<center>
<h3>欢迎,${sessionScope.userinfo.userName }</h3>
</center>
<table align="center" border="1" cellpadding="5" cellspacing="0">
<tr>
<td>序号</td>
<td>员工姓名</td>
<td>员工科室号</td>
</tr>
<c:if test="${not empty requestScope.employess }">
<c:forEach items="${requestScope.employess }" var="employee"
varStatus="vars">
<tr>
<td>${vars.count}</td>
<td>${employee.empName}</td>
<td>${employee.dept_id}</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值