Pure servlet: Reconsider view

Source: Internet
Author: User
Tags java format

The purpose of designing JSPs is to separate the tasks of Web developers from the tasks of non-developers who design dynamic page UIs. Unfortunately, JSP is too complex for many designers and it is tricky to add a layer of software to solve a variety of dynamic content issues. (for example, internationalization requires that the text be stored elsewhere and referenced by a key.) So for most projects, Java developers have to deal with JSP code themselves, which often includes the work of the designer, which consumes their energy on tag libraries and other things and cannot focus on Java code.

Unlike the orthodox approach, you can use simple helper objects to build a simple, graceful Web interface based on a regular servlet. This article explains how to write the view output of a dynamic Web page in a standard Java format. I will explain the benefits of this approach and demonstrate this approach with a scoring application that manages an NCAA March Frenzy bonus pool.

HTML is dynamic

This pure servlet approach is simple. It involves a servlet base class and a custom writer object that the servlet subclass uses to produce output. The code is concise because most of the HTML is encapsulated in the helper object's methods and can be overridden on demand. Code reuse is always delightful, and most Web site pages share a lot of HTML, so reuse should be an important consideration. The HTML output method produces intuitive, compact servlet code, so maintainability is very high, which makes the maintenance cost of the code almost directly proportional to the size of the code. By rewriting the JSP interface into a pure servlet, you can reduce your code by two-thirds.

For example, to output a link based on user rights, you need the following lengthy construction code:

<c:if test="${user.permission[ sessionScope.ConstantMap[ EDIT_WIDGET ] ] != 0}">
  <c:url var="editUrl" value="/EditWidget.jsp"/>
  <div class="navigation"><a href="<c:out value="${editUrl}"/>">Edit
     this widget</a></div>
</c:if>

By using Java syntax, the code is much simpler:

if (user.getPermission(Constants.EDIT_WIDGET) != 0)
  out.printNavlinkDIV("/EditWidget.jsp", "Edit this widget");

Also, it can save a lot of code by getting and exporting business objects in the same place rather than passing them through the request object. Simplicity is beauty.

Using JSP and other view technologies may be the most frustrating part of WEB development. JSP pages are not HTML or XML, Java code, JavaServer pages Standard Tag Library (JSTL) code or expression language (EL), but a hodgepodge of these things. JSP code is not only a strange combination, but each layer of abstraction brings new obstacles to development. For example, debugging a JSP page is almost as difficult as prospecting. You know there's something wrong, but you can't find the location of the problem; the cryptic error message, while pointing to the line number, is often not the real thing.

JSP technology cannot extend base classes, so code reuse can only be done through beans, include files, and custom tag libraries. Tag libraries are cumbersome and unsuitable for efficient reuse. It is cumbersome to maintain an XML for every API modification you make, and "markup design is language design" (see Resources for Noel Bergman article). The result is an additional layer on the interface that has already been divided into many layers.

We are facing the new World Wide Web. Whether Ajax can lead the way of web development, Web sites will continue to evolve toward smarter directions. In addition, although HTML itself is always declarative, the code that produces it is not necessarily the case. JSP technology and other templating systems must be overly complex, as they attempt to express essentially dynamic output in a declarative manner. That's why developers can't tolerate adding scriptlet to the JSP source code: The logic we're trying to express has a variety of forms.

By encapsulating HTML into Java code, you can express the output logic succinctly. An if statement and a for loop can take a familiar form. Page elements can be used to compose methods so that they are easy to understand and maintain. (Maintaining a larger JSP page is cumbersome and prone to errors, especially if there is a lack of good annotations.) By using a pure servlet, you can increase code reuse as much as possible, because you do not need to write a new class for each page's construction.

A fanatical design

To demonstrate the concept of a pure servlet, I built a scoring interface for an NCAA March Madness tournament bonus pool. (see March Mania and downloads). Users can select the 20 teams they think are the best from the 64 teams participating in the tournament and assign a weighted score to each team. After the game begins, their choices become read-only; When the game is over, the administrator enters the name of the winning team. According to the team chosen by the user, the cumulative score of the user is calculated automatically and the rank of the score is displayed.

The project took about three weeks of my spare time and spent most of my time on styles and images (I'm not a painter after all). In addition to an HTML file and other static resources, the UI layer consists of 21 Java classes, and according to JavaNCSS metrics, there are 1,334 Java statements (see Resources).

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.