Analysis of JSP Design Mode

Source: Internet
Author: User

◆Introduction to JSP Design Patterns

I remember that when I was a beginner in JSP, I always liked to compare it with ASP and PHP, And I was used to writing JSP in Asp development mode. Later I found this was a silly practice, in fact, JSP has already used the MVC mode when it comes out. Below I will briefly talk about JSP design using MVC for design.

◆Introduction to MVC in JSP Design Patterns

MVC is the abbreviation of model, view, and control. That is to say, when using JSP, there are corresponding files for corresponding operations.

Generally, JSP is only responsible for viewing, that is, displaying pages only. The business logic is implemented by bean (ejb. The following describes implementation without using ejb. If ejb is used, the ejb is responsible for M. C which is usually implemented by servlet. Or use struts.

◆Design Concept of JSP Design Mode

When you build an Application. You must consider the interface problems, and the interface modification is very common. If you implement all the operations in Jsp, it will be too much trouble to modify the interface. If the artist does not understand JSP, you must modify countless files by yourself. The header should not be too large, but the time is usually very tight. using MVC can reduce your troubles. During the design, Jsp only displays the page. That is to say, Jsp calls the data transmitted by Bean (Struts, servlet) and then displays the data required by JSP (Struts, servlet, use ArrayList (arttivity) to pass to JSP. If a form needs to be submitted, it is generally submitted directly to Struts and servlet. After processing, the processing information will be returned. The corresponding business logic is implemented by Bean.

◆Bean design in JSP Design Mode

When using Bean, there are usually three types of beans in the manager, entry, and database directories.

The bean in the manager performs business logic

The bean under the entry is the encapsulation of data. In fact, each database table has a bean. Jsp also obtains all classes.

The bean in the database is the database for operations, such as insert, update, delete, load (query a record), and batchload (query multiple records ).

The relationship between them is that the entry is responsible for encapsulating data and returning results as database call parameters.

The result of the manager calling the database. Manager communicates with Jsp. JSP results are returned from the manager. JSP requires operations to call the manager. Even if an insert statement exists in the database, you still need to encapsulate it again in the manager. The purpose is to make the structure as simple as possible. Database is only responsible for database operations. The manager only uses the logic to fetch the corresponding data) to process the corresponding logic, while the entry only encapsulates the data obtained from the database, or encapsulates the parameters obtained from the page as parameters and passes them to the corresponding Bean.

◆Design Example of JSP Design Mode

I will discuss the message board as an example below:

 
  1. Entry/Guestbook. java (message board object)
  2.  
  3. Database/GuestbookMap. java (Update, delete, modify Message Board)
  4.  
  5. Manager/GuestbookManager. java (process all transactions)
  6.  
  7. Data Table Structure (postgresql)
  8.  
  9. Create sequence seq_guestbook increment1;
  10. /** Serial number generator **/ 
  11. Create table guestbook (
  12. Id int8DefaultNextval ('Seq _ guestbook'),/** Primary key **/ 
  13. Title varchar (64),/** Topic **/ 
  14. Body text,/** Content **/ 
  15. Sayid int8,/** Spokesman **/ 
  16. Toid int8,/** Receiver **/ 
  17. Saytime datetimeDefaultNow (),/** Message time **/ 
  18. Newflg smallintDefault 1/** Check **/ 
  19. );
  20. Guestbook. java
  21. ======================================
  22. ImportJava. util .*;
  23. Public ClassGuestbook (){
  24. Private IntId;
  25. PrivateString title;
  26. PrivateBody title;
  27. Private IntSayid;
  28. Private IntSayid;
  29. PrivateDate saytime;
  30. Private ShortNewflg;
  31. PublicGuestbook (){
  32. }
  33. Public IntGetId (){
  34. Return This. Id;
  35. }
  36. Public VoidSetId (Int_ Id ){
  37. This. Id = _ id;
  38. }
  39. ........
  40. (All get/set methods)
  41. }
  42. GuestbookMap. java
  43. ====================================
  44. ImportGuestbook;
  45. Public ClassGuestbookMap (){
  46. PublicGuestbookMap (){
  47. }
  48. PublicGuestbook load (IntId ){
  49. File:// Obtain a guestbook 
  50. }
  51. File:// Sqlstr query Conditions 
  52. File:// Orderstr sorting Condition 
  53. File:// The rcdbegin record starts 
  54. File:// The rcdend Record ends. 
  55. // 
  56. PublicArrayList batchLoad (String sqlstr, String orderstr,IntRcdbegin,IntRcdend ){
  57. File:// The ArrayList encapsulates the Guestbook 
  58. }
  59. Public VoidInsert (Guestbook info ){
  60. }
  61. Public VoidUpdate (Guestbook info ){
  62. }
  63. Public VoidDelete (IntId ){
  64. File:// Obtain a guestbook 
  65. }
  66. Public IntGetRcdNums (String sqlstr ){
  67. File:// Retrieve the number of records 
  68. }
  69. }
  70. GuestbookManager. java

Encapsulate the required methods as needed. This part is what you want to write.

In the preceding method, the database file can be automatically generated. What you need to write is the method in GuestbookManager. You may think that the workload is larger than all your operations in JSP, but the structure is very clear. You also need to write a database connection pool. All database operations are taken from one place, and it is costly to connect to the database every time.

  1. A brief overview of embedded objects in JSP
  2. Functions and principles of several encodings in JSP and Servlet
  3. Use JSP include mechanism to improve appearance
  4. Six common issues for JSP Programming
  5. JSP tag library Parsing

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.