My first JSP page Parameter value is A JSP file interweaved with HTML codes and JSP scriplets"> My first JSP page Parameter value is A JSP file interweaved with HTML codes and JSP scriplets">
Download presentation
Presentation is loading. Please wait.
Published byShannon Elliott Modified over 9 years ago
1
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics
2
The JSP Framework JavaServer Pages (JSP) is a Java technology that allows Java code and certain pre-defined actions to be embedded into static content. JSPs are compiled into Java Servlets by a JSP compiler. Architecturally, JSP can be viewed as a high-level abstraction of servlets that is implemented as an extension of the Servlet API. Ref: Wikipedia
3
<% String param = request.getParameter("ParamName"); %> My first JSP page Parameter value is. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 A JSP file interweaved with HTML codes and JSP scriplets
4
JSP vs. Servlet JSP (corresponding class)Servlet jspInit()init() jspDestroy()destroy() _jspService(request, response) service(request, response) A JavaServer page can be viewed a Java class written in "different language". A JavaServer page is first compiled into a Servlet class and then into Java byte code. It has the same lifecycle as a servlet (init, serve, destroy) Static contents,, and elements in a JSP Page are translated into Java codes and placed in _jspService()
5
JSP Syntax A JavaServer Page may be broken down into the following pieces: Static data such as HTML JSP scripting elements and variables JSP actions JSP directives Custom tags with correct library
6
Predefined Variables (Implicit Objects) These objects are automatically made available in a JSP page (they are declared as local variables in _jspService() ) request The HttpServletRequest object response The HttpServletResponse object out The stream (of type JspWriter ) used to send output to the client application An instance of ServletContext. It is the object obtained by invoking getServletContext(). We can use this object to share data among all servlets and JSP pages in the same application.
7
Predefined Variables (Implicit Objects) session The HttpSession object associated with the request (unless disabled with the session attribute of the page directive) page The servlet itself (for self reference). pageContext A PageContext instance that contains data associated with the whole page. PageContext config An instance of ServletConfig. It is the object obtained by calling getServletConfig(). exception Represent an exception not caught by the application code in an "Error handling JSP page".
8
Implicit objects and their corresponding class application: javax.servlet.ServletContext config: javax.servlet.ServletConfig exception: java.lang.Throwable out: javax.servlet.jsp.JspWriter page: java.lang.Object pageContext: javax.servlet.jsp.PageContext request: javax.servlet.ServletRequest response: javax.servlet.ServletResponse session: javax.servlet.http.HttpSession
9
JSP Elements Three types of JSP elements: Directive elements Scripting elements Action elements JSP Elements have two forms: the XML form the alternative form
10
Comments Comments Discarded by JSP container Won't appear in the output Treated as template data. Reproduced in the output Note: --> is produced as
11
Scripting Elements Allow you to insert Java code in JSP pages. Three types: Expressions Scriplets Declarations
12
Expressions Format: Result The element is replaced by the evaluated result of the expression in the output. Examples Current time: Your hostname: 3 * 4 + 5 = XML-compatible syntax Java Expression
13
Scriptlets Format: Result Code is inserted verbatim into servlet's _jspService() Examples XML-compatible syntax Java Code
14
JSP/Servlet Correspondence Scriptlets in JSP: Possible resulting servlet code: public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Initialization code... out.println(foo()); bar();... } No ';' after foo() as it represents an expression and not a statement
15
JSP Scriptlets Example <% for (int i=100; i>=0; i--) { %> bottles of beer on the wall. <% } %> for (int i=100; i>=0; i--) { out.println(i); out.println(" bottles of beer on the wall. "); } 100 bottles of beer on the wall. 99 bottles of beer on the wall. 98 bottles of beer on the wall. 97 bottles of beer on the wall. 96 bottles of beer on the wall. 95 bottles of beer on the wall. 94 bottles of beer on the wall. … JSP Resulting Servlet Output appears in browser
16
Example Using JSP Scriptlets Color Testing <% String bgColor = request.getParameter("bgColor"); boolean hasExplicitColor; if (bgColor != null) hasExplicitColor = true; else { hasExplicitColor = false; bgColor = "WHITE"; } %> "> …
17
Declarations Format: Result Code is inserted verbatim into servlet's class definition, outside of any existing methods Use this to introduce instance/static variables and methods Examples XML-compatible syntax Java Code
18
Original JSP Some Heading <%! private String randomHeading() { return(" " + Math.random() + " "); } %> Possible resulting servlet code public class xxxx implements HttpJspPage { private String randomHeading() { return(" " + Math.random() + " "); } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... out.println(" Some Heading "); out.println(randomHeading());... } JSP/Servlet Correspondence
19
Original JSP Possible resulting servlet code public class xxxx implements HttpJspPage { int age1 = 100; public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... int age2 = 100;... } JSP/Servlet Correspondence (Declaration vs. Sniplet)
20
Example: JSP Tags + HTML Tags Table of Square Roots Number Square Root <% for (int n=0; n<=100; n++) { %> <% } %>
21
References Wikipedia: JavaServer Pages http://en.wikipedia.org/wiki/JavaServer_Pages Free Tutorial (Java, JSP, Java Servlets) http://www.courses.coreservlets.com/Course-Materials/ Sample JSP codes http://www.java2s.com/Code/Java/JSP/CatalogJSP.htm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.