JSP Java Server Pages JSP n JSP Java

  • Slides: 11
Download presentation
JSP Java Server Pages

JSP Java Server Pages

JSP n JSP (Java Server Pages) is an alternate way of creating servlets n

JSP n JSP (Java Server Pages) is an alternate way of creating servlets n n JSP is written as ordinary HTML, with a little Java mixed in The Java is enclosed in special tags, such as <%. . . %> The HTML is known as the template text JSP files must have the extension. jsp n n JSP is translated into a Java servlet, which is then compiled The browser or other client sees only the resultant HTML, as usual 2

JSP scripting elements n n There is more than one type of JSP “tag,

JSP scripting elements n n There is more than one type of JSP “tag, ” depending on what you want done with the Java <%= expression %> n n <% code %> n n n The expression is evaluated and the result is inserted into the HTML page The code is inserted into the servlet's service method This construction is called a scriptlet <%! declarations %> n The declarations are inserted into the servlet class, not into a method 3

Example JSP n n <HTML> <BODY> Hello! The time is now <%= new java.

Example JSP n n <HTML> <BODY> Hello! The time is now <%= new java. util. Date() %> </BODY> </HTML> Notes: n n The <%=. . . %> tag is used, because we are computing a value and inserting it into the HTML The fully qualified name (java. util. Date) is used, instead of the short name (Date), because we haven’t yet talked about how to do import declarations 4

Variables n n You can declare your own variables, as usual JSP provides several

Variables n n You can declare your own variables, as usual JSP provides several predefined variables n n n request : The Http. Servlet. Request parameter response : The Http. Servlet. Response parameter session : The Http. Session associated with the request, or null if there is none out : A Jsp. Writer (like a Print. Writer) used to send output to the client Example: n Your hostname: <%= request. get. Remote. Host() %> 5

Scriptlets n Scriptlets are enclosed in <%. . . %> tags n n Scriptlets

Scriptlets n Scriptlets are enclosed in <%. . . %> tags n n Scriptlets do not produce a value that is inserted directly into the HTML (as is done with <%=. . . %>) Scriptlets are Java code that may write into the HTML Example: <% String query. Data = request. get. Query. String(); out. println("Attached GET data: " + query. Data); %> Scriptlets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiled n Example: <% if (Math. random() < 0. 5) { %> Have a <B>nice</B> day! <% } else { %> Have a <B>lousy</B> day! <% } %> 6

Declarations n Use <%!. . . %> for declarations to be added to your

Declarations n Use <%!. . . %> for declarations to be added to your servlet class, not to any particular method n n n Caution: Servlets are multithreaded, so nonlocal variables must be handled with extreme care If declared with <%. . . %>, variables are local and OK Data can also safely be put in the request or session objects Example: <%! private int access. Count = 0; %> Accesses to page since server reboot: <%= ++access. Count %> You can use <%!. . . %> to declare methods as easily as to declare variables 7

Directives n n n Directives affect the servlet class itself A directive has the

Directives n n n Directives affect the servlet class itself A directive has the form: <%@ directive attribute="value" %> or <%@ directive attribute 1="value 1" attribute 2="value 2". . . attribute. N="value. N" %> The most useful directive is page, which lets you import packages n Example: <%@ page import="java. util. *" %> 8

The include directive n The include directive inserts another file into the file being

The include directive n The include directive inserts another file into the file being parsed n n Syntax: <%@ include file="URL " %> n n n The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives The URL is treated as relative to the JSP page If the URL begins with a slash, it is treated as relative to the home directory of the Web server The include directive is especially useful for inserting things like navigation bars 9

Actions n n Actions are XML-syntax tags used to control the servlet engine <jsp:

Actions n n Actions are XML-syntax tags used to control the servlet engine <jsp: include page="URL " flush="true" /> n n n Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) This is great for rapidly changing data <jsp: forward page="URL" /> <jsp: forward page="<%= Java. Expression %>" /> n Jump to the (static) URL or the (dynamically computed) Java. Expression resulting in a URL 10

JSP in XML n n n JSP can be embedded in XML as well

JSP in XML n n n JSP can be embedded in XML as well as in HTML Due to XML’s syntax rules, the tags must be different (but they do the same things) HTML: <%= expression %> XML: <jsp: expression>expression</jsp: expression> HTML: <% code %> XML: <jsp: scriptlet>code</jsp: scriptlet> HTML: <%! declarations %> XML: <jsp: declaration>declarations</jsp: declaration> HTML: <%@ include file=URL %> XML: <jsp: directive. include file="URL"/> 11