core jsp - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · jsp-servlet source code and...

35
ATIJ Core JSP 1/35 Core JSP Advanced Topics in Java Khalid Azim Mughal [email protected] http://www.ii.uib.no/~khalid/atij/ Version date: 2006-09-04

Upload: others

Post on 02-Jun-2020

38 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 1/35

Core JSP

Advanced Topics in Java

Khalid Azim [email protected]

http://www.ii.uib.no/~khalid/atij/

Version date: 2006-09-04

Page 2: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 2/35

Overview• Comparing Servlets and JSP Technologies

• Installing JSP Pages under Tomcat

• Overview of JSP Syntax and Semantics

• JSP Life Cycle

• Embedding Java Code in Scripting Elements

• Using the JSP page Directive to control JSP-servlet Structure

Page 3: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 3/35

What is JSP (Java Server Pages)?• Java-based technology for developing web pages with dynamic content.

• JSP is server-side technology, included in the J2EE platform.

• Current specification: JSP 2.0– Defines the syntax and semantics of a JSP page.

• Imprecise analogies:– Servlets are Java with embedded HTML.– JSP pages are HTML with embedded Java.

Page 4: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 4/35

Why use JSP?• Disadvantages with servlets:

– Tedious writing and maintaining HTML content using print-methods.– Requires programming skills as HTML content in a servlet is not accessible to non-

programmers.– Content (i.e. data) and presentation (i.e. HTML to present the data) of a web page are

tightly coupled.

• Advantages with JSP (when used with servlets):– Supports both scripting and element-based dynamic content.

• Easier writing and maintaining HTML content.– Compiled to a servlet when requested for the first time, and subsequent requests

are processed by the servlet.– Clear division of labour: Programmers can deal with content (i.e. processed by

servlets), non-programmers can deal with presentation (i.e. HTML code specified by JSP pages).

Wicked Combination for creating Dynamic Content: Servlets + JSP

Page 5: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 5/35

Installing JSP Pages under Tomcat• JSP pages do not require any

specification in the web.xml file of a web application.– Filename extension: .jsp– The JSP page

corresponding to the file name in the requested URI is deployed.

• JSP pages are installed directly under the document root of a web application.

• However, classes of any servlets used by the JSP pages are installed in a package hierarchy under the WEB-INF/classes directory.

• Any libraries used by the JSP pages are installed in the WEB-INF/lib directory.

All web applications are placed in this directory

Web Application Structure

Publicly accessible files, including JSP pages

All information and support files

Class files in their package structure

Other necessary JAR archives

Deployment descriptor file

webapps

<Web server home>

myApp

*.html,*.jsp

...

WEB-INF

classes

no

myCompany

MyServletClass.classlib

*.jar

web.xml

...

Document Root

work Directory where the container storesJSP-servlet source code and class files

Page 6: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 6/35

JSP Syntax and Semantics• Contents of a JSP page can be categorized into two groups:

– Elements– Template Data

• Elements make up the dynamic parts of a JSP page. – Elements define actions whose results are inserted in the response.

• Template data is all static text that is not part of JSP elements.– It is sent unchanged to the client.

Page 7: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 7/35

JSP Elements Overview• Scripting Elements

– Expression: <%= expression %>– Scriplet: <% codeFragment %>– Declaration: <%! memberDeclaration %>

• Directive Elements: <%@ directiveName attribute="value" %>– attribute– include– page– taglib– tag– variable

• Action Elements<jsp:action> ... </jsp:action>

• Custom Elements <prefix:action> ... </prefix:action>

• Expression Language (EL): ${ expression }

• Comments: <%-- whatever --%>

• Escape Character: \

Page 8: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 8/35

JSP Elements Overview (cont.)

JSP Elements Description

HTML Text HTML content passed unchanged to the client.<h2>JSP just simplifies presentation.</h2>

HTML Comments A HTML comment is passed through unchanged to the client who ignores it.<!-- Client, please ignore me. -->

Template Text Text passed unchanged to the client, for example HTML content.

JSP Comments A JSP comment is not passed to the client, and is ignored when the JSP page is translated.<%-- I don’t want to be translated. --%>

JSP Expression Each page request results in the expression being evaluated and its value sent to the client.<%= --timerToWWIII %>

