PetStore Source Code Tracking (3) - Business Logic Processing (1)

zhaozj2021-02-08  396

Author: European Xuan repair

图文 并 版 请 h 请 h版 如何 前 如何 商 如何 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商 商Business logic's processing, first let us first understand why the business logic and information show (Presentation), so we have the following benefits:

1. Reducing the impact of program changes: business logic and data show each other independent, not due to business logic changes, and the procedural code is affected, and vice versa. 2. Easy to maintain: Centralize business logic, once there is a modification, only need to modify one place. 3. Reuse: If the business logic is mixed with the information display layer program, such business logic can only serve a user interface (client), if the two are separated, we can easily expand the second user interface. 4. The designers and commercial logic designers are usually different from the design personnel of information, which makes the operations in specialization, quality and efficiency. In front of the PetStore business logic processing, we will first look at the invisible role in the PetStore. These invisible roles are ready for the server (RI), silently ready, to be properly timed to be commercial logic processing services in PetStore, so The author must introduce them first.

Please open PetStore_home (Note) /SRC /APPS/PETSTORE/SRC/Docroot/web-inf/web.xml, we can see: //1. a Fliter EncodingFilter EncodingFilter no description com.sun.j2ee.blueprints.encodingfilter .web.encodingfilter Encoding utf-8 // 2. Second filter signonfilter Signonfilter no description com.sun.j2ee.blueprints.signon.web.signonfilter // First filter encodingfilter / * // Second Filter Corresponding Signonfilter / * //3. First listnerner com.sun.j2ee.blueprints.petstore.controller.web.petstoreComponentManager <

! - Signon Attribute Listener -> // 4. Second Listerner com.sun.j2ee.blueprints.petStore.Controller.web.signonnotifier Filter and Listener are the functions of servlet2.3. Filter can do some check processing after accepting the user's request. If there is no problem, the RESPONSE required by the user will be lost back to the user, and vice versa. Handling the screen, the most commonly used case is login, some functions in the web application system must be logged in to use, and the past practices should write login checks on these individual features, which will cause login check to fix, must By branch modification, causing time waste, using Filter, we can independent login checkpoints with other programs, easy to maintain in the future. Listener is an increase in Context, Session Lifecycle, for example, when we can initialize in Session, generate the information you want, and save the Reference into session, and see the resource is removed when the resource is turned off, so resources Concentrate control, easy to maintain.

Encoding Filter its position in the program code Petstore_home / src / components / encodingfilter / src / com / sun / j2ee / blueprints / encodingfilter / web / EncodingFilter.java, it will re-read the web.xml (position Petstore_home / src / apps The parameters in /PETSTORE/Src/docroot/web-inf/web.xml determines the encoding method to set it in Request: Web.xml EncodingFilter EncodingFilter no description com.sun.j2ee.blueprints.encodingfilter.web.EncodingFilter // Setting the encoding method parameter Encoding UTF-8 EncodingFilter.javapublic class EncodingFilter implements filter {private FilterConfig config = null; // default to ASCII private String targetEncoding = "ASCII"; read parameter public void init (FilterConfig config) throws ServletException {this.config // initialization time = Config; this.targetencoding = config.getinitParameter ("encoding" );} Public void destroy () {config = null; targetEncoding = null;} // encoding parameters into the reqeust, this end Filter public void doFilter (ServletRequest srequest, ServletResponse sresponse, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) srequest; request.setCharacterEncoding (targetEncoding); // move on to the next chain.doFilter (srequest, sresponse);}} I think this Filter full of useful and can be applied individually in the traditional JSP JavaBeans Web Application, in Tomcat3.2.x,

The preset encoding is UTF-8, so we handle Chinese, must be processed in the program, and the Chinese characters must be transferred from Unicode to BIG5; to Tomcat3.3.x, Tomcat will pre- from OS Set the encoding method, so we don't have to handle itself; after tomcat4.0.x, the situation returns to Tomcat3.2.x, the author develops Web Application to encounter this situation, after three versions of Tomcat, program change Come over, do some white workers, and later, the author can use this filter, you don't need to convert Chinese characters from Unicode to BIG5 before depositing into the database. ComponentManager Listener Second Filter mainly depends on, close relationship with this topic, so that the process is smooth, so later explain. Let's look at the first Listener. Its main function is as a service link provider. When the user enters the system, the application sever will build a storage service connection container when the session is generated, and places it Entering the session and use after the subsequent program service is generated. DefaultComponentManager.java, source code Petstore_home / src / waf / src / controller / com / sun / j2ee / blueprints / waf / controller / webpublic void sessionCreated (HttpSessionEvent se) {HttpSession session = se.getSession (); // generates a service link container sl = ServiceLocator.getInstance (); // present Listener stored Session session.setAttribute (WebKeys.COMPONENT_MANAGER, this);} ServiceLacator.java, source code Petstore_home / src / components / servicelocator / src / com / sun / j2ee / Blueprints / ServiceLocator / Web, this category is full, you can learn more about programming skills, chasing seniors source code, in addition to how to understand how various functions are designed, program code decomposition (refactor), write style , Design skills, it is very important to gain! ServiceLacator basically is an EJB and JMS resource link service provider, and its service does not change due to the user, so it uses Static Writings to achieve this effect.

转载请注明原文地址:https://www.9cbs.com/read-776.html

New Post(0)