Presentation is loading. Please wait.

Presentation is loading. Please wait.

Developing JavaServer Pages

Similar presentations


Presentation on theme: "Developing JavaServer Pages"— Presentation transcript:

1 Developing JavaServer Pages

2 JSP Technology JavaServer Pages (JSP) enable you to write standard HTML pages contaning tags that run powerful programs based on the Java programming language.

3 Hello Servlet Code public void generateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException { // Determine the specified name (or use default) String name = request.getParameter("name"); if ( (name == null) || (name.length() ==0) ) { name = DEFAULTJSTAME; // Specify the content type is HTML response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Generate the HTML response out.printIn("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>Hello Servlet</TITLE>"); out.println("</HEAD>"); out.println("<B0DY BGCOLOR='white'>"); out.printIn("<B>Hello, " + name + "</B>"); out.printIn("</BODY>"); out.println("</HTML>"); out. close () ;

4 Hello JSP Code <%! private static final String DEFAULT_NAME = "World"; %> <HTML> <HEAD> <TITLE>Hello JavaServer Page</TITLE> </HEAD> <%-- Determine the specified name (or use default) --%> <% String name = request.getParameter("name"); if ( (name == null) || (name.length() ==0) ) { name = DEFAULT NAME; } %> <BODY BGCOLOR='white'> <B>Hello, <%= name %></B> </BODY> </HTML>

5 JSP Scripting Elements
JSP scripting elements are embedded with the <% %> tags and are processed by the JSP engine during translation of the JSP page. <HTML> <%-- scripting element --%> </HTML>

6 JSP Scripting Elements
There are five types of scripting elements: Comments <%-- comment --%> Directive tag directive %> Declaration tag <%! dec! %> Scriptlet tag <% code %> Expression tag <%= expr' %>

7 Comments Documentation is important to any application. There are three types of comments permitted in a JSP page: HTML comments <!-- This is an HTML comment. It will show up in the response. --> JSP page comments <%-- This is a JSP comment. It will only be seen in the JSP code. It will not show up in either the servlet code or the response. --%>

8 Comments Java comments can be embedded with scriptlet and declaration tags. /* This is a Java comment. It will show up in the servlet code. It will not show up in the response. */

9 <%@ DirectiveName [attr="value"]* %>
Directive Tag A directive tag provides information that will affect the overall translation of the JSP page. The syntax for a directive tag is: DirectiveName [attr="value"]* %> Examples: page session="false" %> include file="incl/copyright.html" %>

10 <%@ page import="java.util.Date" %>
The page Directive The page directive is used to modify the overall translation of the JSP page. page import="java.util.Date" %> Note: You can have more than one page directive, but can only declare any given attribute once per page, except for the import attribute. You can place a page directive anywhere in the JSP file. It is good practice to make the page directive the first statement in the JSP file.

11 The page Directive The page directive defines a number of page-dependent properties and communicates these to the Web container during translation time. The language attribute defines the scripting language to be used in the page. The extends attribute defines the (fully-qualified) class name of the superclass of the servlet class that is generated from this JSP page.

12 The page Directive The import attribute defines the set of classes and packages that must be imported in the servlet class definition. For example: import="java.sql.Date,java.util.*,j ava.text.*" The session attribute defines whether the JSP page is participating in an HTTP session. The value is either true (the default) or false.

13 The page Directive The buffer attribute defines the size of the buffer used in the outputstream (a JspWriter object). The value is either none or Nkb. For example: buffer="8kb" or buffer="none“ The autoFlush attribute defines whether the buffer output should be flushed automatically when the buffer is filled or whether an exception is thrown. The value is either true (automatically flush) or false (throw an exception).

14 The page Directive The isThreadSafe attribute allows the JSP page developer to declare that the JSP page is thread-safe or not. If the value is set to false, then this attribute instructs JSP parser to write the servlet code such that only one HTTP request will be processed at a time. The default value is true. The info attribute defines an informational string about the JSP page. The errorPage attribute indicates another JSP page that will handle all runtime exceptions thrown by this JSP page. The value is a URL that is either relative to the current Web hierarchy or relative to the context root.

15 The page Directive The isErrorPage attribute defines that the JSP page has been designed to be the target of another JSP page's errorPage attribute. The value is either true or false (default). All JSP pages that "are an error page" automatically have access to the exception implicit variable. The content Type attribute defines the MIME type of the output stream. The default is text/html. The pageEncoding attribute defines the character encoding of the output stream. The default is BO Other character encodings permit the inclusion of non-Latin character sets, such as Kanji or Cyrillic.

16 <%! JavaClassDeclaration %>
Declaration Tag A declaration tag allows you to include members in the JSP servlet class, either attributes or methods. The syntax for a declaration tag is: <%! JavaClassDeclaration %> Examples: <%! public static final String DEFAULT_NAME = "World"; %> <%! public String getName(HttpServletRequest request) { return request.getParameter("name");} <%! int counter = 0; %>

17 Declaration Tag You can use a declaration tag to override the jsplnit and jspDestory life cycle methods. The signature of these methods has no arguments and returns void. For example: <%! public void jsplnit() { /* initialization code here */ public void jspDestroyO { /* clean up code here */ %>

18 Scriplet Tag A scriptlet tag allows the JSP page developer to include arbitrary Java technology code in the _j spService method. The syntax for a declarations tag is: <% JavaCode %> Examples: <% int i = 0; %> <% if ( i > 10 ) { %> I is a big number. <% } else { %> I is a small number <% } %>

19 Scriplet Tag <TABLE BORDERS=‘1’ CELLSPACING='0' CELLPADDING='5'>
<TR><TH>number</TH> <TH>squared</TH> </TR> <% for ( int i=0; i<10; i++ ) { %> <TR><TD><%= i %></TD> <TD><%= (i * i) %></TD> <% } %> </TABLE>

20 <%= JavaExpression %>
Expression Tag An expression tag holds a Java language expression that is evaluated during an HTTP request. The result of the expression is included in the HTTP response stream. The syntax for a declaration tag is: <%= JavaExpression %> Examples: <B>Ten is <%= (2*5) %></B> Thank you, <I><%= name %></l>, for registering for the soccer league. The current day and time is: <%= new java.util.Date() %>

21 Implicit Variables The Web container gives the JSP technology developer access to the following variables in scriptlet and expression tags. These variables represent commonly used objects for servlets that JSP developers might need to use. Variable name Description request The HttpServletRequest object associated with the request. response The HttpServletResponse object associated with the response that is sent back to the browser. out The JspWriter object associated with the output stream of tl response. session The HttpSession object associated with the session for the given user of the request. This variable is only meaningful if the JSP page is participating in an HTTP session.

22 Implicit Variables Variable name Description application
The ServletContext object for the Web application. config The ServletConf ig object associated with the servlet for th JSP page. pageContext This object encapsulates the environment of a single request for this JSP page. page This variable is equivalent to the this variable in the Java programming language. exception The Throwable object that was thrown by some other JSP page. This variable is only available in a "JSP error page."

23 JSP Page Exception Handling
The JSP specification provides a different mechanism for handling exceptions. The page directive allows you to specify another JSP page to be activated whenever the JSP page throws an exception.

24 Declaring an Error Page
The errorPage attribute of the page directive is used to declare the error page to be used by the Web container when this JSP page throws an exception.

25 Developing an Error Page
The errorPage attribute of the page directive is used to declare that this JSP page has access to the exception implicit variables. This is used by the Web container when another JSP page throws an exception.


Download ppt "Developing JavaServer Pages"

Similar presentations


Ads by Google