?
?login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body background="1.jpg">
<form action="/day14_login_war_exploded/loginServlet" method="post" class="box" name="form1">
<h2 align="center">Login</h2>
<input class="ienp" type="text" name="username" placeholder="请输入用户名" >
<input class="ienp" type="password" name="password" placeholder="请输入密码" >
<input type="radio" name="permissions" value="管理员" checked>管理员
<input type="radio" name="permissions" value="学生" >学生
<input type="radio" name="permissions" value="教师" >教师
<input type="submit" class="ienp" value="登录">
<input type="reset" class="ienp" onclick="formReset()" value="重置">
</form>
</body>
<script>
function formReset()
{
document.getElementById("from1").reset()
}
</script>
</html>
JDBCUtils.java
public class JDBCUtils {
private static DataSource dataSource;
static {
try {
//加载配置文件
Properties properties = new Properties();
InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(resourceAsStream);
//初始化连接池对象
// ds=DruidDataSourceFactory.createDataSource(properties);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return dataSource;
}
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
}
User.java
public class User {
private int id;
private String username;
private String password;
private String permissions;
public String getPermissions() {
return permissions;
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", permissions='" + permissions + '\'' +
'}';
}
}
UserDao.java
public class UserDao {
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
public User login(User loginUser) {
//ctrl+alt+t
try {
System.out.println(loginUser.getPermissions());
String sql = "select * from user where username=? and password=? and permissions=?";
User user = jdbcTemplate.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class), loginUser.getUsername(),loginUser.getPassword(),loginUser.getPermissions());
return user;
} catch (DataAccessException e) {
// e.printStackTrace();
return null;
}
}
}
LoginServlet.java
@WebServlet( "/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
//获取请求参数
// String username = request.getParameter("username");
// String password = request.getParameter("password");
// //封装user对象
// User loginUser = new User();
// loginUser.setUsername(username);
// loginUser.setPassword(password);
Map<String, String[]> parameterMap = request.getParameterMap();
//创建User对象
User loginUser = new User();
try {
BeanUtils.populate(loginUser,parameterMap);
System.out.println(loginUser);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
//调用UserDao方法
UserDao userDao = new UserDao();
User user = userDao.login(loginUser);
if(user==null)
{
request.getRequestDispatcher("/failServlet").forward(request,response);
}
else {
//存储数据
request.setAttribute("user",user);
request.getRequestDispatcher("/successServlet").forward(request,response);
}
}
}
SuccessServlet.java
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取request域中共享的user对象
User user = (User) request.getAttribute("user");
if(user!=null) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登陆成功"+user.getUsername()+"欢迎您");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
FailServlet.java
@WebServlet( "/failServlet")
public class FailServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登陆失败,用户名或者密码错误");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
数据库环境
界面展示
?
?
?
|