The example in this article describes how JSP uses the MVC pattern to complete the deletion and modification functions. Share to everyone for your reference. Specifically as follows:
Goal:
① further understand the MVC pattern;
② Master the basic realization process of deletion function;
③ Master the basic realization process of modifying function.
Main content:
① use MVC to complete the deletion function;
② uses the MVC pattern to complete the information update function.
1, how to use the MVC pattern to complete the deletion function
According to the characteristics of MVC pattern, 3 parts of MVC are considered separately.
① first consider the V section:
Input: Usually the deletion function is done on the basis of the query, so you can add the deleted hyperlinks on the user Information list interface.
Output: Prompts the user to delete the success, can use a separate interface, can also be displayed in other pages. We use the second way to display the prompts in the user list interface.
② Second consider the M section: you need to add a way to remove users in User.java.
③ finally consider the C section: Get the user name to delete, and then call M to complete the deletion, and finally to the user Information list interface.
The following are implemented separately.
2. Add delete hyperlink and hint information in userlist.jsp 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 you delete, you need to know the name of the user you want to delete (the name is the primary key), so pass the name by the parameter.
2) to add a hint message:
Copy Code code 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} "> Prev </a> </c:if> <c:if test=" ${pageno!=pagecounter} "> <a href="
Getalluser?pageno=${pageno+1} "> Next </a> <a href=" Getalluser?pageno=${pagecounter} "> last page </a> </c:if> <br> <table width= "$" border= "1" height= "a" > <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.userpa SS} </td> <td> <a href= "Deleteuser?usErname=${user.username} "> Delete </a> </td> </tr> </c:forEach> </tbody> </table>
;
3. Write M: Add a method to the User.java
Add the DeleteUser method to the User.java and the reference code is as follows:
public boolean deleteuser (String username) {
Connection con = null;
Statement stmt = null;
Boolean B; Indicates whether the delete succeeds
try {
//indicates the driver required to connect to the database
class.forname ("Oracle.jdbc.driver.OracleDriver");
Establish a connection to a database
con = drivermanager.getconnection (
"Jdbc:oracle:thin: @myserver: 1521:mydb", "Scott",
" Tiger ");
SQL statement to write query database information
String sql = "Delete from usertable where username= '" + username + "'";
Creates a statement object that executes the SQL statement
stmt = Con.createstatement ();
Executes a statement that does not return a result set, and returns the number of
int n = stmt.executeupdate (SQL) that affects the records in the database table;
if (n > 0)
b = true;
else
B = false;
} catch (Exception e) {
b = false;
} finally {
//Close Related object
if (stmt!= null)
try {
stmt.close ();
} catch (Exception ee) {
}
if (con!= null)
try {
con.close (); c33/>} catch (Exception ee) {
}
return
b;
}
Note: The red section needs to be modified to your own server and database.
4, Write C part: Add 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 {//Get information String username = request.getparameter ("username");
Call JavaBean User user = new user ();
Boolean B = User.deleteuser (username);
Pass Delete successful or failed information String info; if (b) info = "Delete succeeded!"
"; else info = "Delete failed!"
";
Request.setattribute ("Deleteinfo", info);
Select interface to user response 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 the MVC pattern to implement the modification function
The modification feature itself consists of two processes: the user selects the user to be modified, and then the user's information is displayed in the Modify interface; The user modifies, modifies and submits, and the server completes the modification function.
For the first feature, use the MVC pattern:
V section:
Enter: Add a "modify" hyperlink to the user list interface.
Output: User information to modify the interface, the query to the information displayed in the Modify interface.
M: Write a method for querying user information by user name.
Part C: Get the name of the user to delete, call the M part of the query method, get the user information, pass to modify the interface, use modify interface to user response.
For the second part, the MVC pattern is used:
V Part
Input: The modified interface written above.
Output: User Information List interface
M: Write the user information modification method.
Part C: Get the user input information, call the 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 "> First page </a> <a href=" getalluser?pageno=${pageno-1} "> Prev </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=" "border=" 1 "height=" a "> <tbody> <tr> <td> user name </td> <td> password </td> <td> </td> &
lt;/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 information to modify the interface
The reference code is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%> Modify user <br>
<form name= "Form1" method= "POST" action= "UpdateUser" >
username: ${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, the method of adding query information in User.java
Public User Finduserbyname (String username) {Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {//indicates the driver Class.forName ("Oracle.jdbc.driver.OracleDriver") required to connect to the database;
Establish a connection to the database con = drivermanager.getconnection ("Jdbc:oracle:thin: @myserver: 1521:mydb", "Scott", "Tiger");
SQL statement to write query database information String sql = "SELECT * from usertable where username= '" + username + "'";
Creates a statement object that executes the SQL statement stmt = con.createstatement ();
Executes a statement that does not return a result set, and returns the number of records that affect 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;
The catch (Exception e) {return null;
Finally {//close related objects try{rs.close ();
}catch (Exception e) {} if (stmt!= null) try {stmt.close ();
The catch (Exception ee) {} if (con!= null) try {con.close (); catch (Exception Ee) {}} return null;
}
Note: The red section needs to be modified to your own server and database.
9, add "Modify Information" method in User.java
public boolean update () {
Connection con = null;
Statement stmt = null;
Boolean B;
try {
//indicates the driver
class.forname ("Oracle.jdbc.driver.OracleDriver") required to connect to the database;
Establish a connection to a database
con = drivermanager.getconnection (
"Jdbc:oracle:thin: @myserver: 1521:mydb", "Scott",
" Tiger ");
Write the updated SQL statement
String sql = "Update usertable set userpass = ' +userpass+" ' where username= ' + username + "'";
Creates a statement object that executes the SQL statement
stmt = Con.createstatement ();
Executes a statement that does not return a result set, and returns the number of
int n = stmt.executeupdate (SQL) that affects the records in the database table;
if (n>0)
b=true;
else
b=false;
} catch (Exception e) {
b = false;
} finally {
if (stmt!= null)
try {
St Mt.close ();
} catch (Exception ee) {
}
if (con!= null)
try {
con.close ();
} catch (Exception ee) {
}
} return
B;
}
Note: The red section needs to be modified to your own server and database.
10, write the query information servlet
The reference code is as follows:
package servlet; import java.io.IOException; import java.io.PrintWriter; Import Javabea
N.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, Httpse Rvletresponse response) throws Servletexception, IOException {//Get information String username = request.getparameter ("Usernam
E ");
Call JavaBean User user = new user ();
user = User.finduserbyname (username);
Pass value Request.setattribute ("User", user);
Select the interface to respond to the user RequestDispatcher Rd = request.getrequestdispatcher ("updateuser.jsp");
Rd.forward (request, response); }
}
11, write the change of information servlet
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, Http Servletresponse response) throws Servletexception, IOException {//Get information String username = request.getparameter ("Usern
Ame ");
String Userpass = Request.getparameter ("Userpass");
Call JavaBean User user = new user ();
User.setusername (username);
User.setuserpass (Userpass);
Boolean B = User.update ();
Pass value String info; if (b) info= "Modified successfully!"
"; else info= "Modify failed!"
"; Select an interface to respond to the user RequestDispatcher Rd = request.getrequestdispatcher ("GetalLuser ");
Rd.forward (request, response);
}
}
I hope this article will help you with your JSP programming.