用servlet完成登陆验证
package com.zr.bean;
public class User {
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.zr.dao;
import com.zr.bean.User;
import com.zr.utl.DButil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public User findUserByUserNameAndPassword(String username,String password){
Connection connection= null;
PreparedStatement statement=null;
ResultSet resultSet =null;
User user=null;
try {
connection = DButil.getConnection();
//3.写sql语句
String sql="select * from tb_user where username=? and password=?";
//4.获得statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
//5.执行sql 得到结果集
resultSet = statement.executeQuery();
//6.处理结果集
while (resultSet.next()){
user=new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//7.关闭资源
try {
DButil.closeAll(resultSet,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return user;
}
public void deleteById(int id){
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DButil.getConnection();
System.out.println("创建连接成功");
//3.写sql语句
String sql="delete from tb_user where id=?";
//4.获得statement对象
statement=connection.prepareStatement(sql);
statement.setInt(1,id);
//5.执行sql 得到结果集
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DButil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void add(User user){
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DButil.getConnection();
System.out.println("创建连接成功");
//3.写sql语句
String sql="INSERT INTO tb_user (username,PASSWORD) VALUES (?,?)";
//4.获得statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DButil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void update(User user){
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DButil.getConnection();
System.out.println("创建连接成功");
//3.写sql语句
String sql="UPDATE tb_user SET username=?,PASSWORD=? WHERE id=?";
//4.获得statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.setInt(3,user.getId());
//5.执行sql 得到结果集
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DButil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
package com.zr.service.impl;
import com.zr.bean.User;
import com.zr.dao.UserDao;
import com.zr.service.UserService;
public class UserServiceimpl implements UserService {
UserDao userDao=new UserDao();
@Override
public boolean login(String username, String password) {
User user = userDao.findUserByUserNameAndPassword(username, password);
if(user==null){
return false;
}else {
return true;
}
}
}
package com.zr.service;
public interface UserService {
boolean login(String username,String password);
}
package com.zr.servlet;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
servletResponse.getWriter().write("Hello");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
import com.zr.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet{
UserService userService=new UserService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
if(username.equals("123")&&password.equals("123")){
resp.getWriter().write("success");
}else{
resp.getWriter().write("failure");
}
}
}
package com.zr.utl;
import java.sql.*;
public class DButil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wzsxy","root","123456");
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement,Connection connection) throws SQLException {
resultSet.close();
statement.close();
connection.close();
}
}
修改index.jsp网页
html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/login" method="post">
username:<input name="username" type="text">
password:<input name="password" type="password">
<input type="submit" value="login">
</form>
</body>
</html>