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">

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://";
        try {
    //Connect to database
        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();
                    request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);
                    request.getRequestDispatcher("/Lose.html").forward(request, response);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
        } catch (SQLException e) {
            // TODO Auto-generated catch block

3. Create web.xml and configure it

<?xml version="1.0" encoding="UTF-8"?> //Pay attention to the coding format
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
    <servlet-class>servletTest.Servlet3</servlet-class> //Absolute path of servlet class
    <url-pattern>/Cc</url-pattern>  //url path to access
  //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

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

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

Create another page to show failures,

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

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