JSP Design Mode

Source: Internet
Author: User

JSP Design Mode

I. Introduction
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 out that this is really a silly practice. In fact, JSP has already used the MVC mode when it comes out. Let me give a brief introduction.
JSP design uses MVC design.

II. Introduction to MVC
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. Which of the following is
It is implemented without using EJB. If EJB is used, the EJB is responsible for M. C which is usually implemented by servlet. Or use struts.
For details about struts, visit http://jakarta.apache.org/struts. I will introduce it in future articles.

Iii. Design Ideas
When you build an application. You must consider the interface problems, and the interface modification is very common. If you are
All the operations in JSP are implemented. Once you modify the interface, you are in great trouble. If the artist does not understand JSP, you must modify countless
File, the header should not be too big, and the time is usually very tight at this time, 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
Bean (struts, Servlet) collects the data required by JSP and transmits it to JSP using arraylist (arttivity. If you need
Submit a form. Generally, it is submitted directly to struts and Servlet. After processing, it returns the processing information.
The corresponding business logic is implemented by bean.

Iv. Bean Design
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 (querying a record ),
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 obtained from the manager. jsp needs to do
Operation Call Manager. Even if an insert statement exists in the database, you still need to encapsulate it in the Manager.
Once. The purpose is to make the structure as simple as possible. Database is only responsible for database operations. Manager only performs logic (fetch corresponding data)
The corresponding logic is processed, while the entry only encapsulates the data obtained from the database, or encapsulates the parameters obtained from the page as parameters.
To the corresponding bean.

V. design instances
I will discuss the message board as an example below:
Entry/guestbook. Java (message board object)
Database/guestbookmap. Java (Update, delete, modify Message Board)
Manager/guestbookmanager. Java (process all transactions)
Data Table Structure (PostgreSQL)
Create sequence seq_guestbook increment 1;
/** Serial number generator **/
Create Table guestbook (
Id int8 default nextval ('seq_guestbook '),/** primary key **/
Title varchar (64),/** topic **/
Body Text,/** content **/
Sayid int8,/** speaker **/
Toid int8,/** receiver **/
Saytime datetime default now (),/** message time **/
Newflg smallint default 1/** check **/
);

Guestbook. Java
======================================
Import java. util .*;
Public class guestbook (){
Private int ID;
Private String title;
Private body title;
Private int Sayid;
Private int Sayid;
Private date saytime;
Private short newflg;

Public guestbook (){

}

Public int GETID (){
Return this. ID;
}

Public void setid (INT _ id ){
This. ID = _ id;
}
........
(All get/set methods)
}

Guestbookmap. Java
====================================
Import guestbook;
Public class guestbookmap (){
Public guestbookmap (){

}

Public guestbook load (int id ){
// Obtain a guestbook
}
// Sqlstr query Conditions
// Orderstr sorting Condition
// The rcdbegin record starts
// The rcdend Record ends.
//
Public arraylist batchload (string sqlstr, string orderstr, int rcdbegin, int rcdend ){
// The arraylist encapsulates the guestbook
}

Public void insert (guestbook info ){

}

Public void Update (guestbook info ){

}

Public void Delete (int id ){
// Obtain a guestbook
}

Public int getrcdnums (string sqlstr ){
// Retrieve the number of records
}
}

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. I have developed this tool. Contact me if necessary.
What you need to write is the method in guestbookmanager. You may feel that the workload is more than all your operations in JSP.
Large, but the structure is very clear. You still need to write a database connection pool, and all your database operations are from one location
It is costly to connect to the database every time.

Vi. Technical advantages
1: clear structure
2: easy to maintain
3: the Protection Code is better.
..........
7. Conclusion
I am just a brief introduction. You need to accumulate in practical use. If you need to refer to my code, contact me,
If you think you are writing and shit, you are very embarrassed and wasted your precious time.

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.