Multiple options for building Java Web Applications

Source: Internet
Author: User
Tags glassfish

The Apache Geronimo team has successfully implemented the new Java platform, Enterprise Edition (Java ee) 5.0 specification. Java ee 5 has many notable features, one of which is the new java standard tag Library (jstl) 1.2 specification. The key to jstl 1.2 is the Unified Expression Language, which allows us to combine the best features of jstl in JavaServer faces (JSF. This article describes the importance of jstl 1.2, explores the history of Java Web technology, and how the Geronimo team can use glassfish jstl 1.2 to add jstl 12 support to Geronimo.

Evolution of Java Web Technology

Web technology has always been part of the Enterprise Java language. They started from Servlet and evolved on this basis.

Servlets

The initial purpose of servlets is to respond to HTTP requests. Generally, compiling servlets is quite troublesome. See list 1.

List 1. Generate the HTML Servlet

                     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          ServletOutputStream out = response.getOutputStream();          out.println("<!DOCTYPE html PUBLIC /"-//W3C//DTD XHTML 1.0 Transitional//EN/" /"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/">");          out.println("

Servlets involves a large number of HTML (for example) embedded into Java code ). Compared with developing Servlets, it is more difficult to maintain them. Suppose you want to modify the code in Listing 1 to make the table have a boundary. To achieve this goal, you need to modify the Java code and recompile the servlet. Fortunately, servlets soon expanded through the assumerver pages (JSP) technology.

Assumerver pages

JSP technology enhances Java Servlets. The JSP component provides many improvements to servlets, including the ability to mix native markup and Java code. Listing 2 showsListing 1The same Servlet as the JSP component.

Listing 2. jsp 1.0

                <?xml version="1.0" encoding="ISO-8859-1" ?><%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><%@ page import="org.developerworks.*" %><%@ page import="java.util.List" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Obviously, the code in Listing 2 has been greatly improved on the basis of servlet. JSP components still need to be compiled as servlet, but this needs to be done through the servlet container (or can be completed as part of the build ). Therefore, JSP components can provide the same performance as servlet. PHP syntax is similar to Active Server Page (ASP) and PHP page, But compiling as servlet gives JSP components a significant performance advantage over other technologies.



Back to Top

JSP Model 2

Listing 2There are still some issues that cannot be ignored in the code. It uses a SectionScriptletIs a short Java code. Whether in design or practice, using scriptlet will bring about some problems. The JSP component can mix the business logic (retrieving the user list) with the representation at will. After development and evolution, the JSP Model 2 architecture solves this problem and enables servlet to be used together with JSP components. See listing 3.

Listing 3. Model 2 Servlet

                    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        UserDao dao = new UserDao();        List users = dao.getAllUsers();        request.setAttribute("users", users);        request.getRequestDispatcher("/user.jsp").forward(request, response);    }

Servlet can first process requests and execute business logic. Then you can save the result inHttpServletRequestAnd forward it to the JSP component. This simplifies the JSP component, as shown in Listing 4.

Listing 4. Model 2 JSP Components

                <?xml version="1.0" encoding="ISO-8859-1" ?><%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><%@ page import="org.developerworks.*" %><%@ page import="java.util.List" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

This solves some architecture problems. However, there is still a practical problem. The combination of Java code and HTML increases the threshold for JSP components. Java developers who do not have HTML knowledge and web designers who do not understand the Java language will encounter difficulties when using JSP components.

Assumerver pages standard tag Library (jstl)

Java scriptlet in JSP components is a goal of JSP technology. The final result of this exploration is jstl. Jstl introduces HTML style tags for accessing Java objects and executing many build tasks in the Java language, such as iterative sets, conditional logic, and formatting text. Jstl allows further development of JSP components, as shown in listing 5.

Listing 5. jsp containing jstl

                <?xml version="1.0" encoding="ISO-8859-1" ?><%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<c:forEach>Mark allows iteration of the user list.<c:out>Mark to allow access to Java objects and their data output.

