Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Server Pages. 2 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.

Similar presentations


Presentation on theme: "Java Server Pages. 2 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."— Presentation transcript:

1 Java Server Pages

2 2 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 The HTML is enclosed in out.println( ) statements JSP (Java Server Pages) is an alternate way of creating servlets 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 JSP is translated into a Java servlet, which is then compiled Servlets are run in the usual way The browser or other client sees only the resultant HTML, as usual

3 3 JSP scripting elements There is more than one type of JSP “tag,” depending on what you want done with the Java 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 The declarations are inserted into the servlet class, not into a method

4 4 Example JSP Hello! The time is now Notes: 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

5 5 Variables You can declare your own variables, as usual JSP provides several predefined variables request : The HttpServletRequest parameter response : The HttpServletResponse parameter session : The HttpSession associated with the request, or null if there is none out : A JspWriter (like a PrintWriter ) used to send output to the client Example: Your hostname:

6 6 Script lets Script lets are enclosed in tags 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: Script lets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiled Example: Have a nice day! Have a lousy day!

7 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

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

9 9 Declarations Use for declarations to be added to your servlet class, not to any particular method 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: Accesses to page since server reboot: You can use to declare methods as easily as to declare variables

10 10 Directives Directives affect the servlet class itself A directive has the form: or The most useful directive is page, which lets you import packages Example:

11 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: pageDirective.jsp Directive to include other JSPs: includeDirective1.jsp, includeDirective2.jsp

12 12 The include directive The include directive inserts another file into the file being parsed The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives Syntax: 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

13 13 Actions Actions are XML-syntax tags used to control the servlet engine Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) This is great for rapidly changing data " /> Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL

14 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:

15 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: Static content: provided by HTML or XML Dynamic content: generated by JSP tags and scriplets written in Java language to encapsulate the application logic.

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

17 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.

18 JSP Tags Declaration: variable declaration Directive: ex: import classes Scriplet: Java code <% if password(“xyz”) { %> Welcome Expression: regular expression using variables and constants Action: <jsp:usebean name =“cart” class=“com.sun.java.Scart”

19 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.

20 How to prepare and run the examples? Simple JSPs can be typed into.jsp type files using your favorite editor. Create a directory called JSPExamples in the public_html directory of J2EE. Store the example JSPs here. Start the J2EE server. Run the JSP from your browser using the command: http://localhost:8000/JSPExamples/xyz.jsp For complex examples with actions and beans you will have to create web component (WAR).


Download ppt "Java Server Pages. 2 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."

Similar presentations


Ads by Google