JavaServer Pages

Java Server Pages, JSP abbreviated, is a technology developed by Sun Microsystems, based on JHTML web programming language for simple dynamic generation of HTML and XML output of a Web server.

It allows you to embed Java code and special JSP actions in HTML or XML pages. This allows the JSP syntax ( APS actions ) involve predefined functionality using special XML tags. This JSP actions are defined in the so-called tag libraries ( tag libraries ) as an extension of the HTML or XML tags.

JavaServer Pages are converted using a special JSP compiler into Java source code. This source code corresponding to a Java servlet is converted by the Java compiler into bytecodes in the terminal. The generated Java classes can be performed by a provided with a Java execution unit server.

JSP that was already available in the late 1990s, is a comparatively long-established technology for generating Web applications. It is increasingly being replaced by other techniques. In the context of the framework, Java Server Faces (JSF ) and the newer Facelets technology in Java EE 6 JSP Oracle describes now as deprecated.

Syntax

Basically, JSP can be as a kind of HTML or XML page describing additional JSP - specific tags and Java code. A JSP can be roughly divided into the following elements:

  • Static content such as HTML ( template text)
  • JSP directives
  • JSP scripting elements
  • APS actions
  • JSP tag libraries ( tag libraries )

Static Content

The static content are all those elements that are taken from the web server in the HTTP response (response) without change (eg HTML tags ). So a normal HTML document would be entirely without JSP elements, such as embedded Java code or JSP actions, a valid JSP; because JSP does allow to insert dynamic data into static content, but does not require this. It follows that (as with any template language ) the creator of the static content (ie, the design ) does not require any knowledge of Java; as soon as HTML structures but generated by Java code increases the complexity and the layout can be almost entirely changed at these points by the programmer.

Since this is a dynamic component in a JavaServer Page, but this can also make do entirely without static elements. In this way it is possible to create a JSP document binary data such as images or sounds, and send to the client.

Directives

A directive is used to convey specific information to the JSP page compiler; thus we can specify whether the JSP a tag library ( taglib short ) integrates or how to proceed further in the case of error.

The general syntax for a directive is <% @ ... %>. The following directives ( with the most common attributes) are available:

Standard variables

The following variables can be used in any JSP; They are also called implicit objects:

Script elements

There are three basic script elements that allow you to insert Java code directly in the JSP:

  • A tag that allows you to insert code in the class. This tag can be used to specify data of the class. The term is transferred to the member variables of the generated servlet class.
  • A tag that allows you to insert code in the method of the generated servlet _jspService. So there are local variables from it - Methods can not be created for this purpose, the above notation must be used.
  • A tag that allows you to expand code and to write directly to the HTTP response. The semicolon is not used here because the code is evaluated as an expression.

Declarations

Declarations are used to define variables and methods that can be used by other elements in the JSP. Declarations do not generate output within the JSP.

Expressions

Expressions ( expressions ) are used to integrate variables or methods directly in the HTML or XML output stream.

Scriptlets

JSP scriptlets can be used to implement the process logic and the generation of the XML output. The scriptlet code is inserted within the method of the generated servlet _jspService.

Comments

Comments are only visible within the original JSP, they are not written to the output stream.

Actions

JSP actions are XML tags that embed the built-in functionality of web servers. The following actions are available:

            < / jsp: include > jsp: param defining a parameter which is added to the request parameters. This action can within a jsp: include or jsp: forward block are used. If the parameter was already present in the request, the parameter that is the value from the "Query String" with the defined value attribute is overwritten, replaced. jsp: forward the request and the response is passed to another JSP or a servlet. The control does not return to the current JSP.                 < / jsp: forward > jsp: plugin This action generates different browsers a day to integrating a Java applet. This is needed because different tags are used to embed an applet in the older versions of Netscape Navigator and Internet Explorer.     < jsp: plugin type = " applet" height = " 100 % " width = "100%"              archive = " myjarfile.jar, myotherjar.jar "             codebase = " / applets "                 code = " com.example.foo.MyApplet.class " >                                                 Your browser does not support applets.             < / jsp: plugin > jsp: fallback defines the content that is displayed if the browser does not support applets. jsp: setProperty this action sets a property in the defined Java Bean.      < jsp: setProperty name = " MyBean " property = "last changed" value = " <% = new Date ()% >" / > jsp: getProperty This action retrieves a property of the defined Java Bean.      jsp: useBean this action creates or uses a Java Bean again. If a bean does not exist on the defined type, it is created initially so new. The optional scope attribute indicates how long the bean is available, that is, in which scope the bean is stored. The following values ​​can be defined: request Attributes are only available as long as the request exists. page Attributes are available only for the current JSP. (Standard) session Attributes are only available as long as the user session exists. application Attributes are always available. "Available" means here how long and from where the developer can access the bean. If the bean instance in the request scope, so the developer with this bean can be used both within the current JSP as well as in subsequent work ( included ). " Page Scope " limited the possible access to the current JSP page. "Session " describes all requests of a user session for this web application. "Application " means all requests of all users of the same web application this web server. example:         Tag Libraries

