Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet.

Similar presentations


Presentation on theme: "Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet."— Presentation transcript:

1 Java Server Pages Jeffrey Jongko

2 Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet technology Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet technology Java Servlets was Sun’s first answer for developing web-based applications use the Java programming language. Java Servlets was Sun’s first answer for developing web-based applications use the Java programming language.

3 Introduction Java Servlet technology suffered similar maintenance problems that many traditional web-technologies like CGI had (presentation and logic are combined) Java Servlet technology suffered similar maintenance problems that many traditional web-technologies like CGI had (presentation and logic are combined) JSP is designed to facilitate development of dynamic web sites by more easily dissociating presentation from application logic JSP is designed to facilitate development of dynamic web sites by more easily dissociating presentation from application logic –allowing page developers to develop page presentation without interfering with application logic development

4 JSP Architecture The purpose of JSP is to provide a declarative, presentation-centric method of developing servlets. The purpose of JSP is to provide a declarative, presentation-centric method of developing servlets. JSP specification is defined as a standard extension on top the Servlet API. JSP specification is defined as a standard extension on top the Servlet API. Consequently, it should not be too surprisingly that under the covers, servlets and JSP pages have a lot in common. Consequently, it should not be too surprisingly that under the covers, servlets and JSP pages have a lot in common.

5 JSP Architecture Typically, JSP pages exist as simple text files and are subject to a translation phase and a request processing phase. Typically, JSP pages exist as simple text files and are subject to a translation phase and a request processing phase. The translation phase is carried out only once, unless the JSP page changes, in which case it is repeated. The translation phase is carried out only once, unless the JSP page changes, in which case it is repeated. –The JSP page is transformed into a servlet which subsequently processes all future requests to the page

6 JSP Architecture

7 Sample JSP file <% String hello = "Hello World"; %> The following JSP file is saved to a file HelloWorld.jsp The following JSP file is saved to a file HelloWorld.jsp It looks like regular HTML will special regions delimited with special markers like “ ” which represent JSP features It looks like regular HTML will special regions delimited with special markers like “ ” which represent JSP features

8 JSP Syntax core elements There are four basic core elements in JSP There are four basic core elements in JSP –comments –declarations –expressions –scriptlets

9 JSP Comments There are 2 types of JSP comments There are 2 types of JSP comments –HTML comment –Hidden comment HTML Comment Syntax HTML Comment Syntax ] --> ] --> Hidden Comment Syntax Hidden Comment Syntax A JSP HTML comment is a comment that is sent to the client (appears on the page data) A JSP HTML comment is a comment that is sent to the client (appears on the page data) JSP expressions (seen later) can be included inside an HTML comment JSP expressions (seen later) can be included inside an HTML comment

10 Sample JSP Comments <!-- Loaded on --> <% String hello = "Hello World"; %> </body></html> DATA RECEIVED BY CLIENT: Hello World

11 JSP Declarations JSP Declarations are used to define variables and methods that are visible to the whole page JSP Declarations are used to define variables and methods that are visible to the whole page –variables are translated into an instance variable in the compiled servlet JSP Declaration Syntax JSP Declaration Syntax Variables declared in this way are not thread- safe. The JSP page has to be declared as single-threaded if thread safety is needed for these variables. Variables declared in this way are not thread- safe. The JSP page has to be declared as single-threaded if thread safety is needed for these variables.

12 Sample JSP Declaration <%! int counter = 0; boolean isSameString(String a, String b) { return a.equals(b); } %> bjlee and hjk is <%= (isSameString(“bjlee”,”hjk”)) ? ”” : ”not” %> same string OUTPUT ON CLIENT: bjlee and hjk is not same string

13 JSP Expressions Scripting language expression that is evaluated and converted into a String for insertion into the output page Scripting language expression that is evaluated and converted into a String for insertion into the output page JSP Expression Syntax JSP Expression Syntax

14 Sample JSP Expressions <!-- Loaded on --> <% String hello = "Hello World"; %> </body></html> DATA RECEIVED BY CLIENT: Hello World

15 JSP Scriptlets Scripting language code fragment that is run within the service() method of the compiled servlet Scripting language code fragment that is run within the service() method of the compiled servlet –variables declared within a scriptlet are local unlike those declared by JSP Declarations JSP Scriptlet Syntax JSP Scriptlet Syntax

16 Sample JSP Scriptlet <% String name = “Byung Joon Lee”; StringTokenizer st= new StringTokenizer(name, “ “); while ( st.hasMoreTokens() ) { %> <% } %> OUTPUT ON CLIENT: Byung Joon Lee

17 Directive Directive directive defines attributes that apply to a whole JSP page directive defines attributes that apply to a whole JSP page Example of some attributes: [ language=“java”] [ extends=“package.class”] [ import=“{package.class | package.*},...”] [ session=“true|false”] [ isThreadSafe=“true|false”] [ info=“text”] [ errorPage=“relativeURL”] [ isErrorPage=“true|false”]

18 Implicit Objects Data is passed to JSP pages from the outside via HTTP POST or GET Data is passed to JSP pages from the outside via HTTP POST or GET This data is accessed via an implicit object This data is accessed via an implicit object –the name of this object is called request of type –the name of this object is called request of type javax.servlet.ServletRequest – –implicit objects are accessed via the scriptlets Other implicit objects exist such as Other implicit objects exist such as –response of type –response of type javax.servlet.ServletResponse – –pageContext – –session – –application

19 Other JSP features Ability to access JavaBean components using JSP tags, e.g. Ability to access JavaBean components using JSP tags, e.g. – – This allows access to JavaBean objects without the use of scriptlets/Java code This allows access to JavaBean objects without the use of scriptlets/Java code

20 Other JSP features Ability to extend the usable JSP tags using a custom tag library Ability to extend the usable JSP tags using a custom tag library –this is used to reduce the number of scriptlets on the page by encapsulating their logic behind tags. Both these features reduce the need for people with actual Java language experience which is needed for coding scriptlets Both these features reduce the need for people with actual Java language experience which is needed for coding scriptlets –allows for the development of presentation (JSP page) and the actual business logic (JavaBeans) to developed separately

21 Sites Some sites that use Java Server Pages Some sites that use Java Server Pages –http://www.sun.com http://www.sun.com –http://www.friendster.com http://www.friendster.com

22 References http://java.sun.com/products/jsp/ http://java.sun.com/products/jsp/ http://java.sun.com/products/jsp/ http://www.swpark.pe.kr/lecture/jsp.pdf http://www.swpark.pe.kr/lecture/jsp.pdf http://www.swpark.pe.kr/lecture/jsp.pdf http://developer.java.sun.com/developer/ onlineTraining/JSPIntro/ http://developer.java.sun.com/developer/ onlineTraining/JSPIntro/ http://developer.java.sun.com/developer/ onlineTraining/JSPIntro/ http://developer.java.sun.com/developer/ onlineTraining/JSPIntro/ http://archive.coreservlets.com http://archive.coreservlets.com http://archive.coreservlets.com


Download ppt "Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet."

Similar presentations


Ads by Google