Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Similar presentations


Presentation on theme: "JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content."— Presentation transcript:

1 JSP BASICS AND ARCHITECTURE

2 Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

3 What are JSP? JSP is a specification and not a product but a specification. JSP More on line of the J2EE specification. Can utilize the Beans to separate the code out. Is compiled to Servlet the first time call is received for the page.

4 THE PROCESS JSP Servlet Client JSP JSP First Request Generates Response Text Uses

5 A SIMPLE JSP CODE %@page import=“java.util.Date”% The current time is

6 The Container According to the JSP specifications that the servlets implement the Servlets must extend from:  Specified by JSP author via. Extends command. Or  A container specific Implementation class that implements javax.servlet.jsp.JSPPage interface.

7 The JSP Life Cycle public void jspInit() used for initialization purposes defined in javax.servlet.jsp.JSPpage Interface. public void jspDestroy() used for performing cleanup operations defined in javax.servlet.jsp.JSPPage Interface. public void _jspService(HttpServletRequest request, HttpServletResponese response) throws ServletException,IOException. The implementation of this method is generated by the container and should never be provided by Page authors.

8 General Rules for JSP Pages JSP tags are case sensitive. Attribute values in tags always appear quoted,Single or double. Eg: body Or The Second one is valid in case when there is no body.

9 General Rules for JSP Pages The character \ can be used as an escape character eg: to use %. URL that does not start with / are interpreted relative to the current JSP. URL starting with a /, called a context relative path, is interpreted with reference to the web application to which the JSP page belongs. Any white spaces with in the body text of a document are not significant although they are preserved during translation into a servlet.

10 Types of JSP Tags Directives. Scripting Elements. Actions.

11 JSP Directives They serve as messages to the JSP container from the JSP. Used to set global values as  Class Declarations  Methods to be implemented.  Output Content Type. They don’t produce any output to the user. Affects the whole file but only that file.

12 Types of JSP Directives page Directive. include Directive. taglib Directive.

13 page Directive Used to define and manipulate a number of page dependent attributes that affect the whole JSP. A page can contain any number of page directives, in any order, anywhere in the JSP.They are assimilated during translation. There can be only one occurrence of attribute/value pair defined by the page directives.

14 Program utilizing Page Directives <%@ page language="Java" session="true" import="java.rmi.*,java.util.*" session="true" buffer="12kb" autoFlush="true" info="my page directive jsp" errorPage="error.jsp" isErrorPage="false" isThreadSafe="false" %>

15 Program utilizing Page Directives Page Directive test Page Page directive test page This is a JSP to test the page directive.

16 Error in the Code This code will throw an error at present because the language page directive has been kept there for future use if JSP containers support multiple languages. At present this is not supported therefore it will through could not locate style sheet for JAVA error.

17 include Directive It instructs the container to include the content of the resource in the current JSP by inserting it inline. The include action occurs at runtime. Eg: The only available attribute file specifies the file name to be included. The included file can be a static resource such as an HTML file or another JSP. The code is inserted inline.

18 A simple Include Directive code Include Directive test page 1 Include directive test page 1

19 The copyright HTML file © 2000 Wrox Press

20 Output Include directive test page 1 © 2000 Wrox Press

21 taglib Directive This directive allows the page to use tag extensions (custom tags). It names the tag library that contains the compiled Java code. Eg:

22 Scripting Elements These allow Java Code – variable or method declarations, scriptlets (arbitrary Java code) and expressions to be inserted into your JSP page.

23 Types of Scripting Elements They are of three types: Declarations Scriptlets Expressions

24 Declarations A declaration is a block of Java code that is used to define class wide variables and methods in the generated servlet. Declarations are initialized when the JSP page is initialized. Syntax :

25 Scriptlets A scriptlet is a block of code that is executed during the request processing time. All the scriptlets in the JSP are combined in the order they appear in the code. As expected all the code for the scriptlets is put into the service() method of the servlet. Syntax:

26 Expressions The expression is a shorthand notation for a scriptlet that sends the value of Java expression back to the client. The expression is evaluated at HTTP request processing time and the result is converted to sting and displayed. In case the result of the expression is an object the conversation is done by using the objects toString() method. Syntax:

27 Standard Actions Standard Actions are tags that affect the runtime behavior of the JSP and the response sent back to the user. They have to provided by the container irrespective of the usage. During compilation into the servlet, the container comes across the this tag and replaces it with Java code that corresponds to the required predefined task.

28 Types of the Standard Action

29 This tag is used to instantiate a Java bean, or locate bean instance and assign it to a variable name (or id). We can also specify the lifetime of an object by giving it a specific name.

30 It is used with the action to set the value of bean properties. The properties in a bean can be set either:  At request time from parameters in the request object.  At request time from an evaluated expression.  From a specified string. Syntax:

31 It is used to access the properties of a bean. It accesses a property, converts it into a String, and prints it into the output stream of the client. Syntax:

32 Example utilizing For this example we will first of all create an HTML file beans.html in which user can insert his name and choose his favorite language from the drop down menu. Another file beans.jsp will be used to set the bean properties as per the values entered by the user and then by utilizing the methods defined in the LanguageBean.class it retrieves the information and displays it to the user.

33 beans.html useBean action test page useBean action test page Please enter your user name : What is your favorite programming language?

34 beans.html continued.. Java c++ Perl

35 beans.jsp useBean action test result

36 beans.jsp continued… useBean action test result Hello,. Your favorite language is.

37 beans.jsp continued… My comments on your favorite language:

38 LanguageBean.class public class LanguageBean{ private String name; private String language; public LanguageBean() {} public void setName(String name){ this.name=name; }

39 LanguageBean.class continued.. public String getName(){ return name; } public void setLanguage(String language){ this.language=language; }

40 LanguageBean.class continued.. public String getLanguage(){ return language; } public String getLanguageComments(){ if(language.equals("Java")){ return "The king of OOP languages."; }

41 LanguageBean.class continued.. else if(language.equals("c++")){ return "Rather too complex for some folks' liking."; } else if (language.equals("perl")){ return "OK if you like incomprehensible code."; }else{ return "Sorry, i have never heard of" +language+"."; }

42 Output of the Code useBean action test result Hello, Navdeep Mahajan. Your favorite language is Java. My comments on your favorite language: The king of OOP languages.

43 It is used to provide other tags with additional information in form of name value pairs. Syntax:

44 This action allows the static or dynamic resource, specified by the URL to be included in the current JSP at request processing time. The included page has access to only JspWriter object. It cannot set headers and cookies. It include page cannot have jsp tags. If the page output is buffered then the buffer is flushed prior to the inclusion. It has a small penalty on the efficiency.

45 Syntax: ……

46 It allows the request to be forwaded to the another JSP, to a servlet, or to a static resource. Execution in the current JSP stops when it encounters the tag,the buffer is cleared, and the request is modified..

47 Syntax: …..

48 It is used in pages to generate client browser specific HTML tags like or that result in the download of the Java Plug-in Software, if required,followed by the execution of the applet or JavaBeans component that is specified in the tag.

49 supported tags It supports two additional support tags: ,to pass additional parameters to the applet or the java beans component. , to specify the content to be displayed in the client browser if the plugin cannot be started because the generated tags are not supported.

50 Syntax

51 Syntax continued.. ……… Alternate text to display


Download ppt "JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content."

Similar presentations


Ads by Google