In addition to the predefined JSP actions, there is the possibility to use custom JSP actions. This requires a separate JSP tag library must be provided. A tag library is summarized on an XML description file, the day the so-called Library Descriptor (TLD ), defining the elements ( tags) to a Library ( Library) and associated with server-side Java classes that implement the functional logic of one or more implement tags.

As part of the Java Community Process Java Server Pages Standard Tag Library ( JSTL ) was created. This standardizes the main custom tags in different libraries. Also very common are the Apache taglibs, which in addition to the reference implementation for JSTL also many additional tag libraries.

Model-View- Controller

Sun recommends (similar to the Model View Controller design pattern ) when using JSPs using the Model 2 design pattern. For very small projects, the model can be used 1. The use of the design pattern is used to separate the presentation of the request processing. It should own servlets are used for request processing. After the request processing its own JSP should be available only for the creation of the output stream. This JSP should only contain HTML or XML and predefined or custom JSP actions. To integrate the data JavaBeans should be used.

JSP 2.0

With the introduction of the Servlet API 2.4 and the adoption of JSP 2.0, it is possible to dispense entirely with JSP scripting elements. JSPX documents correspond to well-formed XML documents whose tag elements are defined by unique namespaces. It is for example possible to produce XHTML -compliant outputs collide without Tag identifier.

The following example shows the structure of a well-formed document JSPX for generating XHTML code. About the XML root element " " a default namespace for XHTML elements are defined ( xmlns ) and a namespace for the JSP elements ( xmlns: jsp ). By prefixing the respective namespace (for example, < jsp: ... ) can be uniquely referenced in this way one day. Also Java tag libraries can be integrated in this way as a namespace; a corresponding directive no longer exists. The namespace definition is inherited by child elements.

In the example, agreed to ensure compatibility with current browsers on the JSP directive "page" as Content-Type "text / html" instead of "application / xhtml xml", as many popular browsers XHTML documents interpreted otherwise than XML source would, instead of interpreting the XHTML elements and display. In addition, the Internet Explorer knows this guy not know what causes the page is offered as a file for download. ( However, this only happens when the web server xml extradite the file as application / xhtml. )

< jsp: root xmlns = " http://www.w3.org/1999/xhtml "          xmlns: jsp = " http://java.sun.com/JSP/Page "          version = "2.0" >      < jsp: output doctype -root - element = "html"          doctype-public = " - / / W3C / / DTD XHTML 1.1 / / EN "          doctype-system = " http://www.w3c.org/TR/xhtml11/DTD/xhtml11.dtd " />                   XHTML and / or JSP elements      < / jsp: root > Another new feature is the so-called " Expression Language " ( EL). The Expression Language consists of script elements that are based on script elements interpreted scripting languages. Each script element starts with a dollar sign and includes an expression (german expression) in curly brackets.

$ { 1 2 }, $ { PI / 2 }, $ { } person.name The expression can be a simple placeholder to a variable, a constant or a Term also referencing within abstract data structures is about introspection possible.

A special extension of the JSP compiler replaces each occurrence before the compilation of the EL elements by the corresponding Java code.

The EL devices were designed so that they get along largely without protected XML entities. Exceptions are certain Boolean comparisons, but easily can be described as, for example, < according to the items must be used in well-formed XML documents.

Another new feature in JSP 2.0 are the so-called "Day Files" so you can set " Custom Tags " in the form of JSP pages without having to create special Java classes. Their use is particularly suited to " custom tags " that contain mostly visualization components to.

Versions

  • JSP 1.0, released on 27 September 1999
  • JSP 1.1
  • JSP 1.2
  • JSP 2.0 ( JSPX, EL )
  • JSP 2.1, part of Java EE 5
  • JSP 2.2 ( 10.12.2009, maintenance release of JSP 2.1), part of Java EE 6
  • JSP 2.3 ( 12/06/2013, second maintenance release of JSP 2.1), part of Java EE 7
18374
de