Summary
JSP Element Syntax Interpretation Notes JSP Expression <% = expression%> Expression is evaluated and placed in output XML equivalent is.
import = "package.class" contentType = "MIME-Type" isThreadSafe = "true | false" session = "true | false" buffer = "sizekb | none" autoflush = "true | false" extends = "package.class" info = "message" errorPage = "url" isErrorPage = "true | false" language = "java" JSP include Directive <% @ include file = "url"%> A file on the local system to be included when the JSP page is translated into a servlet XML equivalent is. . Flush = "true" /> Includes a file at the time the page is requested If you want to include the file at the time the page is translated, use the page directive with the include attribute instead Warning:. On some servers, the INCLUDED FILE MUSP FILE, AS Determined By The Server (USEBEAN Action). The JSP: Usebean Action ID = "name" scope = "Page | Request | session | Application" class = "package.class" type = "package.class" beanname = "package.class" the JSP: setProperty action Name = "PropertyName" Value = "val" /> retrieve and output bean proties. The JSP: Forward Action Page = "relative URL" /> Forwards Request to Another Page. The JSP: Plugin Action Attribute = "value" *> ... jsp: Plugin> Generates Object or Embed Tags, As Appropriate To The Browser Type, Asking That An Applet Be Run Using The Java Plugin. 3. Template Text: Static HTMLIN MANY CASES, A Large Percent of Your JSP Page Just Consists of Static HTML, KNOWN AS template text. In all respects except one, this HTML looks just like normal HTML, follows all the same syntax rules, and is simply "passed through" to the client by the servlet created to handle the page. Not only does the HTML LOOK NORMAL, IT CAN BE Created by wherever Tools you already it... I buy, i buy, i buy, i buy, i buy, i buy, i buy, i buy, i buy, i buy, i buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy, I buy. The one minor exception to the "Template Text IS Passed Straight Through" Rule Is That, if You Want to Have "<%" in The Output, You NEED TO PUT "%" in The Template Text. 4. JSP Scripting ElementsJSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page There are three forms:. Expressions of the form <% = expression%> that are evaluated and inserted into the output, Scriptlets of the form <% code%> that are inserted into the servlet's service method, and Declarations of the form <%! code%> that are inserted into the body of the servlet class, outside of any existing methods. Each of these is described IN more detail below. 4.1 JSP Expressionsa JSP Expression IS Used to Insert Java Values Directly Into The Output. It has the Following Form: <% = Java expression%> The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time (when the page is requested), and thus has full access to information about the request. For example, the following shows The date / time That the the page was required: Current Time: <% = new java.util.date ()%> To simplify these expressions, there are a number of predefined variables that you can use These implicit objects are discussed in more detail later, but for the purpose of expressions, the most important ones are.: request, the HttpServletRequest; response, the HttpServletResponse; session, the HttpSession associated with the request (if any); and out, the PrintWriter (a buffered version of type JspWriter) used to send output to the client Here's an example:. Your hostname: <% = request.getremotehost ()%> Finally, Note That XML Authors Can Use An Alternative Syntax for JSP Expressions: Java Expression jsp: expression> Remember That XML Elements, Unlike Html Ones, Are Case Sensitive. So Be Sure Use LowerCase.4.2 JSP Scriptletsif You Want To Do Something More Complex Than Insert A Simple Expression, JSP Scriptlets let you insert Arbitrary Code InTo The Servlet Method That Will Be Built to Generate The page. Scriptlets Have The Following Form: <% Java code%> Scriptlets Have Access To The Same Automatically Defined Variables As Expressions. So, For Example, IF You Want Output To Appear in The Resultant Page, you ' Out variable. <% String querydata = request.getQueryString (); Out.println ("Attached Get Data: QueryData); %> Note That Code InsIde A Scriptlet Gets Inserted EXACTLY As Written, And Any Static HTML (Template Text) Before or After a Scriptlet Gets Converted To print statements. This means that scriptlets need not contain complete Java statements, and blocks left open can affect the static HTML outside of the scriptlets. For example, the following JSP fragment, containing mixed template text and scriptlets <% IF (Math.random () <0.5) {%> Have a nice b> day! <%} else {%> Have a lousy b> day! <%}%> Will Get Converted to Something Like: IF (Math.random () <0.5) { Out.println ("Have a nice b> day!"); } else { Out.println ("Have a lousy b> day!"); } If you want to use the characters %> "Inside A Scriptlet, Enter" % /> "INSTEAD. Finally, Note That The XML Equivalent of <% Code%> IS Code jsp: Scriptlet> 4.3 JSP Declarativesa JSP Declaration Lets You Define Methods or Fields That Get Insert Class (Outside of There W) The Request. IT HAS The Following Form: <%! Java code%> Since declarations do not generate any output, they are normally used in conjunction with JSP expressions or scriptlets. For example, here is a JSP fragment that prints out the number of times the current page has been requested since the server booted (or the servlet class WAS Changed and Reloaded: <%! private int accesscount = 0;%> Accesses to page Since Server reboot: <% = accesscount%> As with scriptlets, if you want to use the characters %> ", Enter" % /> "INSTEAD. Finally, Note That The XML Equivalent of <%! Code%> IS Code jsp: Declaration> 5. JSP DirectiveSa JSP Directure Affects The Overall Structure of The Servlet Class. It usually has the full Form: <% @ Directive Attribute = "Value"%> However, You Can Also Combine Multiple Attribute Settings for a Single Directive, As Follows: <% @ Directive Attribute1 = "Value1" Attribute2 = "Value2" ... Attributen = "Valuen"%> There Are Two Main Types of Directive: Page, Which Lets You Do Things Like Import Classes, Customize The Servlet Superclass, and The Like; Include, Which Lets you insert a file into the servlet class at the time the jsp file is translated into a servlet. The specification also exercions the taglib directive, which is not supported in JSP version 1.0, but is intended to let JSP authors define their own tags. It is expected that this will be the main new contribution of JSP 1.1.5.1 The JSP page DirectiveThe Page Directive Lets You Define One or more of the folload case-sensitive attributes: Import = "package.class" or import = "package.class1, ..., package.classn". this lets you specify what package for example: <% @ Page Import = "java.util. *" %> The import attribute is the only one that is allowed to appear multiple times contentType = "MIME-Type" or contentType =.. "MIME-Type; charset = Character-Set" This specifies the MIME type of the output The default is . text / html For example, the directive <% @ page contentType = "text / plain"%> has the same effect as the scriptlet <% response.setContentType ( "text / plain");%> isThreadSafe = "true | false ". A value of true (the default) indicates normal servlet processing, where multiple requests can be processed simultaneously with a single servlet instance, under the assumption that the author synchronized access to instance variables. A value of false indicates that the servlet should implement SingleThreadModel, with requests Either Delivered Serially or with Simultaneous Requests Being Given Separate Servlet Instances. Session = "TR ue |. false "A value of true (the default) indicates that the predefined variable session (of type HttpSession) should be bound to the existing session if one exists, otherwise a new session should be created and bound to it A value of. false indicates that no sessions will be used, and attempts to access the variable session will result in errors at the time the JSP page is translated into a servlet buffer =. "sizekb | none" This specifies the buffer size for the JspWriter out.. The default is server-specific, but must be at least 8kb. Autoflush = "True | FALSE" . A value of true, the default, indicates that the buffer should be flushed when it is full. A value of false, rarely used, indicates that an exception should be thrown when the buffer overflows. A value of false is illegal when also using buffer = "none". extends = "package.class". This indicates the superclass of servlet that will be generated. Use this with extreme caution, since the server may be using a custom superclass already. info = "message". This defines .. a string that can be retrieved via the getServletInfo method errorPage = "url" This specifies a JSP page that should process any Throwables thrown but not caught in the current page isErrorPage =. "true | false" This indicates whether or not the. current page can act as the error page for another JSP page. The default is false. language = "java". At some point, this is intended to specify the underlying language being used. for now, do not bother with this since java Is Both the default and the only legal choice. The Xml Syntax for Defin ING DIRECTIVES IS For Example, The XML Equivalent of <% @ page import = "java.util. *"%> IS 5.2 The JSP Include DirectiveTHIS Directive Lets You include Files at The Time The JSP Page IS Translated Into a servlet. The Directive Looks Like this: <% @ include file = "relative URL"%> The URL specified is normally interpreted relative to the JSP page that refers to it, but, as with relative URLs in general, you can tell the system to interpret the URL relative to the home directory of the Web server by starting the URL with a forward slash. The contents of the included file are parsed as regular JSP text, and thus can include static HTML, scripting elements, directives, and actions.For example, many sites include a small navigation bar on each page. Due to problems with HTML frames , this is usually implemented by way of a small table across the top of the page or down the left-hand side, with the HTML repeated for each page in the site. The include directive is a natural way of doing this, saving the developers From The Maintenance Nightmare of actually Copying The HTML INTO Each Separate File. Here's Some RepereSentative Code:
Href = "site-styles.css" TYPE = "text / css"> HEAD> <% @ include file = "/ navbar.html"%> Body> Html> Note That Since The include directive inserts the files at the time the page is translated, if the navigation bar changes, you need to re-translate all the JSP pages that refer to it. This is a good compromise in a situation like this, since the navigation bar probably . changes infrequently, and you want the inclusion process to be as efficient as possible If, however, the included files changed more often, you could use thejsp:. include action instead This includes the file at the time the JSP page is requested, and IS discussed in The Tutorial Section on JSP Actions. 6. Example Using Scripting Elements and Directiveshere Is A Simple Example Showing The Use of JSP Expressions, Scriptlets, Declarations, and Directives. You can Also Download the Source OR Try it on-line.
Content = "JSP, JavaServer Pages, Servlets">
Content = "a Quick Example of The Four Main JSP Tags.">
HREF = "my-style-sheet.css"
TYPE = "text / css">
HEAD>
VLINK = "# 551a8b" alink = "# ff0000">