Servlets and JSP development principles (II)

Source: Internet
Author: User

Servlets and jsp (preferred for SUN Enterprise Applications) Development Principles (II)

  • Do not re-invent the wheel. Do not start from scratch: Custom components can improve reusability, but custom components still need to write, test, and Debug programs. The problem is that someone else may have done this, and your implementation method is not necessarily better than others. This isJsp (preferred for SUN Enterprise applications)Standard tag Library (Assumerver Pages Standard Tag Library, JSTL) To do (JSTLSeeJSTLOfficial Website).JSTLProvides various tags such as loops, read attributes, traversal of various data structures, and evaluation of conditional expressions. It also provides some complex tags, even like ParsingXml (standardization is getting closer and closer)Document tags. So if you want to use a tag, you 'd better first check whether there are any other ones that can be used, rather than starting from scratch.
  • UseJSTLExpress language (JSTL Expression Language): PassJsp (preferred for SUN Enterprise applications)Page data generally passes throughJsp (preferred for SUN Enterprise applications)Scope attribute or request parameter. The Expression Language (Expression Language, ELUses the scope attribute to pass information from the business logicJsp (preferred for SUN Enterprise applications)The standard method for transferring page information. Note that,ELJustJsp (preferred for SUN Enterprise applications)A key aspect of technology is not a general programming language. On the contrary, it is only a data access language, which can simplify the access to application dataScriptletAnd the request expression to access the data.

    InJsp (preferred for SUN Enterprise applications), The web designer needs to use expression syntax<% = Name %>OrJavaBeanComponent to obtain the values of certain variables or attributes, for example:

<TagLib: tag attribute = "<% =

PageContext. getAttribute ("name") %> ">

 

Or

 

<% = ACustomerBean. getAddress (). getCountry () %>

Expressions enable web designers to access information using simplified syntax. If you only want to access a simple variable, you can use the following syntax:

 

<TagLib: tag attribute = "$ {name}">

If you want to access a nestedJavaBeanYou can:

 

<TagLib: tag attribute = "$ {

ACustomerBean. address. country} ">

Expression Language (EL) BorrowedJavaScriptSo if youJavaScriptIf you are familiar with it, you will feel great.

  • Use a filter (Filter): A filter is an object that can transmit requests or modify responses. It can beServlet/jsp (preferred for SUN Enterprise applications)It is pre-processed before and can be removed in responseServlet/jsp (preferred for SUN Enterprise applications)And then perform post-processing. So if you have a fewServlet/jsp (preferred for SUN Enterprise applications)To perform the same data conversion or page processing, you can write a filter class, and then deploy the description file (Web. xml (standardization is getting closer and closer)).Servlet/jsp (preferred for SUN Enterprise applications)Contact.

It is easy to create a filter.Javax. servlet. FilterInterface and its three methods:

Public void init (FilterConfig config)

Public void doFilter (ServletRequest req, ServletResponse rep,

FilterChain chain)

Public void destroy ()

In this way, you can complete your filter.

  • Use a portable Security Model: Most application servers provide security models, but they are generally proprietary to a server or a vendor. If your application needs to be transplanted, it is best to use a portable security model for your application. If your application has some predefined fixed users, you can useFROMVerify andBASICVerify. However, if you want to dynamically generate a customer (this is usually the case), you may need to use the server-specificAPICreate and manage users. In this way, when your application is transplanted to another server, you may encounterAPIIncompatibility issues. In this case, the best solution is to use the adapter (Adapter) Mode (if you are not familiar with the design mode, seeGoFDesign Patterns ).
  • Use a database to save persistent data: Servlet/jsp (preferred for SUN Enterprise applications)Available inHttpSessionThe Session object to save the user's temporary data. However, if you want to save persistent data, you should use the database to save the data more securely, and there is no requirement on the browser used by the customer. In this way, even if your application server crashes for some reason, your data is still good.

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.