users表
1.新建login web项目
2.导入相关jar包
3.WEB-INF下配置web.xml
(1)配置监听器
(2)
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
4.src下配置applicationContext.xml
1.pojo
Users
package com.bjsxt.pojo;
public class Users {
private int id;
private String username;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.mapper
(1)UsersMapper.java(接口)
package com.bjsxt.mapper;
import com.bjsxt.pojo.Users;
public interface UsersMapper {
Users selByUsers(Users users);
}
(2)UsersMapper.xml
(接口绑定原则)
3.service
UsersService.java
package com.bjsxt.service;
import com.bjsxt.pojo.Users;
public interface UsersService {
Users login(Users users);
}
UsersServiceImpl.java
package com.bjsxt.service.impl;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
public class UsersServiceImpl implements UsersService {
private UsersMapper usersMapper;
public UsersMapper getUsersMapper() {
return usersMapper;
}
public void setUsersMapper(UsersMapper usersMapper) {
this.usersMapper = usersMapper;
}
@Override
public Users login(Users users) {
return usersMapper.selByUsers(users);
}
}
4.servlet
LoginServlet.java
package com.bjsxt.servlet;
import java.io.IOException;
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 org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
import com.bjsxt.service.impl.UsersServiceImpl;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private UsersService usersService;
//init()方法对全局的usersService进行注入
@Override
public void init() throws ServletException {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
//usersService应该是usersServiceImpl,但是现在是代理,所以在配置文件中修改成cglib代理
usersService = wac.getBean("usersService",UsersServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Users users = new Users();
users.setUsername(req.getParameter("username"));
users.setPassword(req.getParameter("password"));
Users user = usersService.login(users);
if(user!=null){
resp.sendRedirect("main.jsp");
}else{
resp.sendRedirect("login.jsp");
}
}
}
5.页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
6.advice
MyBefore.java
package com.bjsxt.advice;
import java.lang.reflect.Method;
import java.util.Date;
import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
import com.bjsxt.pojo.Users;
public class MyBefore implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
Users users = (Users) arg1[0];
Logger logger = Logger.getLogger(MyBefore.class);
logger.info(users.getUsername()+"在"+new Date().toLocaleString()+"进行登录");
}
}
package com.bjsxt.advice;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
import com.bjsxt.pojo.Users;
public class MyAfter implements AfterReturningAdvice{
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
Logger logger = Logger.getLogger(MyAfter.class);
Users users = (Users) arg2[0];
if(arg0!=null){
logger.info(users.getUsername()+"登录成功!");
}else{
logger.info(users.getUsername()+"登录失败!");
}
}
}
7.在src下配置log4j.properties
log4j.rootCategory=INFO, CONSOLE ,LOGFILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%m %n
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=E:/my.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%m %n