Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the JSP design pattern

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the JSP design pattern, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Introduction to ◆ JSP Design pattern

I remember that when I first learned JSP, I always liked to compare him with ASP,PHP, and I was used to using Asp's development mode to write JSP. Later, I found that it was really silly to do so. In fact, JSP has already used MVC mode since it came out. Let me briefly talk about JSP design using MVC to design.

Introduction to MVC in ◆ JSP Design pattern

MVC is actually an abbreviation for module, view and control, that is to say, when using JSP, there are corresponding files to implement the corresponding operations.

Usually JSP is only responsible for view, that is, only for displaying pages. Business logic is implemented by bean (ejb). What is discussed below is the implementation without using ejb. If you use ejb,ejb to take charge of M.C, it is usually implemented by servlet. Or use struts.

Design idea of ◆ JSP Design pattern

When you build an Application. You have to think about the interface, and interface modification is very common. If you implement all the operations in Jsp, you will be in big trouble once you modify the interface. Artists do not understand JSP, you have to modify countless files, the head is not too big, and at this time the general time is very tight, the use of MVC can reduce some of your trouble. At design time, Jsp is only responsible for displaying the page, that is, Jsp calls the data from Bean (Struts,servlet) and then displays it, while Bean (Struts,servlet) is responsible for collecting the data needed by JSP and passing it to JSP with ArrayList (Arttibute). If you need to submit a form, you usually submit it directly to Struts,servlet, and then return the processing information after processing. The corresponding business logic is implemented by Bean.

The Design of Bean in ◆ JSP Design pattern

There are usually three types of Bean when I use them, which are under the manager,entry,database directory.

The bean under manager does business logic.

Bean under entry encapsulates data, which means that there is a bean corresponding to each database table. All Jsp gets are classes, too.

Bean under database is to operate the database, such as insert,update,delete,load (query one record) and batchload (query multiple records).

The relationship between them is that entry is responsible for encapsulating the data and returning the result as a call parameter to database.

Manager calls the result of database processing. Manager communicates with Jsp. The result of JSP comes from manager, JSP needs to do an operation to call manager, even if an insert has such a method in database, you still need to package it again in manager. The aim is to make the structure as simple as possible. Database is only responsible for operating the database. Manager only does logic (taking the corresponding data) to deal with the corresponding logic, while entry only encapsulates the data obtained by database, or encapsulates the parameters obtained from the page as parameters to the corresponding Bean.

A design example of ◆ JSP design pattern

Let me take the message board as an example to discuss:

Entry/Guestbook.java (message board object) database/GuestbookMap.java (update, delete, modify message board) manager/GuestbookManager.java (handle all transactions) data table structure (postgresql) create sequence seq_guestbook increment 1 / * * Serial number generator * * / create table guestbook (id int8 default nextval ('seq_guestbook'), / * * key * * / title varchar (64), / * topic * * / body text,/** content * * / sayid int8,/** spokesperson * * / toid int8,/** recipient * * / saytime datetime default now (), / * message time * / newflg smallint default 1 newflg smallint default * have you checked * * /) 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) {file:// fetches a guestbook} file://sqlstr query condition file://orderstr sort condition file://rcdbegin record start file://rcdend record end / / public ArrayList batchLoad (String sqlstr,String orderstr,int rcdbegin Int rcdend) {file://ArrayList encapsulates Guestbook} public void insert (Guestbook info) {} public void update (Guestbook info) {} public void delete (int id) {file:// to fetch a guestbook} public int getRcdNums (String sqlstr) {file:// fetch number of records}} GuestbookManager.java

Encapsulate the required method according to the need, this is the part you want to write

In the above way, entry,database files can be generated automatically. What you need to write is the method in GuestbookManager, you may feel that the workload is greater than all your operations in JSP, but the structure is very clear. What is also needed is to write a database connection pool, where all database operations are taken from one place, and it is expensive to connect to the database every time.

The above is all the contents of the article "how to use JSP Design patterns". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report