环境配置
需要配置web.xml文件,如图所示:
userServlet:是指那个继承自HttpServlet的.java文件的名称
index.jsp:是指那个随意一个.jsp文件名称即可
要使用到Servlet相关类,需要提前将相关导包导入到pom.xml文件中
相关文件的编写
1、JDBCUtils.java文件(存储连接数据库的各种操作:连接、查询、修改,关闭等)
import java.sql.*;public class JDBCUtils {private String driver="com.mysql.cj.jdbc.Driver";private String url="jdbc:mysql://localhost:3306/ad";private String root="root";private String password="20214063";private Connection conn=null;private Statement stmt=null;private ResultSet rs=null;//打开连接public boolean connect(String sql) throws ClassNotFoundException, SQLException {boolean b=false;Class.forName(driver);conn= DriverManager.getConnection(url,root,password);b=true;return b;}//修改public void update(String sql) throws SQLException {boolean b=false;stmt=conn.createStatement();stmt.execute(sql);b=true;}//查询public void print(String sql) throws SQLException {boolean b = false;stmt = conn.createStatement();rs = stmt.executeQuery(sql);}//关闭连接public void close() throws SQLException {if(rs!=null){rs.close();}if(stmt!=null){stmt.close();}if(conn!=null){conn.close();}}}
2、userServlet.java文件(用于读取用户的相关信息,并进行判断)
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 java.io.IOException;
import java.sql.SQLException;@WebServlet("/userServlet")
public class userServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doGet(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String name=req.getParameter("name");String id=req.getParameter("id");if(name==null||id==null){System.out.println("用户名或者密码不能为空!");resp.sendRedirect("index.jsp");return ;}userBean user=new userBean();boolean isValue= false;try {isValue = user.isValid(name,id);} catch (SQLException e) {throw new RuntimeException(e);} catch (ClassNotFoundException e) {throw new RuntimeException(e);}if(isValue){System.out.println("登录成功!");req.getSession().setAttribute("name",name);resp.sendRedirect("index.jsp");return ;}}
}
3、userBean.java文件(用于存放具体的判断验证是否有效的代码,上个文件只是引用其中的具体代码)
public class userBean {//登录验证public boolean isValid(String name,String id) throws SQLException, ClassNotFoundException {boolean b=false;JDBCUtils uu=new JDBCUtils();if(uu.connect()){String sql="select * from we where name="+name+" and id="+id+"";uu.print(sql);if(uu.next()){b=true;return b;}b=false;return b;}return b;}}
4、index.jsp(编写的登录界面,直接传递值传到页面中)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>登录界面</title></head><body>
<div id="loginDiv" style="..."><form action="userServlet?name=1" id="form" method="post"><h1 id="loginMsg">LOGIN IN</h1><div id="用户名或密码不正确"></div><p>Username:<input id="username" name="username" type="text"></p><p>Id:<input id="id" name="id" type="password"></p><div id="subDiv"><input type="submit" class="button" value="login up"><input type="reset" class="button" value="reset"><a href="register.jsp">没有账号?</a></div></form>
</div></body>
</html>
当然还有相关的注册界面、验证是否登陆成功、注册成功界面的编写,其基本结构与index.jsp结构一致,自己自行编写即可。