Connect Servlet to JDBC to realize page Jump

Keywords: xml JSP SQL Database

1. First, build a static HTML page for display. The page is simple and only needs a form form

    <form action="Cc" method="post">
        User name < input type = "text" name = "name1" >
        Password < input type = "password" name = "pass" >
        <input type="submit">
        </form>

Here only the fragments in it are written, and the others remain unchanged.

2. Create a Servlet class to process the data of the static page.
Directly in the project, new is a servlet class. Only two methods, doGet() and doPost(), need to be checked.

public class Servlet3 extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name1");
        String pass= request.getParameter("pass");
        request.setAttribute("wantname", name);
        request.setAttribute("wantpass", pass);
        String url="jdbc:mysql://127.0.0.1:3306/maxin";
        try {
    //Connect to database
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, "root", "root");
        Statement stmt = conn.createStatement();
    //sql statements, querying
        String sql = "select * from ma1 ";
         PreparedStatement pst = null;
         pst = (PreparedStatement) conn.prepareStatement(sql);
         ResultSet rs = pst.executeQuery();
         while(rs.next()){                  
             if(rs.getString("name").equals(name)){
                    request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);
             }else{
                    request.getRequestDispatcher("/Lose.html").forward(request, response);
                    }
                }           
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

3. Create web.xml and configure it

<?xml version="1.0" encoding="UTF-8"?> //Pay attention to the coding format
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Servlet_1_24</display-name>  
  <servlet>
    <servlet-name>Servlet3</servlet-name>
    <servlet-class>servletTest.Servlet3</servlet-class> //Absolute path of servlet class
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet3</servlet-name>
    <url-pattern>/Cc</url-pattern>  //url path to access
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  //When the program starts to run, the server calls the. XML file, and first accesses the index.xml file. When the browser accesses, you need to pay attention to adding the page you want to display
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4.
Create a. jsp file to get the data processed by the servlet

<body>
  <h2>Login succeeded</h2>
  <a>Full name</a>
        <%=request.getAttribute("wantname") %>
  <a>Password</a>
        <%=request.getAttribute("wantpass") %>
  </body>

Create another page to show failures,

<body>
    <h1>Not found</h1>
    < a href = ". / myhtml. HTML" > back to query page</a>
  </body>

Query failed to jump to query page.

This is the data from my database

Turn on the server and the display 404 can't be found. Here you need to use the path of the page to be displayed

After the change, the page opens

Enter the name of qqq in the database, where the search field path changes to Cc

If the input is wrong

Click hyperlink to return

Insert data code into database

//  ************Insert*******************************
//          String sql = "insert into ma1 values('"+name+"','"+pass+"')";   
//          int i = stmt.executeUpdate(sql);            
//          request.setAttribute("wantname", name);
//          request.setAttribute("wantpass", pass);
//          
//          if(i>0){
//              request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);
//          }else{
//              request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);
//          }

Posted by Arc on Sun, 03 May 2020 03:00:03 -0700