Examples of JSP deletion and modification functions in MVC mode are described in detail.

Source: Internet
Author: User

Examples of JSP deletion and modification functions in MVC mode are described in detail.

The example in this article describes how to delete and modify JSP functions using the MVC mode. Share it with you for your reference. The details are as follows:

Objectives:

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

Main content:

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

1. How to Use the MVC mode to complete the deletion function

According to the characteristics of the MVC pattern, the three parts of MVC are considered respectively.
① Consider Part V first:

Input: The delete function is usually completed based on the query. Therefore, you can add a delete hyperlink to the user information list interface.

Output: indicates whether the deletion is successful. You can use a separate interface or display it on other pages. We use the second method to display the prompt information on the user list interface.

② Consider the M part. You must add the method to delete a User in User. java.

③ Finally, consider Part C: Get the user name to be deleted, call part M to complete the deletion, and finally go to the user information list interface.

The following are implemented respectively.

2. add and delete superlinks and prompt information to the userlist. jsp file.

1) add or delete a hyperlink (red part ):

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

Note: You need to know the name of the user you want to delete (here the name is used as the primary key), so you can pass the name through the parameter.

2) Add prompt information:
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 "> page 1 </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> </tr> <c: forEach var = "user" items = "$ {users}"> <tr> <td >$ {user. username} </td> <td >$ {user. userpass} </td> <a href = "deleteUser? Username =$ {user. username} "> Delete </a> </td> </tr> </c: forEach> </tbody> </table>

3. Compile M part: add the method in User. java

Add the deleteUser method to User. java. 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. try {// specifies the driver Class required to connect to the database. forName ("oracle. jdbc. driver. oracleDriver "); // establish a connection with the database. con = DriverManager. getConnection ("jdbc: oracle: thin :@ myserver: 1521: mydb", "scott", "tiger "); // compile the SQL statement String SQL = "delete from usertable where username = '" + username + "'"; // create a statement object Execute the SQL statement stmt = con. createStatement (); // execute a statement that does not return a result set. The returned result is the number of records in the database table int n = stmt.exe cuteUpdate (SQL); 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 () ;}catch (Exception ee) {}} return B ;}

Note: The red part must be changed to your own server and database.

4. Write Part C: add the DeleteUser Controller

The DeleteUser. java 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 DeleteUser extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletRe Response se response) throws ServletException, IOException {// obtain information String username = request. getParameter ("username"); // call JavaBean User user = new User (); boolean B = user. deleteUser (username); // transmits the message String info for successful or failed deletion; if (B) info = "deleted successfully! "; Else info =" deletion 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 the MVC pattern to implement the modification function

The modification function consists of two processes: the user selects the user to be modified, and then displays the user information on the modification interface; the user modifies the user, submits the modification, and the server completes the modification function.
For the first function, use the MVC mode:

Part V:

Input: Add a "modify" hyperlink on the user list page.
Output: the user information modification interface displays the queried information on the modification interface.

Part M: compile a method to query user information by user name.

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

For the second part, use the MVC mode:

V

Input: the modification interface written above.
Output: User Information List page

Part M: describes how to modify user information.

Part C: Obtain the user input information, call the M part to complete the modification, and turn to the List interface to respond to the user.

The following describes the implementation methods.

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 "> page 1 </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> </tr> <c: forEach var = "user" items = "$ {users}"> <tr> <td >$ {user. username} </td> <td >$ {user. userpass} </td> <a href = "findUser? Username =$ {user. username} "> modify </a> </td> <a href =" deleteUser? Username =$ {user. username} "> Delete </a> </td> </tr> </c: forEach> </tbody> </table>

7. Edit the Information Modification page

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. Add Query Information in User. java

Public User findUserByName (String username) {Connection con = null; Statement stmt = null; ResultSet rs = null; try {// specifies the driver Class required to connect to the database. forName ("oracle. jdbc. driver. oracleDriver "); // establish a connection with the database. con = DriverManager. getConnection ("jdbc: oracle: thin :@ myserver: 1521: mydb", "scott", "tiger "); // compile the SQL statement String SQL = "select * from usertable where username = '" + username + "'"; // create the statement object, Run the SQL statement stmt = con. createStatement (); // execute a statement that does not return a result set. The returned result is the number of records in the database table rs = stmt.exe cuteQuery (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 the related object 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 must be changed to your own server and database.

9. Add "Modify information" in User. java

Public boolean update () {Connection con = null; Statement stmt = null; boolean B; try {// specifies the driver Class required to connect to the database. forName ("oracle. jdbc. driver. oracleDriver "); // establish a connection with the 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 + "'"; // create a statement object for executing the SQL statement stm T = con. createStatement (); // execute a statement that does not return a result set. The returned result is the number of records in the database table int n = stmt.exe cuteUpdate (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 must be changed to your own server and database.

10. Compile the Servlet for querying 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. extends; 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 {// obtain information String username = request. getParameter ("username"); // call Javabean User user = new User (); user = user. findUserByName (username); // pass the request value. setAttribute ("user", user); // select the interface to respond to the user RequestDispatcher rd = request. getRequestDispatcher ("updateuser. jsp "); rd. forward (request, response );}}

11. Compile the Servlet for Information Modification

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, HttpServletRe Response se response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// get 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 (); // pass the 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 will help you with JSP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.