Servlet登录功能的简单实现
Servlet 是什么?
Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
使用 Servlet,您可以收集来自网页表单的用户输入,呈现来自数据库或者其他源的记录,还可以动态创建网页。
环境配置
IDEA
tomcat 8.0.50
1.创建Servlet项目
Aplication Server中选择 Tomcat,勾选Web Application
2.导入需要的jar包
- 拖入对应的jar包
- 打开project structure ——Aritifacts项
- 点击右下角fix,成功导入
3.创建每一层的包
视图如下
4.每一个类的具体代码
- UserDao
package com.zr0721.dao;
import com.zr0721.model.User;
import com.zr0721.util.DBUtil;
import java.sql.*;
public class UserDao {
public User selectByName(String name){
ResultSet rs = null;
Connection conn = null;
PreparedStatement pstmt = null;
DBUtil util = new DBUtil();
User user = new User();
try{
conn = util.getConnection();
pstmt = conn.prepareStatement("select * from user where name =?");
pstmt.setString(1,name);
rs = pstmt.executeQuery();
while (rs.next()){
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
user.setAge(rs.getString(4));
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
util.closeAll(rs,pstmt,conn);
}catch (SQLException e){
e.printStackTrace();
}
}
return user;
}
}
- User
package com.zr0721.model;
public class User {
private int id;
private String name;
private String password;
private String age;
//alt+insert
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age='" + age + '\'' +
'}';
}
}
- UserService
package com.zr0721.service;
import com.zr0721.dao.UserDao;
import com.zr0721.model.User;
public class UserService {
UserDao userDao = new UserDao();
public User selectByName(String name){
return userDao.selectByName(name);
}
}
- LoginServlet
package com.zr0721.servlet;
import com.zr0721.model.User;
import com.zr0721.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();
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String password = req.getParameter("password");
User user = userService.selectByName(name);
System.out.println(password);
System.out.println(user);
if(password.equals(user.getPassword())){
resp.getWriter().write("success");
}else {
resp.getWriter().write("failed");
}
}
}
- DBUtil
package com.zr0721.util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost:3306/jdbc_test?useSSL=false","root","******");
return connection;
}
public static void closeAll(ResultSet rs,Statement stmt,Connection conn) throws SQLException {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
- Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.zr0721.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>登录页面</h1>
<form action="/login" method="post">
name:<input name="name" type="text">
password:<input name="password" type="password ">
<input type="submit" value="登录">
</form>
</body>
</html>
5.验证登录功能
输入数据库中对应的用户名和密码
正确将会返回success;否则将返回failed