登录案例
题目与分析
实现
User类
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserDao 操作数据库
/**
* 操作数据库中user表的类
*/
public class UserDao {
private JdbcTemplate template=new JdbcTemplate(JdbcUtils.getDataSource());
/**
* 登录方法
* @param loginUser 只有用户名和密码
* @return user包含用户的全部信息
*/
public User login(User loginUser){
try{
String sql="select * from user where username=? and password=?";
User user = template.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class),
loginUser.getUsername(), loginUser.getPassword());
return user;
}catch (EmptyResultDataAccessException e){
e.printStackTrace();
return null;
}
}
}
登陆页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="Servlet" method="post">
<label>用户名</label><input name="username" type="text" placeholder="请输入用户名"><br>
<label>密码</label><input name="password" type="text" placeholder="请输入密码"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
JdbcUtils工具类
/**
* Jdbc的工具类
*/
public class JdbcUtils {
private static DataSource ds;
static {
try {
//1.加载配置文件
Properties pro=new Properties();
pro.load(JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//2.初始化对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}
/**
* 获取连接对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
Servlet
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取参数
String username = request.getParameter("username");
String password=request.getParameter("password");
User loginUser=new User();
loginUser.setUsername(username);
loginUser.setPassword(password);
//3.存储
UserDao dao=new UserDao();
User user=dao.login(loginUser);
request.setAttribute("User",user);
//4.转发
if (user!=null){
request.getRequestDispatcher("/successServlet").forward(request,response);
}else {
request.getRequestDispatcher("/failServlet").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
successServlet
@WebServlet("/successServlet")
public class successServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
User user = (User) request.getAttribute("User");
response.getWriter().write("登录成功!"+user.getUsername()+"欢迎您");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
failServlet
@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);
}
}
druid.properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///day14
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
注意
- web注释,括号里面需要/
- xml里的form表单的action属性里不要/
- web目录下有WEB-INF(里面有lib),xml
- response设置编码:response.setContentType(“text/html; charset=UTF-8”);
效果
改进
使用BeanUils,简化数据的封装。Servlet里获取参数封装到loginUser对象中:
Map<String, String[]> map = request.getParameterMap();
User loginUser=new User();
try {
BeanUtils.populate(loginUser,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}