JSP uses MVC mode to complete deletion and modification function instance details

Time:2020-6-16

The example of this paper describes the method of JSP using MVC mode to complete the function of deletion and modification. Share with you for your reference. The details are as follows:

Objectives:

① Further understand MVC mode;
② Master the basic implementation process of deletion function;
③ Master the basic implementation process of modification function.

Main contents:

① Use MVC to complete the deletion function;
② Use MVC mode to complete the information update function.

1. How to use MVC mode to complete deletion function

According to the characteristics of MVC mode, three parts of MVC are considered.
① Consider part V first:

Input: generally, the deletion function is completed on the basis of query, so you can add the deleted hyperlink on the user information list interface.

Output: prompts the user whether the deletion is successful. It can be displayed in a separate interface or in other pages. We use the second method to display prompt information in the user list interface.

② Second, consider part M: need to User.java Add the method to delete users.

Finally, consider the C part: get the user name that the user wants to delete, then call the M part to complete the deletion, and finally turn to the user information list interface.

The following are implemented separately.

2. At userlist.jsp Add delete hyperlink and prompt information in the file

1) Add delete hyperlink (red part):

<c:forEach var="user" items="${users}">
 <tr>
  <td>
   ${user.username}
  </td>
  <td>
   ${user.userpass}
  </td>
  <td>
   <a href="deleteUser?username=${ user.username }"> delete</a>
  </td>
 </tr>
</c:forEach>

Note: when deleting, you need to know the name of the user to be deleted (here the name is the primary key), so pass the name through the parameter.

2) To add a prompt:

Copy codeThe code is as follows:
<font color=”red”> ${deleteinfo} </font>

3) The modified code is as follows:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<font color="red"> ${addinfo} </font>
<font color="red"> ${deleteinfo} </font>
<br>
<c:if test="${pageNo!=1}">
 < a href = "getalluser? Pageno = 1" > first page</a>
 < a href = "getalluser? Pageno = ${pageno-1}" > previous page</a>
</c:if>
<c:if test="${pageNo!=pageCounter}">
 < a href = "getalluser? Pageno = ${pageno + 1}" > next page</a>
 < a href = "getalluser? Pageno = ${pagecounter}" > last page</a>
</c:if>
<br>
<table width="200" border="1" height="56">
 <tbody>
  <tr>
   <td>
    user name
   </td>
   <td>
    Password
   </td>
   <td>
   </td>
  </tr>
  <c:forEach var="user" items="${users}">
   <tr>
    <td>
     ${user.username}
    </td>
    <td>
     ${user.userpass}
    </td>
    <td>
     <a href="deleteUser?username=${ user.username }"> delete</a>
    </td>
   </tr>
  </c:forEach>
 </tbody>
</table>

3. Write part M: in User.java Add method to

stay User.java The deleteuser method is added in, and the reference code is as follows:

public boolean deleteUser(String username) {
 Connection con = null;
 Statement stmt = null;
 Boolean B; // indicates whether the deletion is successful or not
 try {
  //Indicates the drivers needed to connect to the database
  Class.forName("oracle.jdbc.driver.OracleDriver");
  //Establish a connection to the database
  con = DriverManager.getConnection(
    "jdbc:oracle:thin:@myserver:1521:mydb", "scott",
    "tiger");
  //Writing SQL statement to query database information
  String sql = "delete from usertable where username='" + username + "'";
  //Create statement objects for executing SQL statements
  stmt = con.createStatement();
  //Execute the statement without result set return. It returns the number of records in the database table
  int n = stmt.executeUpdate(sql);
  if (n > 0)
   b = true;
  else
   b = false;
 } catch (Exception e) {
  b = false;
 } finally {
  //Close related objects
  if (stmt != null)
   try {
    stmt.close();
   } catch (Exception ee) {
   }
  if (con != null)
   try {
    con.close();
   } catch (Exception ee) {
   }
 }
 return b;
}

Note: the red part needs to be modified to your own server and database.

4、 Write part C: add DeleteUser controller

DeleteUser.java The code of is as follows:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javabean.User;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeleteUser extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 //Access to information
 String username = request.getParameter("username");
 //Call JavaBean
 User user = new User();
 boolean b = user.deleteUser(username);
 //Pass information about the success or failure of deletion
 String info;
 if(b)
  Info = "deletion succeeded! ";
 else
  Info = "deletion failed! ";
 request.setAttribute("deleteinfo",info);
 //Select interface response to user
 RequestDispatcher rd = request.getRequestDispatcher("getAllUser");
 rd.forward(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  doGet(request,response);
}
}

5. How to use MVC mode to realize modification function

The modification function itself includes two processes: the user selects the user to modify, and then displays the user’s information on the modification interface; the user modifies, submits the modification, and the server completes the modification function.
For the first function, MVC mode is adopted:

Part V:

Input: add “modify” hyperlink on the user list interface.
Output: user information modification interface, which displays the queried information on the modification interface.

Part M: write a method to query user information according to user name.

Part C: get the name of the user to be deleted, call the query method of part m, get the user information, pass it to the modification interface, and use the modification interface to respond to the user.

For the second part, MVC mode is adopted:

Part V

Input: the modification interface written above.
Output: user information list interface