JSP Scriplet Each page request results in the statements being executed.<% if (timerToWWIII <= 0) { %>

JSP Declaration The declared member becomes part of the servlet class that is generated when the page is translated.<%! int timerToWWIII = 24; %>

JSP Directive A directive specifies high-level information about the page.<%@ include file="form.html" %>

Page 9: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 9/35

JSP Action Actions are operations performed when the page is requested.<jsp:forward page="errorPage.jsp" />

JSP Expression Language (EL) EL expressions can be used in template text and as attribute values of actions.${anArry[0]}

Custom Tag (Custom Action) JSTL Action Libraries are standardized custom tags.<c:redirect url="http://www.tryelsewhere.com/" />

JSP Elements Description

Page 10: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 10/35

JSP Life Cycle

[not initialized]

[load]

[request][destroy]

Initialized

Instantiated

Servicing

• Translation Phase: a JSP page is translated into a servlet that is compiled when requested for the first time.

• Otherwise, it is analogous to a standard servlet.

• Before a JSP-servlet can process requests, it must be loaded and instantiated.

• The JSP-servlet is initialized once during its life time. As part of the initialization, the jspInit() method of the JSP-servlet is also called.

• Request Handling Phase: Once instantiated and loaded, the JSP-servlet is ready for servicing requests. The method _jspService() is called to elicit a response.

• When no longer needed, the JSP-servlet is destroyed. Before destroying the JSP-servlet, the jspDestroy() method of the JSP-servlet is called.

• A JSP-servlet is run in a JSP container.

• A JSP developer does not deal directly with the code of the JSP-servlet.

• Under Tomcat, one can designate where the code of the JSP-servlet is placed (source code + class files), usually under the work directory of the web application.

Page 11: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 11/35

Processing a Request with JSP• Request processing can involve the following, depending on the timing of the

request:– The JSP page is translated to a JSP-servlet.– The JSP-servlet is compiled to a class file.– The class file is loaded and instantiated.– The JSP-servlet instance is initialized.– The JSP-servlet instance creates the response.

Page 12: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 12/35

Embedding Java Code in Scripting Elements• Template Text

– Content that is not in the scope of a JSP element.– Static text that is sent unchanged to the client.– Translation of template text results in calls to the print-methods of the JSPWriter

associated with the response. – HTML content is template text, but JSP comments are not.

• JSP scripting elements in a JSP page allow Java code to be inserted in the corresponding JSP-servlet.– Expression: <%= expression %>– Scriplet: <% codeFragment %>– Declaration: <%! memberDeclaration %>

• It is instructive to see how scripting elements are translated into Java code that is generated in the JSP-servlet.

Page 13: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 13/35

Implicit Objects in JSP• Defined as local variables in _jspService() method of the JSP-servlet.

• Scripting elements can access these implicit objects for their own purpose.

Implicit Object Type

request HttpServletRequest

response HttpServletResponse

session HttpSession

out JSPWriter, associated with the responseapplication ServletContext

config ServletConfig

pageContext PageContext, repository for other implicit objects.page Synonym for this reference

Page 14: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 14/35

JSP Expressions: <%= expression %>• JSP expressions allow computed values to be inserted into the response.

– A value is computed by the Java expression in an JSP expression.

• An JSP expression is usually translated to print() or write() method calls on the out object.– The calls are inserted into the _jspService() method of the JSP-servlet.– The Java expression in the JSP expression is passed as argument to the print call.

Page 15: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 15/35

Example of JSP Expressions: <%= expression %>

JSP (/horoscopeJSP/SimpleJSP.jsp) JSP-Servlet (work/org/apache/jsp/SimpleJSP_jsp.java)

<html><head><title>Hi!</title></head><body><h1>Hi! Wanna know your horoscope?</h1><br/><%= new java.util.Date() %></body></html>

out.write("<html>\r\n");out.write("<head><title>Hi!</title></head>\r\n");out.write("<body>\r\n");out.write("<h1>\r\n");out.write("Hi! Wanna know your horoscope?\r\n");out.write("</h1>\r\n");out.write("<br/>\r\n");out.print( new java.util.Date() );out.write("\r\n");out.write("</body>\r\n");out.write("</html>");

Page 16: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 16/35

JSP Scriptlets: <% Java code %>• JSP scriptlets allow arbitrary Java code to be inserted into the _jspService() method

of the JSP-servlet.

Example: /horoscopeJSP/StarSigns.jsp <% String[] horoscopeSign = { "aquarius", "pisces", "aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn" }; for (int i = 0; i < horoscopeSign.length; i++) out.print(horoscopeSign[i] + "<br/>"); %> ... <% int i; for (i = 0; i < horoscopeSign.length; i++) { %> Sign <%= i %>: <%= horoscopeSign[i] %> <br/> <% } %>

Page 17: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 17/35

Example of JSP Scriptlets: <% Java code %>

continued

Page 18: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 18/35

JSP Declarations: <%! Member declaration %>• JSP declarations allow member declarations to be inserted into the JSP-servlet.

Example 1: /horoscopeJSP/StarSignsII.jsp (see screenshots for /horoscopeJSP/StarSigns.jsp) <%! private String[] horoscopeSign = { "aquarius", "pisces", "aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn" }; %> ... <% for (int i = 0; i < horoscopeSign.length; i++) out.print(horoscopeSign[i] + "<br/>"); %> ... <% int i; for (i = 0; i < horoscopeSign.length; i++) { %> Sign <%= i %>: <%= horoscopeSign[i] %> <br/> <% } %>

Page 19: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 19/35

• JSP declarations can be used to override the jspInit() and jspDestroy() methods, if necessary to do any initialization or cleanup, respectively.

• JSP declarations are used in conjunction with JSP scriptlets and expressions.

• Note that white space is preserved as it is appears in a JSP page.

Page 20: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 20/35

More Examples with Scripting Elements• Run from http://localhost:8080/horoscopeJSP/.

Example 2: Files SimpleHoroscope.html and SimpleHoroJSP.jsp<%! int visitCount = 0; %><html> <head><title>Your Daily Horoscope</title></head> <body><h1> Simple Horoscope (JSP Version) </h1> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td>Sign</td> <td>Horoscope</td> </tr> <tr> <td> <%= request.getParameter("sign").toUpperCase() %> </td> <td> If you think knowing your horoscope is going to help you, you are gravely mistaken. </td> </tr> </table> <h4>Good luck! You are going to need it.</h4> <p> No. of visits: <%= ++visitCount %> </p></body></html>

Page 21: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 21/35

Example 2: Screenshot for SimpleHoroJSP.jsp

Page 22: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 22/35

Example 3: Files SimpleHoroscopeII.html and SimpleHoroJSPII.jsp (See screenshots for SimpleHoroJSP.jsp)...<%! protected String servletName = "Simple Horoscope"; protected String getHoroscope(String sign) { return "If you think knowing your horoscope " + "is going to help you, you are gravely mistaken."; }%>... <body><h1> <%= servletName + " (JSP Version)" %> </h1>... <tr> <% String sign = request.getParameter("sign").toUpperCase(); %> <td> <%= sign %> </td> <td> <%= getHoroscope(sign) %></td> </tr>...

Page 23: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 23/35

• Example showing some pitfalls with scripting elements (see file: /horoscopeJSP/StarSignsII.jsp) <%! private String[] horoscopeSign = { "aquarius", "pisces", "aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn" }; private void printStars() { for (int i = 0; i < horoscopeSign.length; i++) out.print(horoscopeSign[i] + "<br/>"); // Writes directly to out which // is only available in _jspService() method } %> Signs: <%= printStars(); %> <%-- Two errors. Return type is void, so nothing will get printed if the call was executed. Syntax error: semi-colon should not terminate the expression. -->

Page 24: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 24/35

Thread-safety and JSP Scripting Elements• Expressions and scriptlets are thread-safe for each request (and therefore each thread)

since they are translated to code which is local to the _jspService() method.

• Declarations are not thread-safe since they are accessible to any thread accessing the JSP-servlet class or instance.

Page 25: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 25/35

Best Practises• Limit the Java code embedded in JSP pages.

• Use packages to organize the Java classes used in JSP pages.

Page 26: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 26/35

Using the JSP page Directive to control JSP-servlet Structure• Syntax:

<%@ directiveName attribute1="value1" ... attributek="valuek" %>

• The JSP page directive can be used to control the following aspects of the JSP-servlet:– Importing classes– Extending/customizing the servlet superclass– Setting the content type– ...

• The JSP page directive can be placed anywhere in the JSP page, and multiple JSP page directives are allowed in a JSP page.

• The JSP page directive has the following attributes to control various aspects relating to the JSP-servlet:

import contentType pageEncoding

session isELIgnored buffer

autoFlush info errorPage

isErrorPage language extends

Page 27: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 27/35

Importing Types into the JSP-servlet• Developing strategy:

– Minimize Java code in a JSP page by developing utility classes.– The utility classes must be placed in packages under the /WEB-INF/classes

directory.– Import the required classes into the JSP page using the import attribute of the JSP page directive.

• All types from the following packages are automatically imported:java.langjavax.servletjavax.servlet.httpjavax.servlet.jsp

• Syntax:<%@ page import="p1, ..., pn" %>

there each pi can be either single type import or type import on demand (*).

Page 28: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 28/35

Example: Horoscope from File• See the horoscopeJSP application

• Files HoroscopeInitParams.html, HoroInitParamsJSP.jsp, HoroscopeDataFile.java and web.xml.

• Mixing Java and JSP in the same page can very easily become spaghetti.

We will see mechanisms that allow a better separation of concerns.

Page 29: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 29/35

<%@ page import="java.io.IOException, kam.jsp.*" %><%! int visitCount = 0; %><%! protected String servletName; protected HoroscopeDataFile horoscopeData; public void jspInit() { try { servletName = "Simple Horoscope From File --(JSP Version)"; // Read the parameter from the deployment descriptor. String filename = getInitParameter("HoroscopeDatafile"); System.out.println("Filename: " + filename); // Get the real path of the file. ServletContext context = getServletContext(); String realPath = context.getRealPath(filename); System.out.println("Real path: " + realPath);

horoscopeData = new HoroscopeDataFile(realPath); // horoscopeData.printHoroscope(); } catch (IOException ioe) { System.err.println("I/O Error getting horoscope data"); } }

Page 30: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 30/35

protected String getHoroscope(String sign) { return horoscopeData.getHoroscope(sign); }%><html> <head><title>Your Daily Horoscope</title></head> <body><h1> Simple Horoscope From File (JSP Version) </h1> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td>Sign</td> <td>Horoscope</td> </tr> <tr> <% String sign = request.getParameter("sign").toLowerCase(); %> <td> <%= sign %> </td> <td> <%= getHoroscope(sign) %></td> </tr> </table> <h4>Good luck! You are going to need it.</h4> <p> No. of visits: <%= ++visitCount %></p></body></html>

Page 31: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 31/35

Example: Horoscope from Database• See the horoscopeJSP application

• Files HoroUsingDB.html, HoroUsingDB.jsp, HoroDBConnector.java and web.xml.

Page 32: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 32/35

<%@ page import="java.io.*, javax.naming.*, javax.sql.*, kam.jsp.*" %><%! int visitCount = 0; %><%! protected String servletName; protected HoroDBConnector dbConnector; public void jspInit() { servletName = "Simple Horoscope (JSP) (Using DataSource)"; try { // Obtain the environment naming context. This is the same for all servlets. Context ctx = (Context) new InitialContext(); Context env = (Context) ctx.lookup("java:comp/env"); // "java:comp/env" System.out.println(env == null ? "No Context/env" : "Created Initial Context/Env."); // Look up the required data source String resourceRefName = getInitParameter("resourceRefName"); DataSource pool = (DataSource) env.lookup(resourceRefName); // "jdbc/TestHoro" System.out.println(pool == null ? "No datasource" : "Datasource found."); String dbTableName = getInitParameter("db_table"); dbConnector = new HoroDBConnector(pool, dbTableName); } catch (NamingException e) { e.printStackTrace(); } }%>

Page 33: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 33/35

<html> <head><title>Your Daily Horoscope</title></head> <body><h1> <%= servletName %> </h1> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td>Sign</td> <td>Horoscope</td> </tr> <tr> <% String sign = request.getParameter("sign").toLowerCase(); %> <td> <%= sign %> </td> <td> <%= dbConnector.getHoroscope(sign) %></td> </tr> </table> <h4>Good luck! You are going to need it.</h4> <p> No. of visits: <%= ++visitCount %></p></body></html>

Page 34: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 34/35

Misc. Attributes of the JSP page Directive

Attribute of the page directive Description

import Imports types from packages. contentType Sets the Content-Type response header. Default: text/htmlpageEncoding Sets the character set for the response. Default: ISO-8859-1session Indicates whether the page uses HTTP sessions: true (default

value that creates new session if none exists) or false (error if implicit session object is accessed).

isELIgnored Controls the evaluation of JSP 2.0 EL: true (EL evaluation is ignored) or false (EL is evaluated). Default value depends on the version specified in the <web-app> element in the web.xml file.

buffer Specifies the size (kbytes or none) of the buffer for the implicit out object.

autoFlush Automatically flushes the buffer if true (default). Raises an exception if the buffer overflows when the value is false.

Page 35: Core JSP - ii.uib.nokhalid/atij/atij-core-jsp-web/atij-core-jsp.pdf · JSP-servlet source code and class files. ATIJ Core JSP 6/35 JSP Syntax and Semantics † Contents of a JSP page

ATIJ Core JSP 35/35

info The getServletInfo() method of the servlet can retrieve this information.

errorPage Specifies the error page to process any exceptions thrown but not caught by the page.

isErrorPage A page can act as an error page (true) or not (false, which is default).

Attribute of the page directive Description