JSP training (9)-use the MVC mode to delete and modify features

Source: Internet
Author: User

Objective: l to further understand the MVC mode; L to understand the basic implementation process of the deletion function; and l to understand the basic implementation process of the modification function. Main Content: l use MVC to delete information; l use MVC mode to update information. 1. How to Use the MVC mode to complete the deletion function based on the characteristics of the MVC mode, consider the three parts of MVC. N first consider section V: U input: The delete function is usually completed based on the query. Therefore, you can add a delete hyperchain on the user information list interface. U 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. N. Take m into consideration: You need to add a method for deleting a user in user. java. N 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. In userlist. add or delete a hyperlink and prompt information in the 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 a prompt: <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. Write the M part: in user. add methods to Java in user. add the deleteuser method to 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: 15 21: mydb "," Scott "," Tiger "); // compile the SQL statement string SQL = "delete from usertable where username = '" + username + "'"; // create a statement object, used to 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 deleteuser. the 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 Vo Id doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// obtain information string username = request. getparameter ("username"); // call JavaBean 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 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 the user, after the modification, submit the modification to the server. For the first function, use the MVC mode: l v Part: N input: add the "modify" hyperchain on the user list interface. N output: the user information modification interface displays the queried information on the modification interface. L m: Write a method to query user information by user name. L 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: l v Part N input: the modification interface written above. N output: User Information List Interface part l m: describes how to modify user information. L Part C: obtains the user input information, calls the M part to complete the modification, and redirects to the list interface to respond to the user. The following describes the implementation methods. 6. The modified code on the user information list page 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. The reference code for compiling the information modification interface 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 typ E = "reset" value = "reset"> </form> 8. method for adding Query Information in 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 for querying database information string SQL = "select * from user Table where username = '"+ username +"' "; // create a statement object to 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 rs = stmt.exe cutequery (SQL); If (RS. next () {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. In user. method for adding "Modify information" in 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 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 {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. The servlet reference code for compiling the query information 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 (httpservletreq Uest 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 = 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. The servlet reference code for editing information 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 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 {// obtain information string username = request. getparameter ("u Sername "); string userpass = request. getparameter ("userpass"); // call JavaBean 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 );}}

 

Reference material: Basic tutorial on Java Web Programming

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.