Servlets and JSP Pages Best Practices

Source: Internet
Author: User

Java Servlet technology and jsp (the preferred choice for SUN enterprise-level applications) technology enable Java Server technology. Currently, they control the entire server-side Java technology market and gradually become the standard for building commercial Web applications. Java developers like these technologies for many reasons, including: these technologies are easy to learn, one-time writing, and Run everywhere (Write Once, Run Anywhere ). More importantly, if the following practices are adopted more efficiently, Servlet and jsp (the preferred choice for SUN Enterprise Applications) can help separate Web representations and content. "Best practices" are proven to be a good way to develop high-quality, reusable, and easily maintained Web applications based on Servlet and jsp (the preferred choice for SUN Enterprise applications. Correspondingly, Java code is mixed in HTML, which can easily produce complicated applications that are inefficient, reusable, and difficult to maintain. Best practices will change these drawbacks.

This article will describe the importance of the Best Practices prepared by Servlets and jsp (the preferred choice for SUN Enterprise Applications). Here we assume that the reader has understood the basic working principles of the two. This article covers the following content:

  • This section briefly introduces Java Servlet and JavaServer Pages (jsp (preferred for SUN Enterprise applications )).
  • Provides tips, tips, and rules for developing Servlets and jsp (the preferred choice for SUN Enterprise applications.
  • Provides best practices for Servlet and jsp (preferred for SUN Enterprise applications.
Overview of Servlet and jsp (preferred for SUN Enterprise Applications) Pages

Similar to the Common Gateway Interface (CGI) script, servlets supports the request response programming mode. When a client sends a request to the server, the server sends the request to the servlet. Then, the servlet constructs a response, and the server sends the response back to the client. However, unlike CGI scripts, servlets and HTTP servers run in the same process.

When sending a client request, call the service method and pass a request and response object. Servlet first checks whether the request is a GET operation or a POST operation. Then it calls the following method: doGet or doPost. If the request is GET, the doGet method is called. If the request is POST, The doPost method is called. Both doGet and doPost accept requests (HttpServletRequest) and responses (HttpServletResponse ).

In short, servlets is a Java class that can use print statements to generate dynamic HTML content. However, it must be mentioned that servlets runs in a container and APIs manages the session and object lifecycle. Therefore, when using servlets, you can gain all the advantages of the Java platform, including sandbox (Security), database access API through JDBC, and Server Load balancer with cross-platform portability.

Java Server Pages (jsp (preferred for SUN Enterprise Applications ))

Jsp is a high-level abstraction of Servlet technology. It is a technology developed and open by Sun, similar to Microsoft's ASP dynamic web page technology, and is a key component of Java2 Enterprise Edition (J2EE. Currently, many commercial application servers (such as BEA WebLogic, IBM WebSphere, Live JRun, Orion, and so on) Support jsp (the preferred choice for SUN Enterprise applications ).
 

Jsp (preferred for SUN Enterprise applications)How does a page work?

Jsp (preferred for SUN Enterprise Applications) pages are actually Web pages with traditional HTML and Java code. The extension tells the server that the page needs special processing. The special processing must be implemented by the server extension or plug-in.

When a jsp (preferred for SUN Enterprise Applications) page is read, it is first compiled into a Servlet (jsp (preferred for SUN Enterprise Applications) engine. At this time, this Servlet is handed over to the Servlet engine for processing like other servlets. Then the Servlet engine reads the corresponding Servlet class (using ClassLoader) and runs it to generate a dynamic HTML page (figure 1 ). This Servlet creates some necessary components, and then writes these components as a string to the output stream (OutputStream) and displays them in the browser.
When calling a jsp (preferred for SUN Enterprise Applications) page, it is first compiled into a Java servlet (using the jsp (preferred for SUN Enterprise Applications) engine. In this case, the servlet engine processes the servlet just like any other servlet. Then, the servlet engine loads the servlet class (using the Class Loader) and executes it to create dynamic HTML and send it to the browser, as shown in 1. The Servlet creates all required objects, writes all objects to the output stream as strings, and displays them in the browser.


Figure1:CallJsp (preferred for SUN Enterprise applications)Page request/Response Flowchart

When you request this page next time, the jsp (preferred for SUN Enterprise Applications) engine executes a servlet that has been loaded long ago unless the jsp (preferred for SUN Enterprise Applications) page has been changed for a long time. In this case, it is automatically re-compiled into a servlet and executed.

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.