fbpx
Wikipedia

Jakarta Server Pages

Jakarta Server Pages (JSP; formerly JavaServer Pages) is a collection of technologies that helps software developers create dynamically generated web pages based on HTML, XML, SOAP, or other document types. Released in 1999 by Sun Microsystems,[1] JSP is similar to PHP and ASP, but uses the Java programming language.

JSP
Filename extension
.jsp, .jspx, .jspf
Internet media type
application/jsp[citation needed]
Developed byEclipse Foundation
Initial release1999; 24 years ago (1999)
Latest release
3.1
April 31, 2022; 19 months ago (2022-04-31)
Type of formatDynamic web page
StandardJSR 245
Open format?Yes
Websiteprojects.eclipse.org/projects/ee4j.jsp

To deploy and run Jakarta Server Pages, a compatible web server with a servlet container, such as Apache Tomcat or Jetty, is required.

Overview edit

 
The JSP Model 2 architecture.

Architecturally, JSP may be viewed as a high-level abstraction of Jakarta 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]

Jakarta Server Pages 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. The resulting page is 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 .jar or Java program, code 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 edit

Directives, scriptlets, and expressions, declaration edit

JSPs use several delimiters for scripting functions. The most basic is <% ... %>, which encloses a JSP scriptlet. A scriptlet is a fragment of Java code[5] that runs 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][6]

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. This allows code to be intermingled and can result in poor programming practices.

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.

Example edit

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. 

Standard JSP Tags edit

The useBean Tag edit

The JSP useBean tag enables the developer to access and create a Javabean.[7] Although using the useBean tag looks similar to an HTML tag, all JSP tags for JavaBeans use XML syntax. Therefore the code containing the useBean tag is case-sensitive.[8]

The useBean tag contains several attributes. The id attribute declares the name that is used for gaining access to the bean. The class attribute declares the package and class for the bean. The scope declares the object responsible for storing the bean. The value for the scope defines the duration for which the bean is available for the rest of the java application to use. The scope can be one of the following four values: [8]

  • The page scope implies that the bean is located in the implicitly defined PageContext object, and is only available for the current page. By default, all beans have a scope of page.
  • The request scope implies that the bean can be found in the HttpServletRequest object. This bean can be accessed by all other JSPs and servlets that have access to the current request object.
  • The session scope implies that the bean can be found in the HttpSession object. This bean can be accessed by all other JSPs and servlets that have access to the specified HttpSession object.
  • The application scope implies that the bean can be found in the ServletContext object. This bean can be accessed by all other JSPs and servlets that have access to the specified ServletContext object.

The getProperty and setProperty Tags edit

After a bean has been created using the useBean tag, the getProperty and setProperty tags can be used for getting and setting the properties of the bean. The JSP getProperty is used to get the property of created bean. The JSP setProperty tag is used to set the properties for a bean. For the getProperty and setProperty tags, the name attribute is used to specify the bean's name. So the name attribute must match the id attribute provided by the useBean tag.[9]

Expression Language edit

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.[10]

The JSP Expression Language uses a compact syntax which enables the developer to get attributes and JavaBean properties from a given request object. When using EL, a dollar sign ("$") must be added at the beginning of the code. The dollar symbol is followed by an opening brace ("{"), as well as a closing brace ("}"). The code is then written between the opening and closing braces.[11]

Example edit

The following is an example of EL syntax:

The value of variable in the object javabean is ${javabean.variable}.

Additional tags edit

The JSP syntax add additional tags, called JSP actions, to invoke built-in functionality.[6] Additionally, the technology allows for the creation of custom JSP tag libraries that act as extensions to the standard JSP syntax.[12] One such library is the JSTL.[13]

Jakarta Standard Tag Library edit

Jakarta Standard Tag Library (JSTL) supports common tasks that must be performed in JSPs.[14] Examples includes iteration and conditionals (the equivalent of "for" and "if" statements in Java).[13]

Out of all the libraries in JSTL, the JSTL core library is most commonly used. A taglib directive must be used to specify the URI of the JSTL core library using a prefix. Although there are many different choices for the prefix, the "c" prefix is commonly used for this library.[15]

XML-compliant JSP edit

JSP pages may also be written in fully valid XML syntax.[16] Such JSP files commonly use the alternative .jspx file extension, which usually causes the application server to validate the XML syntax.

Since the usual JSP syntax <% ... %> is not valid in XML, a developer must use alternative tags provided by JSP. For example, the common <%@ page .. %> directive may instead be written as a <jsp:directive.page .. /> tag, and tag libraries are imported using XML namespaces, instead of the usual <%@ taglib .. %> tag.

Compiler edit

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.[17]

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.[18]

Criticism edit

According to Joel Murach and Michael Urban, authors of the book "Murach's Java Servlets and JSP", embedding Java code in JSP is generally bad practice.[19] A better approach would be to migrate the back-end logic embedded in the JSP to the Java code in the Servlet.[19] In this scenario, the Servlet is responsible for processing, and the JSP is responsible for displaying the HTML,[19] maintaining a clear separation of concerns.

In 2000, Jason Hunter, author of "Java Servlet Programming" described a number of "problems" with JavaServer Pages.[20] 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).[20] Today, several alternatives and a number of JSP-oriented pages in larger web apps are considered to be technical debt.

See also edit

Servlet containers edit

Java-based template alternatives edit

References edit

  1. ^ "FoRK Archive: Sun JSP 1.0 *not* available". www.xent.com.
  2. ^ The Life Cycle of a JSP Page (Sun documentation)
  3. ^ Seshadri, Govind (December 29, 1999). "Understanding JavaServer Pages Model 2 architecture". JavaWorld. Retrieved 2020-07-17.
  4. ^ "OutputStream already obtained (JSP forum at Coderanch)". coderanch.com.
  5. ^ a b Murach & Urban 2014, pp. 180–182, §2 Essential servlet and JSP skills - How to use JSP tags.
  6. ^ a b JSP 1.2 Syntax Reference
  7. ^ Murach & Urban 2014, pp. 198, §2 Essential servlet and JSP skills - Summary.
  8. ^ a b Murach & Urban 2014, pp. 186–187, §2 Essential servlet and JSP skills - How to code the useBean tag.
  9. ^ Murach & Urban 2014, pp. 188, §2 Essential servlet and JSP skills - How to code the getProperty and setProperty tags.
  10. ^ The Unified Expression Language (Sun Developer Network)
  11. ^ Murach & Urban 2014, pp. 176–177, §2 Essential servlet and JSP skills - How to use EL to get attributes and JavaBean properties.
  12. ^ Tag Libraries Tutorial - What is a Tag Library? (Sun) April 19, 2012, at the Wayback Machine
  13. ^ a b "JavaServer Pages Standard Tag Library - The Java EE 5 Tutorial". docs.oracle.com.
  14. ^ Murach & Urban 2014, pp. 270–273, §2 Essential servlet and JSP skills -An Introduction to JSTL.
  15. ^ Murach & Urban 2014, pp. 178, §2 Essential servlet and JSP skills - How to enable the core JSTL library.
  16. ^ "The Java EE 5 Tutorial, Chapter 6 JavaServer Pages Documents". oracle.com. Retrieved 2022-07-27.
  17. ^ "IBM Docs". www.ibm.com.
  18. ^ "SyBooks Online". infocenter.sybase.com.
  19. ^ a b c Murach & Urban 2014, pp. 46–47, §1 Get started right - The JSP for the second page.
  20. ^ a b The Problems with JSP (January 25, 2000)

Works cited edit

  • Murach, Joel; Urban, Michael (2014). Murach's Java Servlets and JSP. Mike Murach & Associates. ISBN 978-1-890774-78-3.

Further reading edit

External links edit

  • Official website  
  • (v1.2)
  • Jakarta Server Pages Specification, Latest
  • Official tutorial: The Java EE 5 Tutorial, Chapter 5, JavaServer Pages Technology

jakarta, server, pages, formerly, javaserver, pages, collection, technologies, that, helps, software, developers, create, dynamically, generated, pages, based, html, soap, other, document, types, released, 1999, microsystems, similar, uses, java, programming, . Jakarta Server Pages JSP formerly JavaServer Pages is a collection of technologies that helps software developers create dynamically generated web pages based on HTML XML SOAP or other document types Released in 1999 by Sun Microsystems 1 JSP is similar to PHP and ASP but uses the Java programming language JSPFilename extension jsp jspx jspfInternet media typeapplication jsp citation needed Developed byEclipse FoundationInitial release1999 24 years ago 1999 Latest release3 1April 31 2022 19 months ago 2022 04 31 Type of formatDynamic web pageStandardJSR 245Open format YesWebsiteprojects wbr eclipse wbr org wbr projects wbr ee4j wbr jspTo deploy and run Jakarta Server Pages a compatible web server with a servlet container such as Apache Tomcat or Jetty is required Contents 1 Overview 2 Syntax 2 1 Directives scriptlets and expressions declaration 2 1 1 Example 2 2 Standard JSP Tags 2 2 1 The useBean Tag 2 2 2 The getProperty and setProperty Tags 2 3 Expression Language 2 3 1 Example 2 4 Additional tags 2 4 1 Jakarta Standard Tag Library 2 5 XML compliant JSP 3 Compiler 4 Criticism 5 See also 5 1 Servlet containers 5 2 Java based template alternatives 6 References 6 1 Works cited 7 Further reading 8 External linksOverview edit nbsp The JSP Model 2 architecture Architecturally JSP may be viewed as a high level abstraction of Jakarta 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 Jakarta Server Pages 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 The resulting page is 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 jar or Java program code 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 editDirectives scriptlets and expressions declaration edit JSPs use several delimiters for scripting functions The most basic is lt gt which encloses a JSP scriptlet A scriptlet is a fragment of Java code 5 that runs when the user requests the page Other common delimiters include lt gt for expressions where the scriptlet and delimiters are replaced with the result of evaluating the expression and directives denoted with lt gt 5 6 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 This allows code to be intermingled and can result in poor programming practices 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 Example edit The following would be a valid for loop in a JSP page lt p gt Counting to three lt p gt lt for int i 1 i lt 4 i gt lt p gt This number is lt i gt lt p gt lt gt lt p gt OK lt p gt 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 Standard JSP Tags edit The useBean Tag edit The JSP useBean tag enables the developer to access and create a Javabean 7 Although using the useBean tag looks similar to an HTML tag all JSP tags for JavaBeans use XML syntax Therefore the code containing the useBean tag is case sensitive 8 The useBean tag contains several attributes The id attribute declares the name that is used for gaining access to the bean The class attribute declares the package and class for the bean The scope declares the object responsible for storing the bean The value for the scope defines the duration for which the bean is available for the rest of the java application to use The scope can be one of the following four values 8 The page scope implies that the bean is located in the implicitly defined PageContext object and is only available for the current page By default all beans have a scope of page The request scope implies that the bean can be found in the HttpServletRequest object This bean can be accessed by all other JSPs and servlets that have access to the current request object The session scope implies that the bean can be found in the HttpSession object This bean can be accessed by all other JSPs and servlets that have access to the specified HttpSession object The application scope implies that the bean can be found in the ServletContext object This bean can be accessed by all other JSPs and servlets that have access to the specified ServletContext object The getProperty and setProperty Tags edit After a bean has been created using the useBean tag the getProperty and setProperty tags can be used for getting and setting the properties of the bean The JSP getProperty is used to get the property of created bean The JSP setProperty tag is used to set the properties for a bean For the getProperty and setProperty tags the name attribute is used to specify the bean s name So the name attribute must match the id attribute provided by the useBean tag 9 Expression Language edit Main article Jakarta 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 10 The JSP Expression Language uses a compact syntax which enables the developer to get attributes and JavaBean properties from a given request object When using EL a dollar sign must be added at the beginning of the code The dollar symbol is followed by an opening brace as well as a closing brace The code is then written between the opening and closing braces 11 Example edit The following is an example of EL syntax The value of variable in the object javabean is javabean variable Additional tags edit The JSP syntax add additional tags called JSP actions to invoke built in functionality 6 Additionally the technology allows for the creation of custom JSP tag libraries that act as extensions to the standard JSP syntax 12 One such library is the JSTL 13 Jakarta Standard Tag Library edit Main article Jakarta Standard Tag Library Jakarta Standard Tag Library JSTL supports common tasks that must be performed in JSPs 14 Examples includes iteration and conditionals the equivalent of for and if statements in Java 13 Out of all the libraries in JSTL the JSTL core library is most commonly used A taglib directive must be used to specify the URI of the JSTL core library using a prefix Although there are many different choices for the prefix the c prefix is commonly used for this library 15 XML compliant JSP edit JSP pages may also be written in fully valid XML syntax 16 Such JSP files commonly use the alternative jspx file extension which usually causes the application server to validate the XML syntax Since the usual JSP syntax lt gt is not valid in XML a developer must use alternative tags provided by JSP For example the common lt page gt directive may instead be written as a lt jsp directive page gt tag and tag libraries are imported using XML namespaces instead of the usual lt taglib gt tag Compiler editA 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 17 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 18 Criticism editAccording to Joel Murach and Michael Urban authors of the book Murach s Java Servlets and JSP embedding Java code in JSP is generally bad practice 19 A better approach would be to migrate the back end logic embedded in the JSP to the Java code in the Servlet 19 In this scenario the Servlet is responsible for processing and the JSP is responsible for displaying the HTML 19 maintaining a clear separation of concerns In 2000 Jason Hunter author of Java Servlet Programming described a number of problems with JavaServer Pages 20 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 20 Today several alternatives and a number of JSP oriented pages in larger web apps are considered to be technical debt See also edit nbsp Computer programming portalServlet containers edit Apache Tomcat Apache TomEE Jetty web server GlassFish Oracle iPlanet Web Server WebSphere Application ServerJava based template alternatives edit Adobe ColdFusion Lucee FreeMarker JHTML ThymeleafReferences edit FoRK Archive Sun JSP 1 0 not available www xent com The Life Cycle of a JSP Page Sun documentation Seshadri Govind December 29 1999 Understanding JavaServer Pages Model 2 architecture JavaWorld Retrieved 2020 07 17 OutputStream already obtained JSP forum at Coderanch coderanch com a b Murach amp Urban 2014 pp 180 182 2 Essential servlet and JSP skills How to use JSP tags a b JSP 1 2 Syntax Reference Murach amp Urban 2014 pp 198 2 Essential servlet and JSP skills Summary a b Murach amp Urban 2014 pp 186 187 2 Essential servlet and JSP skills How to code the useBean tag Murach amp Urban 2014 pp 188 2 Essential servlet and JSP skills How to code the getProperty and setProperty tags The Unified Expression Language Sun Developer Network Murach amp Urban 2014 pp 176 177 2 Essential servlet and JSP skills How to use EL to get attributes and JavaBean properties Tag Libraries Tutorial What is a Tag Library Sun Archived April 19 2012 at the Wayback Machine a b JavaServer Pages Standard Tag Library The Java EE 5 Tutorial docs oracle com Murach amp Urban 2014 pp 270 273 2 Essential servlet and JSP skills An Introduction to JSTL Murach amp Urban 2014 pp 178 2 Essential servlet and JSP skills How to enable the core JSTL library The Java EE 5 Tutorial Chapter 6 JavaServer Pages Documents oracle com Retrieved 2022 07 27 IBM Docs www ibm com SyBooks Online infocenter sybase com a b c Murach amp Urban 2014 pp 46 47 1 Get started right The JSP for the second page a b The Problems with JSP January 25 2000 Works cited edit Murach Joel Urban Michael 2014 Murach s Java Servlets and JSP Mike Murach amp Associates ISBN 978 1 890774 78 3 Further reading editBergsten Hans 2003 JavaServer Pages 3rd ed O Reilly Media ISBN 978 0 596 00563 4 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 amp JSP O Reilly Media ISBN 978 0 596 00540 5 External links edit nbsp Wikibooks has a book on the topic of J2EE Programming JavaServer Pages nbsp Wikimedia Commons has media related to Jakarta Server Pages nbsp Wikibooks has a book on the topic of J2EE Programming JavaServer Pages Official website nbsp JSP v2 0 Syntax Reference JavaServer Pages v2 0 Syntax Card v1 2 Jakarta Server Pages Specification Latest Official tutorial The Java EE 5 Tutorial Chapter 5 JavaServer Pages Technology Servlet History Retrieved from https en wikipedia org w index php title Jakarta Server Pages amp oldid 1184632268 Compiler, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.