JSP technology is the Java Platform Technology (enterprise technology) for delivering dynamic content to web user (the person who is giving request from browser window) in a portable, secure and well-defined way. JSP has been built on top of the Servlet API and utilizes Servlet semantics. It uses HTML and XML templates and Java code to generate JSP page. JSP is secure, fast and independent of server platforms.
Although JSP technology is going to be a powerful successor to basic Servlets, there is a relation between JSP and Servlet. Servlets are powerful and sometimes they are a bit cumbersome when it comes to generating complex HTML. But JSP is handy with HTML and JavaScript. JSP is also powerful compared to other web technologies like PHP, ASP as it is developed as per Java specifications.
Most Servlets contain codes to handle application logic and to handle output formatting. This can make it difficult to separate and reuse portions of the code when a different output format is needed. For these reasons, web application developers turn towards JSP as their preferred servlet environment. It also contains many more features like platform independent, server independent.
We’ll be covering the following topics in this tutorial:
Evolution of Web Applications
Over the past few years, web server applications have evolved from static to dynamic application. This evolution became necessary due to some deficiencies in earlier website design. For example, to put more business processes on the web, both in business-to-consumer (B2C) or business-to-business (B2B) markets, conventional website design technologies are not enough.
The main issues every developer faces while developing web applications are as follows:
1. Scalability – A successful web site will have more users and as the number of users increase, the web applications have to scale correspondingly.
2. Integration of data and business logic – The web is just another way to conduct business, and so it should be able to use the same middle-tier and data-access code.
3. Manageability – Websites just keep getting bigger and we need some viable mechanism to manage the ever-increasing content and its interaction with business systems.
4. Personalization – Adding a personal touch to the web page becomes an essential factor to retain customers. Knowing their preferences, allowing them to configure the information they view, remembering their past transactions or frequent search keywords are all important in providing feedback and interaction from what is otherwise a fairly one-sided conversation.
Apart from these general needs of a business-oriented website, the necessity for new technologies to create robust, dynamic and compact server-side web applications has been realized. The main characteristics of today’s dynamic web server applications are as follows:
• Serve HTML and XML, and stream data to the web client
• Separate presentation, logic and data
• Interface to databases, other Java application server middleware to provide transactional support
• Track client sessions
Now let us have a look on the role of Java technology and platform in this regard.
Java’s Role for Server Applications
Sun Microsystems, having consulted many expert partners from other IT related industries, has come out with a number of open APIs for the technologies and services on server side. This collection of APIs is named as Java 2 Enterprise Edition (J2EE). The J2EE specification provides a platform for enterprise applications, with full API support for enterprise code and guarantees of portability between server implementations. Also, it brings a clear division between code which deals with presentation, business logic and data.
The J2EE specification meets the needs of web applications because it provides the following:
• Rich interaction with a web server via servlets and built-in support for sessions available in both Servlets and EJBs.
• The use of EJBs to mirror the user interaction with data by providing automatic session and transaction support to EJBs operating in the EJBs server.
• Entity EJBs to represent data as an object and seamless integration with the Java data access APIs.
• Flexible template-based output using JSP and XML.
This family of APIs means that the final web page can be generated from a user input request, which was processed by a Servlet or JSP and a session EJB, which represents the user’s session with the server, using data extracted from a database and put into an entity EJE. Thus, the Java revolution of portable code and open APIs is married with an evolution in existing products such as database, application, mail and web servers. The wide availability of products to run Java applications on the server has made Java lucrative in a very competitive market. This makes server-side Java a very exciting area.
The Java Server Pages 1.2 specification provides web developers with a framework to build applications containing dynamic web content such as HTML, DHTML and XML. A JSP page is a text based document containing static HTML and dynamic actions which describe how to process a response to the client in a more powerful and flexible manner. Most of a JSP file is plain HTML but it also has, interspersed with it, special JSP tags.
There are many JSP tags such as:
• <%@page language=”java” %> – this is a JSP directive denoted by <%@,
• scrip lets indicated by <%..%> tags and
• <%@include file=”sample.html”%>- this directive includes the contents of the file sample.html in the response at that point.
To process JSP file, we need a JSP engine that can be connected with a web server or can be accommodated inside a web server. Firstly when a web browser seeks a JSP file through an URL from the web server, the web server recognizes the .jsp file extension in the URL requested by the browser and understands that the requested resource is a JavaServer Page. Then the web server passes the request to the JSP engine. The JSP page is then translated into a Java class, which is then compiled into a Servlet.
This translation and compilation phase occurs only when the JSP file is requested for the first time, or if it undergoes any changes to the extent of getting retranslated and recompiled. For each additional request of JSP page thereafter, the request directly goes to the Servlet byte code, which is already in memory. Thus when a request comes for a Servlet, an init() method is called when the Servlet is first loaded into the virtual machine, to perform any global initialization that every request of the Servlet will need. Then the individual machine requests are sent to a service() method, where the response is put together. The Servlet creates a new thread to run service() method for each request. The request from the browser is converted into a Java object that is used to send the response back to the browser. The Servlet code performs the operations specified by the JSP elements in the .jsp file.
TAG Based Programming
Programming may be code based or tag based. Code-based programming is difficult to write and understand. However, tag-based program is easier and program length is less as compared to codes based programming. Tags reduce the development time and maintenance cost and increases the reusability of code.
We have some ideas on tag-based programming like HTML, JavaScript and CSS, etc. JSP is also totally tag based. JSP tags implement the functionality of tags into tag implementation classes. So to develop JSP page, large amount of Java code is not needed. So this helps the programmer to develop java codeless JSP program by which developing separate presentation logic and business logic has become easier.