JavaServer Pages
Filename extension | .jsp |
---|---|
Internet media type | application/jsp |
Latest release | 2.3 |
Standard | JSR 245 |
Website | JavaServer Pages Technology |
JavaServer Pages (JSP) is a technology that helps software developers create dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun Microsystems,[1] JSP is similar to PHP and ASP, but it uses the Java programming language.
To deploy and run JavaServer Pages, a compatible web server with a servlet container, such as Apache Tomcat or Jetty, is required.
Contents
1 Overview
2 Syntax
2.1 Expression Language
2.2 Additional tags
3 Compiler
4 Criticism
5 See also
5.1 Servlet containers
5.2 Java-based template alternatives
6 References
7 Further reading
8 External links
Overview
Architecturally, JSP may be viewed as a high-level abstraction of Java servlets. JSPs are translated into servlets at runtime, therefore JSP is a Servlet; each JSP servlet is cached and re-used until the original JSP is modified.[2]
JSP can be used independently or as the view component of a server-side model–view–controller design, normally with JavaBeans as the model and Java servlets (or a framework such as Apache Struts) as the controller. This is a type of Model 2 architecture.[3]
JSP allows Java code and certain predefined actions to be interleaved with static web markup content, such as HTML, with the resulting page being compiled and executed on the server to deliver a document. The compiled pages, as well as any dependent Java libraries, contain Java bytecode rather than machine code. Like any other Java program, they must be executed within a Java virtual machine (JVM) that interacts with the server's host operating system to provide an abstract, platform-neutral environment.
JSPs are usually used to deliver HTML and XML documents, but through the use of OutputStream, they can deliver other types of data as well.[4]
The Web container creates JSP implicit objects like request, response, session, application, config, page, pageContext, out and exception. JSP Engine creates these objects during translation phase.
Syntax
Wikibooks has a book on the topic of: J2EE Programming/JavaServer Pages |
JSP pages use several delimiters for scripting functions. The most basic is <% ... %>, which encloses a JSP scriptlet. A scriptlet is a fragment of Java code that is run when the user requests the page. Other common delimiters include <%= ... %> for expressions, where the scriptlet and delimiters are replaced with the result of evaluating the expression, and directives, denoted with <%@ ... %>.[5]
Java code is not required to be complete or self-contained within a single scriptlet block. It can straddle markup content, provided that the page as a whole is syntactically correct. For example, any Java if/for/while blocks opened in one scriptlet must be correctly closed in a later scriptlet for the page to successfully compile.
Content that falls inside a split block of Java code (spanning multiple scriptlets) is subject to that code. Content inside an if block will only appear in the output when the if condition evaluates to true. Likewise, content inside a loop construct may appear multiple times in the output, depending upon how many times the loop body runs.
The following would be a valid for loop in a JSP page:
<p>Counting to three:</p>
<% for (int i=1; i<4; i++) { %>
<p>This number is <%= i %>.</p>
<% } %>
<p>OK.</p>
The output displayed in the user's web browser would be:
Counting to three:
This number is 1.
This number is 2.
This number is 3.
OK.
Expression Language
Version 2.0 of the JSP specification added support for the Expression Language (EL), used to access data and functions in Java objects. In JSP 2.1, it was folded into the Unified Expression Language, which is also used in JavaServer Faces.[6]
An example of EL Syntax:Syntax (programming languages)
The value of "variable" in the object "javabean" is ${javabean.variable}.
Additional tags
The JSP syntax add additional tags, called JSP actions, to invoke built-in functionality.[5] Additionally, the technology allows for the creation of custom JSP tag libraries that act as extensions to the standard JSP syntax.[7] One such library is the JSTL, with support for common tasks such as iteration and conditionals (the equivalent of "for" and "if" statements in Java.)[8]
Compiler
A JavaServer Pages compiler is a program that parses JSPs, and transforms them into executable Java Servlets. A program of this type is usually embedded into the application server and run automatically the first time a JSP is accessed, but pages may also be precompiled for better performance, or compiled as a part of the build process to test for errors.[9]
Some JSP containers support configuring how often the container checks JSP file timestamps to see whether the page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) during software development, and a longer interval (perhaps minutes, or even never) for a deployed Web application.[10]
Criticism
In 2000, Jason Hunter, author of "Java Servlet Programming" described a number of "problems" with JSP.[11]. Nevertheless, he wrote that while JSP may not be the "best solution for the Java Platform" it was the "Java solution that is most like the non-Java solution," by which he meant Microsoft's Active Server Pages. Later, he added a note to his site saying that JSP had improved since 2000, but also cited its competitors, Apache Velocity and Tea (template language).[citation needed]
See also
- Java EE version history
Servlet containers
- GlassFish
- Oracle iPlanet Web Server
- WebSphere Application Server
Java-based template alternatives
- Adobe ColdFusion
- Lucee
- FreeMarker
- JHTML
- Thymeleaf
References
^ Mailing list archive: "Sun JSP 1.0 *not* available"
^ The Life Cycle of a JSP Page (Sun documentation)
^ Understanding JavaServer Pages Model 2 architecture (JavaWorld)
^ Forum thread (JavaRanch): OutputStream already obtained
^ ab JSP 1.2 Syntax Reference
^ The Unified Expression Language (Sun Developer Network)
^ Tag Libraries Tutorial - What is a Tag Library? (Sun) Archived April 19, 2012, at the Wayback Machine.
^ JSTLs documentation (Oracle) bb
^ IBM WebSphere 6.0.2 documentation
^ Sybase EAServer 5.0 documentation
^ The Problems with JSP (January 25, 2000)
Further reading
Bergsten, Hans (2003). JavaServer Pages (3rd ed.). O'Reilly Media. ISBN 978-0-596-00563-4..mw-parser-output cite.citation{font-style:inherit}.mw-parser-output q{quotes:"""""""'""'"}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-limited a,.mw-parser-output .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}
Brown, Simon; Dalton, Sam; Jepp, Daniel; Johnson, Dave; Li, Sing; Raible, Matt. Pro JSP 2. Apress. ISBN 1-59059-513-0.
Hanna, Phil (2003). JSP 2.0 - The Complete Reference. McGraw-Hill Osborne Media. ISBN 978-0-07-222437-5.
Sierra, Kathy; Bates, Bert; Basham, Bryan. Head First Servlets & JSP. O'Reilly Media. ISBN 978-0-596-00540-5.
External links
Wikimedia Commons has media related to JavaServer Pages. |
Wikibooks has a book on the topic of: J2EE Programming/JavaServer Pages |
- JSP v2.0 Syntax Reference
- JavaServer Pages
- JavaServer Pages (JSP) and JSTL
JavaServer Pages v2.0 Syntax Card (html)- Official tutorial: The Java EE 5 Tutorial, Chapter 5, JavaServer Pages Technology
- Servlet History
set up the JSP environment JSP Latest- web-mode.el, emacs major mode for editing jsp templates