Java Server Pages

1. INTRODUCTION AND MARKETPLACE

Java Server Pages (JSP) is a server-side technology that enables web programmers to generate web pages dynamically in response to client requests. Among many server-side technologies used to build dynamic web applications, JSP is the one that has pulled the attention of web developers due to several reasons.

JSP is basically a high-level extension to the Java servlet technology. It enables us to insert pure Java code in an HTML document directly. These pages are executed under the supervision of an environment called web container. The web container first compiles the page on the server and generates a servlet, which is loaded in the Java Runtime Environment (JRE) automatically. The interesting part is that the web container only generates the servlet from the page if its last modification time is newer than the time when the servlet was generated last. The servlet serves the client requests in the usual way. This makes the development of web applications convenient, simple, and fast. Maintenance also becomes very easy.

JSP technology provides excellent server-side programming for web applications that need database access. JSP not only provides cross-web-server and cross-platform support, but also integrates the WYSIWYG (What You See Is What You Get) features of static HTML pages with the extreme power of Java technology.

JSP also allows us to separate the dynamic content of our web pages from the static HTML content. We can write regular HTML files in the usual way. We can then insert Java code for the dynamic parts using special tags, which usually start with <% and end with %>.

Usually, JSP files have the extension jsp. They are also installed in a place where we can place our normal web pages. Many web servers let us define aliases for JSP pages or servlets. So, a URL that appears to reference an HTML file may actually point to a JSP page or servlet.

2. JSP AND HTTP

Java Server Pages specification extends the idea of Java servlet API, to provide a robust framework to developers of web applications for creating dynamic web content. Currently, JSP or servlet technology supports only HTTP. However, a developer may extend the idea of servlet or JSP to implement other protocols such as FTP or SMTP. Since, JSP uses HTML, XML, and Java code, the applications are secure, fast, and independent of server platforms. It allows us to embed pure Java code in an HTML document. It is important to note that JSP specification has been defined on top of the Java servlet API. Consequently, it follows all servlet semantics and has all powers that a servlet has.

The life cycle and many of the capabilities, especially the dynamic aspects of JSP pages, are exactly the same as Java servlet technology. So, much of the discussion in this chapter refers to Chapter 20.

3. JSP ENGINES

To process JSP pages, a web container that can understand JSP (sometimes referred to as JSP engine) code is needed. It is interesting to note that what we know as the JSP engine is nothing but a specialized servlet, which runs under the supervision of the servlet engine. This JSP engine is typically connected with a web server or can be integrated inside a web server or an application server. Many such servers are freely available and can be downloaded for evaluation and/or development of web applications. Some of them are Tomcat, Java Web Server, WebLogic, and WebSphere.

Once you have downloaded and installed a JSP-capable web server or application server in your machine, you need to know where to place the JSP files, what their URLs will be and how to access them from the web browser or another application using those URLs. Since, Tomcat from Apache is an open source, free and easily configurable JSP engine, we shall use it to test all our JSP pages in this chapter.

3.1. Tomcat

Tomcat implements servlet 2.2 and JSP 1.1 specifications. It is very easy to install and can be used as a small stand-alone server for developing and testing servlets and JSP pages. For large applications, it is usually integrated into the more powerful Apache Web server.

Since, we shall use Tomcat JSP engine to test our JSP pages in this chapter, let us know how to install Tomcat in our machine first.

  • Download the appropriate version of Tomcat from the Apache site http://tomcat.apache.org\
  • Uncompress the file in a directory. If it is an installer file in Windows, install it in a directory by double clicking on the installer file.
  • Set the environment variable java_home to point to the root directory of your JDK hierarchy. For example, if the root directory of your JDK is D:\Java\jdk1.7.0_07, the JAVAJHOME environment variable should point to this directory. Also make sure that the directory (usually bin) containing the Java compiler (javac) and interpreter is in your path environment variable.
  • Go to the bin directory under the tomcat installation directory and start Tomcat using the command-line command startup.bat (in Windows) or startup.sh (in Unix/Linux). If everything goes well, you will see an output in Windows as shown in Figure 21.1:

  • Tomcat is now running on port 8080 by default. You can test it by using the URL http://127.0.0.1:8080\ . The page shown in Figure 21.2: appears

3.2. Java Web Server

The Java System Web Server by Sun is a leading web server that delivers secure infrastructure for medium and large business technologies and applications. Sun claims that it delivers 8x better performance than Apache 2.0 with Tomcat. It is available on all major operating systems and supports a wide range of technologies such as JSP and Java Servlet technologies, PHP and CGI.

3.3. WebLogic

WebLogic by BEA Systems of San Jose, California, is a J2EE application server and also an HTTP server for Microsoft Windows, Unix/Linux, and other platforms. WebLogic supports DB2, Oracle, Microsoft SQL Server, and other JDBC-compliant databases.

The WebLogic server also includes the .NET framework for interoperability and allows integration of the following native components:

  • CORBA connectivity
  • COM+ Connectivity
  • J2EE Connector Architecture
  • IBM’s WebSphere MQ connectivity
  • Native JMS messaging for enterprise

Data Mapping functionality and Business Process Management are also included in WebLogic Server Process Edition.

3.4. WebSphere

IBM attempted to develop a software to set up, operate, and integrate electronic business applications that can work across multiple computing platforms, using Java-based web technologies. The result is WebSphere Application Server (WAS). It includes both the run-time components and the tools that can be used to develop robust and versatile applications that will run on WAS.

4. HOW JSP WORKS

When a client such as a web browser sends a request to a web server for a JSP file (usually having an extension jsp) using a URL, the web server identifies the .jsp file extension in the URL and figures out that the requested resource is a Java Server Page. The web server hands over the request to a special servlet. This servlet checks whether the servlet corresponding to this JSP page exists or not. If the servlet does not exist, or exists but it is older than the JSP page, it performs the following:

  • Translates the JSP source code into an equivalent servlet source code
  • Compiles the servlet source code to generate a class file
  • Loads the servlet class file and creates an instance of this servlet
  • Initializes the servlet instance by calling the jspinit() method
  • Invokes the _jspService() method, passing request and response objects

If the servlet exists and is not older than the corresponding JSP page requested, it does the following:

  • If an instance is already running, it simply forwards the request to this instance.
  • Otherwise, it loads the class file, creates an instance, initializes it, and forwards the request to this instance.

During development, one of the advantages of JSP pages over servlets is that the build process is automatically performed by the JSP engine. When a JSP file is requested for the first time, or if it is changed, the translation and compilation phase occurs. The subsequent requests for the JSP page directly go to the servlet byte code, which is already in the memory.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *