加深印象篇之Servlet

环境配置

需要配置web.xml文件,

<web-app>      
    <servlet>
        <servlet-name>你起的一个类的名字</servlet-name>
        <servlet-class>该类的全部路径</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>同上面那个类的名字</servlet-name>
        <url-pattern>/加上那个类的名字</url-pattern>
    </servlet-mapping>
</web-app> 

要使用到Servlet相关类,需要提前将相关导包导入到pom.xml文件中(导包的导入在Maven项目就已经将网址分享啦!网址很好用,咱就是说)

Servlet类中,包含两个方法:doGet方法和doPost方法,其中,doGet方法是将数据写入到网页中,即与在jsp文件中写<%%>这样子的Java代码是一样的道理,但servlet更加受欢迎一些,更常见一些,应用更广一些;而doPost方法,主要适用于将数据传递到表单中,达到修改数据库数据的目的;

相关文件的编写

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 {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected 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结构一致,自己自行编写即可。

posted @ 2022-10-05 09:30  yesyes1  阅读(22)  评论(0编辑  收藏  举报