I wouldn’t be biased if I were to say that JSP technology is one of the most favorite presentation/web tier technologies at present. After it’s introduction in JEE technology platform, there has been considerable change in the middle tier technology and data access technology, but the front end tier is mostly built using a JSP. There may be a wrapper in which the JSP is embedded to achieve few additional features, but the base still remains a JSP. Let us take a close look at the different uses of JSP technology at the front end tier.
Plain JSP:
By plain JSP, we are talking about a JSP not surrounded by any other technology. Here when it comes to displaying a page to the client, the response is presented using a jsp page. When the middle tier finishes its processing, it sends/forwards the result to a JSP page (XYZ.jsp). This single page does everything to present the result. This page may arrive at the final result using following options.
- Jsp using HTML: Jsp is constructed using HTML for static/text contents and JSP elements for dynamic contents.
- Jsp using XML: In this approach, a jsp is constructed using XML (eXtensible Markup Language). By using XML, we can make the jsp code look neat, as well as we can use features of XML.
- Jsp with SVG: This is a specialized use of Jsp technology to build presentation tier of two dimensional graphics and graphical applications. Scalable Vector Graphics targets front end of mobile devices.
- Jsp with WML: This is another specialized use of Jsp in mobiles. Wireless Markup Language is used to write pages which are displayed in devices that implement Wireless Application Protocol (WAP). In short jsps built using WML are displayed in WAP browsers.
- Including reusable components like stylesheets, javascript, other jsp pages, custom tags: These components provide support to build a single page using jsp technology.
- o Stylesheets separate the look and feel or formatting definition classes to a file so that it can be reused.
- o Similarly javascript files separate javascript functions in a different file, which can be included in many jsps and reused.
- o There may be some code which will be common in all/many jsps in an application. We can separate this code in another jsp file and include it in each jsp (in static or dynamic way).
- o Custom tags is another powerful way of reusing jsp code. In addition to reuse, these components make the jsp look neat by replacing large chunks of code with a tag. This also reduces the jsp writing time.
Details of Jsp technology can be found in this Jsp Tutorial.
JSP with Servlet:
If we look at the lifecycle of a jsp, we can see, that the jsp has actually a generated a servlet which is doing the job for us. Hence, a jsp gets converted into a servlet and the sevlet runs in the web container. But there is this interesting design pattern which uses a jsp with servlet. You may ask why do we need two servlets when a jsp can do the job of a servlet? The answer is – two important concerns are separated in this approach, viz. presentation and navigation. Jsp is used to handle the presentation business only. Servlet deals with navigation logic and the logic to call services in middle and database tier (if applicable).
Jsp Templates:
This is another way of reusing jsp code. Here we define jsp template that is in line with the standard layout of our application. Each jsp is constructed by replacing the template elements with appropriate components.
Jsp and Tiles:
Tiles take the jsp template to the next level. Tiles ask us to define a layout (s) which is (are) the base for the jsps we write. These layouts contain string and jsp elements placeholders that can be dynamically injected. There is an xml file which has a definition, that tells the container, how to construct a page. The construction includes usage of layout, and replacement of the placeholders. E.g. in layout we define placeholders for headers, footers, left menu and body of a jsp. Whenever we construct a page, we replace these place holders with appropriate pages.
Jsp and Struts:
To integrate a Jsp with Struts, we need to specify the forward path to a jsp either in struts-config.xml or in an Action class itself. Struts does allow us to use tiles. In that case, the integration is done through tiles definition. Also, Sturts has action form, which can be used while the data gets submitted to server. This form is populated and made available through the request object.
Jsp and Java Server Faces:
Java server faces creates a difference in the way we use jsp. When we integrate these two, a jsp page will contain Java Server Faces (JSF) tags. These tags are linked to server side registered components. Each component may have associated listeners, validators and converts with it. We need to be careful while integrating these two technologies as now we are having two server-side components. The first server-side components are the dynamic elements of a jsp and the second are the JSF tags. If we do not consider compilation, execution and rendering mechanism of these two technologies, then it is possible that we end up in a performance problem.
Jsp and Spring MVC:
This is similar to Struts, difference being the implementation is a bit light. Some of us may find Struts a little complicated as compared to Spring MVC, but as long as our scope is jsp integration, they are same.
Here is a good example of Jsp with Tiles and Spring MVC.