The expressions used in the tag, such${users}And${user.id}Are all explained through jstl Expression Language (EL. For example, El interprets strings.${users}And in each object that can be accessed (suchpageContext,request,sessionAndservlet(Application) Context) in SearchusersAttribute. After further development, the JSP component allows external access to El from the jstl tag. This allows the code of the JSP component to be changed, as shown in Listing 6.

Listing 6. Using jstl and El in JSP

                <?xml version="1.0" encoding="ISO-8859-1" ?><%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

This is an updated jstl, and El first appeared in the JSP 2.0 specification. Through the Model 2 architecture (usually implemented by various UI frameworks, such as Apache struts), combined with jstl and El, JSP components can be built without using Java. This allows non-Java programmers to operate JSP components and enables Java developers to focus on implementing the business logic of their applications.

Assumerver faces Technology

However, JSP technology is not the only web technology that forms the Enterprise Java architecture. After the appearance of JSP 2.0 specifications, the JSF technology followed closely. JSF is designed to be a component architecture. Various objects on the Web page are considered as components with lifecycles and bound with Java objects. Therefore, in the JSP Example in this article, we can use JSF to directly bind a Java object to a view component. The generated JSP components are shown in listing 7.

Listing 7. using JSF in JSP

                <?xml version="1.0" encoding="ISO-8859-1" ?><%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %><%@ taglib uri="http:.//java.sun.com/jsf/core" prefix="f" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Note how we setdataTableBound to a Java object. Then, we only need to define the columns of the table, and the component knows the rows represented by the overlap. We assume that a managed backend Bean (backing bean )(UserBean).dataTableThe component will create HTML for us, so no HTML needs to be specified for the table. This is one of the advantages of JSF.



Back to Top

JSF and JSPs 1.2

JSF has always been using JSP technology. However, when JSF is introduced, JSP 1.2 is the most widely used version of JSP technology. Therefore, JSF is Based on JSP 1.2, so that JSF does not support jstl and El. You may have noticedAnd. They look similar to the El used by jstl. However, in JSF 1.0, they only have the same surface. JSF has its own El. its working principle is very similar to the El introduced in jstl, and later became part of JSP technology. However, JSF El and jstl El are not compatible-until now.

Unified Expression Language

One of the key points of Java ee 5 is the Unified Expression Language. In this way, the El used by jstl and JSF is integrated. You can now mix jstl and JSF, as shown in listing 8.

Listing 8. Mixing JSF and jstl

                     <f:view>          <c:forEach items="${UserBean.groups}" var="group">               ${group.groupName}               

The example in listing 8 demonstrates how to use jstl to iterate multiple groups. Then, we create a display table for each group to list users in the group. Note how to use different El labels for jstl respectively (<c:forEach>) And JSF components () References the data and serves as the El expression used directly in the JSP component. By mixing jstl and JSF El, we have achieved the effect of double swords.

El as part of Java ee 5

Java ee 5 also made a major modification to El. Jstl implementation does not need to be part of the J2EE 1.4 specification. Web application developers can select which jstl implementation to use in the application. Of course, they can also choose to only use the JSF implementation in the application.

Java ee 5 specifications require jstl implementation. Web application developers no longer have to worry about implementation in applications. Instead, they can choose to use jstl without hesitation. They can also use the powerful functions of JSF. The Unified Expression Language has become part of the Java ee 5 specification.



Back to Top

Geronimo and glassfish jstl

The previous section introduced the fact that in the past, web application developers could choose whether to include jstl technology. If yes, select the jstl implementation to be used in the application. The available jstl is implemented in a variety of ways.

Currently, in Java ee 5, the jstl implementation is included in the application server. Therefore, any implementation of the Java ee 5 specification must include the jstl implementation. When apache Geronimo developers start to operate on Java ee 5 implementations-Geronimo 2.0-they need to include a jstl implementation.

However, they cannot select an existing implementation at will. Unified Expression Language is an important requirement for jstl implementation. Many jstl implementations cannot be run with JSF during design. Fortunately, the Geronimo team did not implement jstl and the Unified Expression Language on its own. They can use sun's glassfish.

YouGlassfishIt may be quite unfamiliar. It is the reference implementation of Sun's Java ee 5 specification. It is an open-source application and is licensed by Sun's common development and distribution liclicense (cddl) and GNU General Public License (GPL. Sun has always provided a JSF reference implementation, so they soon provided a jstl reference implementation that contains a Unified Expression Language. The open-source nature of glassfish enables the Geronimo team to take advantage of this achievement and include the glassfish jstl implementation in Geronimo 2.0. This milestone release is Geronimo 2.0-m1.

Licensing considerations

Since glassfish has both cddl and GPL licenses, it can be included in Geronimo. However, it does not permit the rest of Geronimo's Apache styles, but adds some restrictions to the Geronimo team.

Basically, Geronimo includes glassfish jstl, but does not include its source code. In addition, the Geronimo team cannot modify its source code, but they can obviously contribute to glassfish jstl and use Geronimo to package a new program.

One remarkable thing about Geronimo is that you can modify its code and release a custom version of Geronimo. However, the jstl implementation is obviously an exception. Its source code is not included in Geronimo, and its license imposes some restrictions on the release of a custom version of Geronimo. For example, if you modify the source code of glassfish, the modification can only be used under the same glassfish license.



Back to Top

Conclusion

With the development and evolution of various web technologies in Java, developers have gained great benefits. Unified Expression Language is the latest development of web technology. It allows developers to mix jstl and JSF technologies and promises developers to continue to benefit from it. Today, Unified Expression Language is an important part of Java ee 5 specifications, which makes it an important part of Geronimo. Geronimo not only implements its specification, but also uses the reference implementation of the Specification, which once again facilitates developers. With glassfish jstl and Geronimo 2.0, developers will have more options when building Java Web applications.

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.