package com.neu.service;
import java.util.List;
import com.neu.entity.Power;
import com.neu.entity.Userinfo;
public interface LoginService {
Userinfo getLogin(String username,String password) throws Exception;
List<String> getRolname() throws Exception;
Power getById(String rolname) throws Exception;
int getInsert(Userinfo userinfo) throws Exception;
}
package com.neu.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import com.neu.entity.Power;
import com.neu.entity.PowerExample;
import com.neu.entity.Userinfo;
import com.neu.entity.UserinfoExample;
import com.neu.mapper.PowerMapper;
import com.neu.mapper.UserinfoMapper;
public class LoginServiceImpl implements LoginService {
private UserinfoMapper userinfoMapper;
private SqlSession session;
private PowerMapper powerMapper;
public void before() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
session = factory.openSession();
userinfoMapper = session.getMapper(UserinfoMapper.class);
powerMapper = session.getMapper(PowerMapper.class);
}
public void after() {
session.close();
}
@Override
public Userinfo getLogin(String username, String password) throws Exception {
before();
UserinfoExample example = new UserinfoExample();
example.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
List<Userinfo> list = userinfoMapper.selectByExample(example);
after();
if(list.size()==0) {
return null;
}
return list.get(0);
}
@Override
public List<String> getRolname() throws IOException {
before();
PowerExample example = new PowerExample();
List<Power> list = powerMapper.selectByExample(example);
List<String> list2 = new ArrayList<>();
after();
for(Power power:list) {
String rolname = power.getRolname();
list2.add(rolname);
}
return list2;
}
@Override
public Power getById(String rolname) throws Exception {
before();
PowerExample example = new PowerExample();
example.or().andRolnameEqualTo(rolname);
List<Power> list = powerMapper.selectByExample(example);
after();
return list.get(0);
}
@Override
public int getInsert(Userinfo userinfo) throws Exception {
before();
UserinfoExample example = new UserinfoExample();
int n = userinfoMapper.insertSelective(userinfo);
session.commit();
after();
return n;
}
}
package com.neu.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neu.entity.Power;
import com.neu.entity.Userinfo;
import com.neu.service.LoginService;
import com.neu.service.LoginServiceImpl;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private LoginService login = new LoginServiceImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
if("login".equals(method)) {
doLogin(request,response);
}else if("loglist".equals(method)) {
doLoglist(request,response);
}else if("logliston".equals(method)) {
doLogon(request,response);
}
}
private void doLogon(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
String rolname = request.getParameter("rolname");
Power power=null;
try {
power = login.getById(rolname);
} catch (Exception e) {
e.printStackTrace();
}
Userinfo userinfo = new Userinfo();
userinfo.setUsername(username);
userinfo.setPassword(password);
userinfo.setEmail(email);
userinfo.setPower(power);
int n= 0;
try {
n = login.getInsert(userinfo);
} catch (Exception e) {
e.printStackTrace();
}
if(n!=0) {
doLogin(request,response);
}else {
doLoglist(request,response);
}
}
private void doLoglist(HttpServletRequest request, HttpServletResponse response) {
try {
List<String> list = login.getRolname();
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/loglist.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void doLogin(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
Userinfo userinfo = login.getLogin(username, password);
if(userinfo !=null) {
request.setAttribute("userinfo", userinfo);
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
}else {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ 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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/LoginServlet?method=login" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>
<a href="${pageContext.request.contextPath }/LoginServlet?method=loglist">注册</a>
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
用户名:${ userinfo.username } 权限:${userinfo.power.rolname }
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/LoginServlet?method=logliston" method="post">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td><td><input type="password" name="password"></td>
</tr>
<tr>
<td>email:</td><td><input type="email" name="email"></td>
</tr>
<tr>
<td>权限:</td>
<td>
<select name="rolname">
<c:forEach items="${list }" var="rolname">
<option value="${ rolname }">${ rolname }</option>
</c:forEach>
</select>
</td>
</tr>
<tr align="center"><td colspan="2"><input type="submit" value="注册"> </td> </tr>
</table>
</form>
</body>
</html>