JSP uses MVC mode to complete deletion and modification of functional examples
- 2021-08-12 03:14:59
- OfStack
This article illustrates how JSP uses the MVC pattern to complete the deletion and modification functions. Share it for your reference. The details are as follows:
Objectives:
① Take one step to understand MVC mode;
Grasp the basic realization process of deletion function;
③ Master the basic realization 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 the deletion function
According to the characteristics of MVC mode, three parts of MVC are considered respectively.
① First consider the V part:
Input: Usually, the deletion function is completed on the basis of query, so the deletion hyperlink can be added on the user information list interface.
Output: Prompts the user whether the deletion was successful, which 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.
② Secondly, consider M: You need to add a method to delete users in User. java.
③ Finally, consider the C part: get the user name to be deleted by the user, then call the M part to complete the deletion, and finally turn to the user information list interface.
The following are implemented separately.
2. Add delete hyperlink and prompt information in userlist. jsp file
1) Add and delete hyperlinks (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 (where the name is the primary key), so pass the name by parameter.
2) Add prompt information:
<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"> No. 1 1 Page </a>
<a href="getAllUser?pageNo=${pageNo-1}"> Upper 1 Page </a>
</c:if>
<c:if test="${pageNo!=pageCounter}">
<a href="getAllUser?pageNo=${pageNo+1}"> Under 1 Page </a>
<a href="getAllUser?pageNo=${pageCounter}"> Finally 1 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. Writing the M section: Adding methods to User. java
Add the deleteUser method to User. java with the following reference code:
public boolean deleteUser(String username) {
Connection con = null;
Statement stmt = null;
boolean b; // Indicates whether the deletion was successful or not
try {
// Indicate the drivers needed to connect to the database
Class.forName("oracle.jdbc.driver.OracleDriver");
// Establish a connection with the database
con = DriverManager.getConnection(
"jdbc:oracle:thin:@myserver:1521:mydb", "scott",
"tiger");
// Object for querying database information SQL Statement
String sql = "delete from usertable where username='" + username + "'";
// Creates a statement object for executing SQL Statement
stmt = con.createStatement();
// Execute a statement that returns no result set, which returns the number of records in the affected 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 the C section: Add an DeleteUser controller
The code for DeleteUser. java 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 {
// Getting information
String username = request.getParameter("username");
// Call JavaBean
User user = new User();
boolean b = user.deleteUser(username);
// Passing information about success or failure of deletion
String info;
if(b)
info = " Delete successful! ";
else
info = " Delete failed! ";
request.setAttribute("deleteinfo",info);
// Select the interface to respond to the 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 be modified, and then displays the user's information on the modification interface; The user modifies, submits after modification, and the server completes the modification function.
For the first function, use MVC mode:
V section:
Input: Add "Modify" hyperlink to the user list interface.
Output: User information modification interface, and display the inquired information on the modification interface.
M section: Write a method for querying user information by user name.
C Part: Get the name of the user to delete, call the M part of the query method, get the user information, transfer to the modification interface, use the modification interface to respond to the user.
For Part 2, use the MVC schema:
V Part
Input: The modified interface written above.
Output: User information list interface
M section: Method of writing user information modification.
C Part: Obtain the information input by the user, call M Part to complete the modification, turn to the list interface to respond to the user.
The implementation methods are described below.
6. Modify the 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"> No. 1 1 Page </a>
<a href="getAllUser?pageNo=${pageNo-1}"> Upper 1 Page </a>
</c:if>
<c:if test="${pageNo!=pageCounter}">
<a href="getAllUser?pageNo=${pageNo+1}"> Under 1 Page </a>
<a href="getAllUser?pageNo=${pageCounter}"> Finally 1 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. Write the 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. Method of adding query information in User. java
public User findUserByName(String username) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Indicate the drivers needed to connect to the database
Class.forName("oracle.jdbc.driver.OracleDriver");
// Establish a connection with the database
con = DriverManager.getConnection(
"jdbc:oracle:thin:@myserver:1521:mydb", "scott",
"tiger");
// Object for querying database information SQL Statement
String sql = "select * from usertable where username='" + username + "'";
// Creates a statement object for executing SQL Statement
stmt = con.createStatement();
// Execute a statement that returns no result set, which returns the number of records in the affected 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. Method of adding "modification information" in User. java
public boolean update() {
Connection con = null;
Statement stmt = null;
boolean b;
try {
// Indicate the drivers needed to connect to the database
Class.forName("oracle.jdbc.driver.OracleDriver");
// Establish a connection with the database
con = DriverManager.getConnection(
"jdbc:oracle:thin:@myserver:1521:mydb", "scott",
"tiger");
// Write an updated sQL Statement
String sql = "update usertable set userpass = '"+userpass+"' where username='" + username + "'";
// Creates a statement object for executing SQL Statement
stmt = con.createStatement();
// Execute a statement that returns no result set, which returns the number of records in the affected 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. Servlet for writing 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 {
// Getting information
String username = request.getParameter("username");
// Call Javabean
User user = new User();
user = user.findUserByName(username);
// Passing value
request.setAttribute("user", user);
// Select the interface to respond to the user
RequestDispatcher rd = request.getRequestDispatcher("updateuser.jsp");
rd.forward(request, response);
}
}
11. Servlet for writing modification 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 {
// Getting information
String username = request.getParameter("username");
String userpass = request.getParameter("userpass");
// Call Javabean
User user = new User();
user.setUsername(username);
user.setUserpass(userpass);
boolean b = user.update();
// Passing value
String info ;
if(b)
info=" Modified successfully! ";
else
info=" Modification failed! ";
// Select the interface to respond to the user
RequestDispatcher rd = request.getRequestDispatcher("getAllUser");
rd.forward(request, response);
}
}
I hope this article is helpful to everyone's JSP programming.