注册登录
package controller;
import java.io.IOException;
import java.sql.SQLException;
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 domain.User;
import model.UserModel;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
UserModel userModel = new UserModel();
User existUser = userModel.login(user);
if(existUser == null) {
//登录失败
request.setAttribute("msg", "用户名或密码错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}else {
//
response.sendRedirect("/web03/success.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>登录页</h1>
<from action="/web03/LoginServlet" method="post">
<table border="1" width="400">
<tr>
<td>用户名</td>
<ts><input type="text" name="username"></ts>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value=""登录></td>
</tr>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>注册页</h1>
<from action="/web03/success.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
性别:<input type="radio" name="sex" value="man">男
<input type="radio" name="sex" value="woman">女
个人介绍:<textarea rows="3" cols="8" name="info"></textarea><br>
<input type="submit" value="提交">
是否确认登录?<input type="checkbox" name="" id="" value="" /><br>
</body>
</html>
package domain;
public class User {
private Integer uid;
private String username;
private String password;
private String nickname;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
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;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
package model;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import domain.User;
import utils.JDBCUtils;
public class UserModel {
/**
*
* @param user
* @return
* @throws SQLException
*/
public User login(User user) throws SQLException{
QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
User existUser = queryRunner.query("select * from user where username=? and password=?", new BeanHandler<User>(User.class),user.getNickname(),user.getPassword());
return existUser;
}
}