JavaServer Pages

JavaServer Pages (JSP) is a programming language developed by Sun Microsystems. It provides a simplified way to create dynamic content in a short time. JSP is based on JHTML. JSP allows insertion of Java code and special JSP actions in HTML or XML pages. Specific functionalities can be added with JSP syntax, in particular specific XML tags that are part of an JSP actions.

JSP actions are defined in the tag libraries as an extension of HTML or XML tags. It is transformed into Java source code using special JSP compilers. The source code is comparable to a Java servlet and is modified by the Java compiler into bytecode. This creates Java classes that can be implemented by servers that have set up the necessary Java execution unit.

JavaServer Pages also enables rapid development of web-based applications that are server and platform-independent. This programming language has been available since the end of the 90s, but has been increasingly replaced over time by other technology.[1] JSP is rejected by many because of its outdated technology.[2]

JavaServer Pages has the advantage that the user interface is separate from content generation, so when you change the page layout, the dynamic content remains unaffected.

Syntax

JavaServer Pages can be understood to be HTML or XML pages containing Java code and other JSP-specific tags.

Generally, JSP can be classified into the following components:

Template text

Template text is fixed content. It is taken from the web server and integrated into the HTTP response, without being changed. An HTML document merely consisting of one static text itself is also a JSP. Insertion of JSP elements is possible but not necessary. This means that the originator of static content does not require any knowledge of Java.

Java Server Page is a dynamic element and can therefore exist without fixed components. This allows data such as images to be produced and sent to the client using a JSP document.

JSP directives

Page information is captured by the JSP compiler through directives that are defined by the general syntax <%@ ...%>. Common directives are listed below:

  • include

Command to the JSP compiler to insert the content of an external file in the original file.

<%@include file ="file.ext"%>
  • page

Further terms can be included under the term page:

    • import

Generating a Java import statement in the file

<%@ page import=”java.util.*%>
    • contentType

Indication of the type of content <%@ page contentType=”Text/html” %>

    • errorPage

Specification of the page that is displayed when an error occurs

<%@ page errorPage="errorresolution.jsp" %>
    • isErrorPage

Specifying whether it is an error page or a correct page. False means that it is not an error page.

<%@ Page isErrorPage=false%>
    • isThreadSafe

Specifies whether the thread is safe. True here is a thread-safe page

<%@ page isThreadSafe=true%>
  • taglib

Command that a tag library is to be used. A prefix and a URL for the tag library must be assigned.

<%@ taglib prefix=”ChosenPrefix” uri=”taglib/MyTagLib.tld”&>

Standard variables

Common variables that are also called implicit objects can be implemented in JSP. These include the variables:

  • application

Application object whose inventory depends on a running server.

  • config

Designation of a configuration interface

  • out

Name of JSPWriters which translates the data into the HTTP response stream

  • page

Identification of the servlet

  • pageContext

Level in which the information of the page is shown.

  • request

HTTP request object

  • response

HTTP response object

  • session

HTTP session object makes it possible to pass on information about the user from one request to the next.

JSP scripting elements

The script elements allow direct insertion of Java code in the JSP. There are three different script elements that can be distinguished.

  • The first tag enables you to install the code in the class. Class data can be defined with this tag.
<%! int serverInstanceVariable = 1; %>
  • Another tag allows you to insert a "_jspService" code in the servlet method. It will produce local variables.
<% int localStackBasedVariable = 1; %>
  • The third tag allows you to expand the code and write in the HTTP response.
<%= "expanded inline data " + 1 %>

JSP actions

The JavaServer Pages actions are XML tags and comprise the built-in functionality of servers. The following actions are possible:

  • jsp:include

By calling the specified JavaServer Pages, the request and response will be passed on. After being executed, control will be returned to JSP. The purpose of the action is that the JSP code is shared and not copied.

   <jsp:include page="mycommon.jsp">
      <jsp:param name="extraparam" value="myvalue"/>
  </jsp:include>
  • jsp:param

Name of a parameter which is annexed to the request parameters.

  • jsp:forward

In this action, the request and the response is forwarded to another JSP or servlet without returning control to the JSP.

 <jsp:forward page="subpage.jsp">
      <jsp:param name="forwardedFrom" value="this.jsp"/>
  </jsp:forward>
  • jsp:plugin

This action creates a tag for the integration of Java applets.

 <jsp:plugin type="applet" height="100%" width="100%"
           archive="myjarfile.jar,myotherjar.jar"
          codebase="/applets"
              code="com.example.foo.MyApplet.class">
      <jsp:params>
          <jsp:param name="enableDebug" value="true"/>
      </jsp:params>
      <jsp:fallback>
          Your browser does not support applets.
      </jsp:fallback>
  </jsp:plugin>
  • jsp:fallback

This action specifies the content which is to be displayed in case the browser does not support applets.

  • jsp:setProperty

This action will set a property in the defined JavaBean.

  <jsp:setProperty name="myBean" property="lastChanged" value="<%= new Date()%>" />
  • jsp:getProperty

A property is obtained by the action of the defined JavaBean.

  <jsp:getProperty name="myBean" property="lastChanged" />
  • jsp:useBean

With this action a JavaBean is written or reused. Attributes such as the request, the page, the session, and the application can be defined.

 <jsp:useBean id="myBean" class="com.example.foo.MyBean" scope="request">
  </jsp:useBean>

JSP tag libraries

Beyond the classic JSP actions, custom Java Script Pages actions can be applied. For this purpose, you have to create your own tag library. It is defined with the tag library descriptor.

Modification by servlet API 2.4

With the implementation of the servlet API 2.4 as successor of JSP 2.0, JSP scripting elements can be dispensed with. JSPX documents are equivalent to XML documents.

Versions

Since its initial release on September 27, 1999 some modifications were made to the programming language:

  • JSP 1.0
  • JSP 1.1
  • JSP 1.2
  • JSP 2.0
  • JSP 2.1

References

  1. http://www.developer.com/java/web/article.php/3867851/JSF- 20 Views-Hello-Goodbye-Facelets JSP.htm JSF Overview
  2. http://www.oracle.com/technetwork/java/javaee/jsp/index.html JavaServer Page technology

Web Links

Category

Related Articles