Part M: write the method of user information modification.

Part C: obtain the information entered by the user, call part m to complete the modification, and turn to the list interface to respond to the user.

Here are the implementation methods.

6. Modify user information list interface

The modified code is as follows:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<font color="red"> ${addinfo} </font>
<font color="red"> ${deleteinfo} </font>
<font color="red"> ${updateinfo} </font>
<br>
<c:if test="${pageNo!=1}">
< a href = "getalluser? Pageno = 1" > first page</a>
< a href = "getalluser? Pageno = ${pageno-1}" > previous page</a>
</c:if>
<c:if test="${pageNo!=pageCounter}">
< a href = "getalluser? Pageno = ${pageno + 1}" > next page</a>
< a href = "getalluser? Pageno = ${pagecounter}" > last page</a>
</c:if>
<br>
<table width="200" border="1" height="56">
<tbody>
 <tr>
  <td>
   user name
  </td>
  <td>
   Password
  </td>
  <td>
  </td>
 </tr>
 <c:forEach var="user" items="${users}">
  <tr>
   <td>
    ${user.username}
   </td>
   <td>
    ${user.userpass}
   </td>
   <td>
    <a href="findUser?username=${ user.username }"> Modify</a>
   </td>
   <td>
    <a href="deleteUser?username=${ user.username }"> delete</a>
   </td>
  </tr>
 </c:forEach>
</tbody>
</table>

7. Edit information modification interface

The reference code is as follows:

<% @ page contenttype = "text / HTML; charset = GB2312"% > modify user < br >
<form name="form1" method="post" action="updateUser">
 User name:${ user.username }
<input type="hidden" name="username" value="${user.username}"><br>
 Password: < input type = "text" name = "userpass" value = "${ user.userpass }"><br>
 < input type = "submit" value = "modify" > < input type = "reset" value = "reset" >
</form>

8. At User.java How to add query information in

public User findUserByName(String username) {
  Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;
  try {
   //Indicates the drivers needed to connect to the database
   Class.forName("oracle.jdbc.driver.OracleDriver");
   //Establish a connection to the database
   con = DriverManager.getConnection(
     "jdbc:oracle:thin:@myserver:1521:mydb", "scott",
     "tiger");
   //Writing SQL statement to query database information
   String sql = "select * from usertable where username='" + username + "'";
   //Create statement objects for executing SQL statements
   stmt = con.createStatement();
   //Execute the statement without result set return. It returns the number of records in the database table
   rs = stmt.executeQuery(sql);
   if(rs.next()){
    User user = new User();
    String userpass = rs.getString(2);
    user.setUsername(username);
    user.setUserpass(userpass);
    return user;
   }   
  } catch (Exception e) {
   return null;
  } finally {
   //Close related objects
   try{
    rs.close();
   }catch(Exception e){}
   if (stmt != null)
    try {
     stmt.close();
    } catch (Exception ee) {
    }
   if (con != null)
    try {
     con.close();
    } catch (Exception ee) {
    }
  }
  return null;
}

Note: the red part needs to be modified to your own server and database.

9. At User.java How to add “modify information” in

public boolean update() {
 Connection con = null;
 Statement stmt = null;
 boolean b;
 try {
  //Indicates the drivers needed to connect to the database
  Class.forName("oracle.jdbc.driver.OracleDriver");
  //Establish a connection to the database
  con = DriverManager.getConnection(
    "jdbc:oracle:thin:@myserver:1521:mydb", "scott",
    "tiger");
  //Writing an updated SQL statement
  String sql = "update usertable set userpass = '"+userpass+"' where username='" + username + "'";
  //Create statement objects for executing SQL statements
  stmt = con.createStatement();
  //Execute the statement without result set return. It returns the number of records in the database table
  int n = stmt.executeUpdate(sql);
  if(n>0)
   b=true;
  else
   b=false;
 } catch (Exception e) {
  b = false;
 } finally {
  if (stmt != null)
   try {
    stmt.close();
   } catch (Exception ee) {
   }
  if (con != null)
   try {
    con.close();
   } catch (Exception ee) {
   }
 }
 return b;
}

Note: the red part needs to be modified to your own server and database.

10. Writing a servlet to query information

The reference code is as follows:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javabean.User;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FindUser 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 {
 //Access to information
 String username = request.getParameter("username");
 //Call Java Bean
 User user = new User();
 user = user.findUserByName(username);
 //Transfer value
 request.setAttribute("user", user);
 //Select interface to respond to users
 RequestDispatcher rd = request.getRequestDispatcher("updateuser.jsp");
 rd.forward(request, response);    
}
}

11. Writing a servlet to modify information

The reference code is as follows:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javabean.User;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UpdateUser 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 {
 //Access to information
 String username = request.getParameter("username");
 String userpass = request.getParameter("userpass");
 //Call Java Bean
 User user = new User();
 user.setUsername(username);
 user.setUserpass(userpass);
 boolean b = user.update();
 //Transfer value
 String info ;
 if(b)
  Info = "modification succeeded! ";
 else
  Info = "modification failed! ";
 //Select interface to respond to users
 RequestDispatcher rd = request.getRequestDispatcher("getAllUser");
 rd.forward(request, response); 
}
}

I hope this article is helpful to our JSP program design.