Java Server Pages Servlets The purpose of a

  • Slides: 21
Download presentation
Java Server Pages

Java Server Pages

Servlets The purpose of a servlet is to create a Web page in response

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

How jsp work?

How jsp work?

JSP-Life Cycle

JSP-Life Cycle

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

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

Example JSP <HTML> <BODY> Hello! The time is now <%= new java. util. Date()

Example JSP <HTML> <BODY> Hello! The time is now <%= new java. util. Date() %> </BODY> </HTML> Notes: n n 6 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

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

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

Script lets are enclosed in <%. . . %> tags n n n Script

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

Examples: Scripting Elements Declaration: <%! %> A declaration is a block of code in

Examples: Scripting Elements Declaration: <%! %> A declaration is a block of code in a JSP that is used to define class-wide variables and methods in the generated servlet. Declaring a piece of code: declaration. jsp

Examples: Scripting Elements (contd. ) Scriplets: <% %> A scriplet is a block of

Examples: Scripting Elements (contd. ) Scriplets: <% %> A scriplet is a block of Java code that is executed during the request-processing time. Expressions: sends a value of a Java expression back to the client. <%= %>

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

Declarations 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 11

Directives affect the servlet class itself A directive has the form: <%@ directive attribute="value"

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 12 Example: <%@ page import="java. util. *" %>

Examples: Directives <%@ %> A directive configures the code generation that container will perform

Examples: Directives <%@ %> A directive configures the code generation that container will perform in creating a servlet. Simple JSP showing access to a Java API class Date: simple. jsp Using Page directives to define various page attributes: page. Directive. jsp Directive to include other JSPs: include. Directive 1. jsp, include. Directive 2. jsp

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

The include directive inserts another file into the file being parsed n The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives Syntax: <%@ include file="URL " %> n The URL is treated as relative to the JSP page n 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 14

Actions are XML-syntax tags used to control the servlet engine <jsp: include page="URL "

Actions are XML-syntax tags used to control the servlet engine <jsp: include page="URL " flush="true" /> n Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) n 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 15

Examples: Standard Actions Standard actions are well known tags that affect the run time

Examples: Standard Actions Standard actions are well known tags that affect the run time behavior of the JSP and the response sent back to the client. Some commonly used tag actions types are: <jsp: use. Bean> <jsp: set. Property> <jsp: get. Property> <jsp: param> <jsp: include> <jsp: forward> <jsp: plugin>

Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages

Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content. JSP technology is an extension/wrapper over the Java servlet technology. JSP are text based documents. We will focus only on JSP since it subsumes the servlet technology. Two major components of JSP: n Static content: provided by HTML or XML n Dynamic content: generated by JSP tags and scriplets written in Java language to encapsulate the application logic.

JSP compilation into Servlets JSP Initial request Web Browser Web Server Web Container translation

JSP compilation into Servlets JSP Initial request Web Browser Web Server Web Container translation Java Subseq request Servlets

More on JSP syntax and contents HTML code for user interface lay out JSP

More on JSP syntax and contents HTML code for user interface lay out JSP tags: declarations, actions, directives, expressions, scriplets JSP implicit objects: a request object, response object, session object, config object Javabeans: for logic that can be taken care of at the JSP level.

JSP Tags Declaration: variable declaration <%! int age = 56 %> Directive: ex: import

JSP Tags Declaration: variable declaration <%! int age = 56 %> Directive: ex: import classes <%@ page import = “java. util. *” %> Scriplet: Java code <% if password(“xyz”) { %> <H 1> Welcome <H 1> Expression: regular expression using variables and constants n <%= param[3]+4 %> Action: <jsp: usebean name =“cart” class=“com. sun. java. Scart”

Java Server Pages by Examples JSPs combine static markup (HTML, XML) with special dynamic

Java Server Pages by Examples JSPs combine static markup (HTML, XML) with special dynamic scripting tags. Each JSP is translated into a servlet the first time it is invoked. Then on, the requests are serviced by the servlets.