280 likes | 452 Views
Scripting With JSP Elements. JavaServer Pages By Xue Bai. Objectives. In this chapter, you will: Use the JSP page directives to determine some characteristics of JSP pages Use JSP include directive and include action to include content from another file Use simple JSP scripting elements
E N D
Scripting With JSP Elements JavaServer Pages By Xue Bai Chapter 5
Objectives In this chapter, you will: • Use the JSP page directives to determine some characteristics of JSP pages • Use JSP include directive and include action to include content from another file • Use simple JSP scripting elements • Understand variable duration • Use JSP comments to make your code more readable Chapter 5
Page Directives • Page directive attributes apply to the entire JSP page: <%@ page attribute1=”value1” attribute2=”value2” attribute3=… %> Chapter 5
Import Attribute • Make Java classes available to the scripting environment • Use Java API to add power to your JSP script • When a Java class is imported to a JSP page, you can reference this class without explicitly specify the class package names <%@ page import=”java.util.*,java.sql.*” %> Chapter 5
Using java.util.Date class • Without import: <% java.util.Date today = new java.util.Date();%> • If the class imported, then: <% Date today = new Date(); %> Chapter 5
Session Attribute • A session attribute can hold information across multiple requests • A session begins when a user requests a JSP page at the first time • The session attribute specifies how the page uses the predefined session object in JSP • Default value is true • If the value is set to false, then no session is created Chapter 5
Session Object • The method isNew() tests whether the session is newly created • The method getCreationTime() returns when the session is created Chapter 5
Buffer Attribute • Specifies the size of the buffer used by the implicit out variable <%@ page buffer=”sizekb” %> <%@ page buffer=”none” %> • If a buffer is specified then output is buffered with a buffer size not less than the specified size Chapter 5
Clear Buffer • out.clear() clears whatever is buffered in the output stream <%@ page buffer=“16kb" %> • Let's send some text to the HTML output stream<br> • The content is buffered <% out.clear(); %> • The buffered content is not cleared Chapter 5
isThreadSafe Attribute • Specify whether to allow more than one thread to execute the JSP code <%@ page isThreadSafe=”true” %> <%@ page isThreadSafe=”false” %> Chapter 5
Thread • A thread is a process that handles client requests by executing the code sequentially • Each client request for this page generates a thread that requests access to this code segment Chapter 5
AutoFlush • Specify whether the buffered output should be sent automatically when the buffer is filled: <%@ page autoFlush=”true” %> <%@ page autoFlush=”false” %> Chapter 5
ErrorPage • Specify a JSP page that can handle any exceptions that are not handled in the current page <%@ page errorPage=“relativeURL”%> Chapter 5
IsErrorPage • Specify whether or not the current page can act as the error page for other JSP pages <%@ page isErrorPage=”true” %> <%@ page isErrorPage=”false” %> (default is false) Chapter 5
Include Files • Usage: • Provide the same navigation links and footer on each of the pages • Reuse JSP code Chapter 5
Include Directive <%@ include file=" relative URL " %> • The content of the specified page is read at translation time (when it is converted into its corresponding servlet) and is merged with the original page when the page is requested for the first time • After the original JSP page has been request, updating included file has no impact on the original file Chapter 5
Include Action <jsp:include page="Relative URL" flush="true" /> • The action does not merge the actual contents of the specified page at translation time • The page is included each time the JSP page is requested • That means whenever you modify the included file, the changes are reflected the next time the page is requested Chapter 5
Declaration • A declaration is used to declare variables and methods in the scripting language used in a JSP page • The variables and methods declared are inserted into the servlet class generated when the JSP container translates the JSP page • A declaration has the following syntax: <%! Variable and method %> Chapter 5
Declaration Example <%! int count = 0; public int getCount(){ return ++count; } %> • Variables are shared among all clients Chapter 5
JSP Expressions • Insert a value directly into the output stream: <%= “Hello!” %> Chapter 5
JSP Scriptlets • A scriptlet is a code fragment that is executed at request-processing time • You can embed any valid Java language code inline between the <% and %> tags • The basic syntax is: <% scriptlets %> Chapter 5
Variable Duration • The duration of variable (also called its lifetime) is the period during which the variable exists in memory • Variables of instance duration exist from the point at which the corresponding servlet that defines them is loaded into memory • Local variables are created when program control reaches their declaration Chapter 5
Variable Duration • They exist while the block in which they are declared is active, and they are destroyed when the block in which they are declared is exited • Variables defined in declaration have instance duration • Variables defined in scriptlet have local duration Chapter 5
Comments • Improve your program’s readability • Ignored when the program is translated • There are two types of comments: • Content comments • And server-side comments Chapter 5
Content Comments • Sent back the client via response output stream • They are not displayed on the browser • The same syntax as comments in HTML: <!– content comments --> • It can include dynamic data: <!-- JSP Page = <%= javax.servlet.http.HttpUtils.getRequestURL(request)%> --> Chapter 5
Server-side Comments • They are ignored at translation time • They are not sent to clients <%-- multiple line comments -- %> <% /* Multiple line comments */ %> <% //single line comment %> Chapter 5