servlet-登录
1.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 + ‘’’ +
‘}’;
}
}
2.dao类
public class UserDao {
public List findAll(String username, String password){
Connection connection= null;
List userList=new ArrayList<>();
PreparedStatement statement=null;
ResultSet resultSet =null;
try {
connection = DButil.getConnection();
String sql=“select * from tb_user where username=? and password=?”;
statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
resultSet =statement.executeQuery();
while (resultSet.next()){
User user=new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7.关闭资源
try{
DButil.closeAll(resultSet,statement,connection);
}catch (SQLException throwables){
throwables.printStackTrace();
}
}
return userList;
}
}
3.servlet类
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() {
}
}
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter(“usern”);
String password=req.getParameter(“passw”);
UserDao userDao =new UserDao();
List userList = userDao.findAll(username,password);
if(userList.isEmpty()){
resp.getWriter().write(“fail”);
}else {
resp.getWriter().write(“success”+"\n"+“usermassage”+userList);
}
}
}
4.util类
public class DButil {
public static Connection getConnection() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2.创建链接
Connection connection = null;
try {
connection = DriverManager.getConnection(“jdbc:mysql://localhost:3306/wzsxy”,“root”,“123456”);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement,Connection connection) throws SQLException {
if(resultSet!=null){
resultSet.close();
}
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
};
}
5.jsp网页
下面展示一些
//<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/login" method="post">
username:<input name="usern" type="text">
password:<input name="passw" type="text">
<input type="submit" value="login">
</form>
</body>
</html>