JavaServer Pages
Java Server Pages abbreviated, JSP , is one of Sun Microsystems developed on JHTML based web - programming language for simple dynamic generation of HTML - and XML -Expenditure a Web server .
It allows Java code and special JSP actions to be embedded in HTML or XML pages. The JSP syntax enables predefined functionality to be integrated using special XML tags (JSP actions) . This JSP actions are in so-called tag libraries ( tag libraries ) defined as an extension of HTML or XML tags.
JavaServer Pages are converted into Java source code using a special JSP compiler . This source code, which corresponds to a Java servlet , is then converted into bytecode by the Java compiler . The Java classes generated in this way can then be executed by a web server equipped with a Java execution unit .
JSP, which was already available in the late 1990s, is a comparatively long-standing technique for creating web applications. It is increasingly being replaced by other technologies. Using the JavaServer Faces (JSF) framework and the newer Facelets technology under Java EE 6 , Oracle JSP is deprecated .
syntax
Basically, JSP can be described as a kind of HTML or XML page with additional JSP-specific tags and Java code. A JSP can roughly be broken down into the following elements:
- static content like HTML (template text)
- JSP directives
- JSP script elements
- JSP actions
- JSP tag libraries
Static content
The static content is all those elements that are adopted by the web server in the HTTP response (response) without change (e.g. HTML tags). A normal HTML document without any JSP elements, such as embedded Java code or JSP actions, would be a valid JSP; because JSP allows dynamic data to be inserted into static content, but does not require this. It follows that (as with every template language ) the creator of the static content (i.e. the design) does not need any Java knowledge; as soon as HTML structures are generated by Java code, their complexity increases and the layout can almost only be changed by the programmer at these points.
Since a JavaServer Page is a dynamic component, it can also do without any static elements. In this way, it is possible to use a JSP document to generate binary data such as images or sounds and to send them to the client.
Directives
A directive is used to transmit special page information to the JSP compiler; this allows you to specify whether the JSP includes a tag library ( taglib for short ) or how to proceed in the event of an error.
The general syntax for a directive is <%@ … %>
. The following directives (with the most common attributes) are available:
- include
- instructs the JSP compiler to copy the entire contents of an external file into the original file. This embedding takes place at translation time , i.e. statically once with the translation of the JSP into a servlet.
<%@ include file="BeispielDatei.ext" %>
- page
-
- import
- generates a Java import statement in the file
<%@ page import="java.util.*" %>
- contentType
- indicates the type of file content . Should be used if you do not use HTML or do not use the preset character set.
<%@ page contentType="text/html" %>
- errorPage
- specifies the page to be displayed in the event of an error
<%@ page errorPage="ErrorHandler.jsp" %>
- isErrorPage
- indicates whether this page is an error page or not; if so, the exception object is available
-
<%@ page isErrorPage=false %>
<% - the page is not an error page -%> - isThreadSafe
- indicates whether the servlet generated from the JSP is thread safe or not
-
<%@ page isThreadSafe=true %>
<% - a thread safe JSP -%>
- taglib
- indicates that a taglib should be used. A prefix and a URI must be assigned for the taglib.
<%@ taglib prefix="MeinPrefix" uri="taglib/MeineTagLib.tld" %>
Standard variables
The following variables can be used in any JSP; They are also known as implicit objects:
- application
- Application object; lives as long as the server is running.
- config
- a configuration interface
- out
- JSPWriter that writes the data to the HTTP response stream
- page
- the servlet itself
- pageContext
- an instance of the PageContext that contains the data for the entire page
- request
- the HTTP request object, i.e. the request from the client to the server
- response
- the HTTP response object, i.e. the response from the server to the client
- session
- the HTTP session object. It can be used to pass information about the user from one request to the next.
Script elements
There are three basic script elements that allow Java code to be inserted directly into the JSP:
- A tag that allows code to be added to the class. This tag can be used to set dates for the class. The expression is transferred to member variables of the generated servlet class.
<%! int serverInstanceVariable = 1; %>
- A tag that allows code to be
_jspService
inserted into the method of the generated servlet. This turns it into local variables - methods cannot be created, the above notation must be used for this.
<% int localStackBasedVariable = 1; %>
- A tag that allows code to be expanded and written directly into the HTTP response. The semicolon is not used here because the code is evaluated as an expression.
<%= "expanded inline data " + 1 %>
Declarations
Declarations are used to define variables and methods that can be used by other elements in the JSP. Declarations do not produce any output within the JSP.
<%! int variableMeinerKlasse = 0; %>
Expressions
Expressions (expressions) are used to integrate variables or methods directly into the HTML or XML output stream.
Die Klassenvariable ist <%= variableMeinerKlasse %>
Scriptlets
JSP scriptlets can be used to implement the flow logic and to generate the XML output. The scriptlet code is _jspService
inserted within the method of the generated servlet.
<% int variable = 0; out.println("Der Wert der Variable ist: " + variable); %>
Comments
Comments are only visible within the original JSP; they are not written to the output stream.
<%-- Kommentar innerhalb einer JSP --%>
Actions
JSP actions are XML tags that integrate the built-in functionality of web servers. The following actions are available:
- jsp: include
- the specified JSP is called by the Java servlet and the request and response are transferred. Once the specified JSP has been processed, control returns to the current JSP. This JSP action causes the embedded JSP code to be shared and not copied by all embedding JSPs.
<jsp:include page="mycommon.jsp"> <jsp:param name="extraparam" value="myvalue"/> </jsp:include>
- jsp: param
- defines a parameter that is added to the request parameters. This action can be used within a jsp: include or jsp: forward block. If the parameter was already available in the request, the parameter is overwritten, ie the value from the "Query String" is replaced with the defined value attribute.
- jsp: forward
- the request and the response are transferred to another JSP or a servlet . Control does not return to the current JSP.
<jsp:forward page="subpage.jsp"> <jsp:param name="forwardedFrom" value="this.jsp"/> </jsp:forward>
- jsp: plugin
- Depending on the browser used, this action generates a tag for integrating a Java applet . This is required because in the older versions of Netscape Navigator and Internet Explorer different tags are used to integrate an applet.
<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
- 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="lastChanged" value="<%= new Date()%>" />
- jsp: getProperty
- this action fetches a property from the defined Java bean.
<jsp:getProperty name="myBean" property="lastChanged" />
- jsp: useBean
- this action creates or reuses a Java bean. If a bean of the defined type does not exist, it is initially recreated. The optional scope attribute specifies how long the bean is available, i. H. in which area of validity the bean is stored. The following values can be defined:
- request
- Attributes are only available as long as the request exists.
- page
- Attributes are only available for the current JSP. (Default)
- session
- Attributes are only available as long as the user session exists.
- application
- Attributes are always available.
- "Available" here means how long or from where the developer can access the bean. If the bean is in the request scope, for example, the developer can work with this bean both within the current JSP and in downstream (included) ones. “ Page Scope” restricts possible access to the current JSP page. “Session” describes all requests of a user session for this web application. "Application" are all requests from all users of the same web application on this web server.
- Example:
<jsp:useBean id="myBean" class="com.example.foo.MyBean" scope="request"> </jsp:useBean>
Tag libraries
In addition to the predefined JSP actions, there is the option of using user-defined JSP actions. A separate JSP tag library must be made available for this. A tag library is defined via an XML description file, the so-called Tag Library Descriptor (TLD), which combines elements (tags) into a library (library) and associates them with server-side Java classes that contain the functional logic of one or more Implement tags.
The JavaServer Pages Standard Tag Library (JSTL) was created as part of the Java Community Process . This standardizes the most important custom tags in various libraries. The Apache Taglibs are also very popular and contain many additional tag libraries in addition to the reference implementation for the JSTL.
Model-View-Controller
Sun recommends using the Model 2 design pattern (similar to the Model View Controller design pattern) when using JSPs. Model 1 can be used for very small projects. The use of the design pattern serves to separate the display from the request processing. You should use your own servlets for request processing. After the request has been processed, a separate JSP should only be available for creating the output stream. This JSP should only contain HTML or XML and pre-defined or custom JSP actions. JavaBeans should be used to integrate the data .
JSP 2.0
With the introduction of the Servlet API 2.4 and the adoption of JSP 2.0, it is possible to dispense with JSP script elements entirely. JSPX documents correspond to well-formed XML documents whose tag elements are defined by unique namespaces. For example, it is possible to generate XHTML -compliant output without the tag identifier colliding.
The following example shows the framework of a well-formed JSPX document for generating XHTML code. A <jsp:root>
standard namespace for XHTML elements (xmlns) and a namespace for JSP elements (xmlns: jsp) are defined via the XML root element " " . By prefixing the respective namespace (for example <jsp: …
) a tag can be clearly referenced in this way. Java Tag Libraries are also integrated as a namespace in this way; there is no longer a corresponding directive. The namespace definition is passed on to child elements.
In the example, for reasons of compatibility with current browsers, the JSP directive "page" is used as the content type "text / html" instead of "application / xhtml + xml", since many common browsers otherwise interpret XHTML documents as XML source text instead of interpreting and displaying the XHTML elements. In addition, Internet Explorer does not recognize this type, which means that the page is offered as a file for download. (However, this only happens if the web server also delivers the file as application / xhtml + xml.)
<?xml version="1.0" encoding="utf-8"?> <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" /> <jsp:directive.page contentType="text/html; charset=utf-8" language="java" /> <html xmlns="http://www.w3.org/1999/xhtml"> XHTML- und/oder JSP-Elemente </html> </jsp:root>
Another innovation is the so-called " Expression Language " (EL). The expression language consists of script elements that are based on script elements of interpreted script languages. Each script element starts with a dollar sign and includes an expression ( English expression ) in curly brackets.
${1+2}, ${PI/2}, ${person.name}
The expression can be a simple placeholder for a variable , a constant or a term . A referencing within abstract data structures is about introspection possible.
A special extension of the JSP compiler replaces each occurrence of the EL elements with the corresponding Java code before the compilation process.
The EL elements were designed in such a way that they largely manage without protected XML entities. Exceptions are certain Boolean comparisons, which can be easily rewritten, for example < becomes lt . The elements can therefore be used in well-formed XML documents.
Another innovation in JSP 2.0 are the so-called "Tag Files", with which "Custom Tags" can be created in the form of JSP pages without special Java classes. They are particularly suitable for "custom tags", which mainly contain visualization components.
Versions
- JSP 1.0, released on September 27, 1999
- JSP 1.1
- JSP 1.2
- JSP 2.0 (JSPX, EL)
- JSP 2.1, part of Java EE 5
- JSP 2.2 (December 10, 2009, maintenance release of JSP 2.1), part of Java EE 6
- JSP 2.3 (June 12, 2013, 2nd maintenance release of JSP 2.1), part of Java EE 7
See also
Web links
- JavaServer Pages Technology Product description at Oracle
- JSP 2.0 Syntax Reference from Sun.
- tomcat.apache.org - reference implementation of a servlet container (open source)
- jsptutorial.org - a tutorial on JavaServer Pages and Java-based web applications
Individual evidence
- ↑ developer.com
- ↑ oracle.com
- ↑ JavaServer Pages 1.0 Specification ( page no longer available , search in web archives ) Info: The link was automatically marked as defective. Please check the link according to the instructions and then remove this notice. (PDF)