1 / 22

Java Server Pages

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 technology Java Servlets was Sun’s first answer for developing web-based applications use the Java programming language. Introduction.

clint
Download Presentation

Java Server Pages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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) • 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. • 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.

  5. JSP Architecture • 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 JSP page is transformed into a servlet which subsequently processes all future requests to the page

  6. JSP Architecture

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

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

  9. JSP Comments • There are 2 types of JSP comments • HTML comment • Hidden comment • HTML Comment Syntax <!-- comment [ <%= expression %>] --> • Hidden Comment Syntax <%-- comment --%> • 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

  10. <html> <body> <%@ page import="java.util.*" %> <%@ page import="java.text.*" %> <!-- Loaded on <%= DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREAN).format(new Date()) %> --> <% String hello = "Hello World"; %> <h1><%= hello %></h1> <%-- This will not appear --%> </body> </html> DATA RECEIVED BY CLIENT: <html> <body> <!-- Loaded on 2000-02-16 --> <h1>Hello World</h1> </body> </html> Sample JSP Comments

  11. JSP Declarations • 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 <%! variable / method declaration %> • 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. <%! int counter = 0; boolean isSameString(String a, String b) { return a.equals(b); } %> <html> <body> bjlee and hjk is <%= (isSameString(“bjlee”,”hjk”)) ? ”” : ”not” %> same string </body> </html> OUTPUT ON CLIENT: <html> <body> bjlee and hjk is not same string </body> </html> Sample JSP Declaration

  13. JSP Expressions • Scripting language expression that is evaluated and converted into a String for insertion into the output page • JSP Expression Syntax <%= variable / method call %>

  14. <html> <body> <%@ page import="java.util.*" %> <%@ page import="java.text.*" %> <!-- Loaded on <%= DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREAN).format(new Date()) %> --> <% String hello = "Hello World"; %> <h1><%= hello %></h1> <%-- This will not appear --%> </body> </html> DATA RECEIVED BY CLIENT: <html> <body> <!-- Loaded on 2000-02-16 --> <h1>Hello World</h1> </body> </html> Sample JSP Expressions

  15. JSP Scriptlets • 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 <% scripting language code %>

  16. <%@ page import=“java.util.*”%> <html><body bgcolor=“white”> <% String name = “Byung Joon Lee”; StringTokenizer st= new StringTokenizer(name, “ “); while ( st.hasMoreTokens() ) { %> <%= st.nextToken() %> <BR> <% } %> </body></html> OUTPUT ON CLIENT: <html><bodybgcolor=“white”> Byung<BR> Joon<BR> Lee<BR> </body></html> Sample JSP Scriptlet

  17. <%@page %> Directive • <%@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 • This data is accessed via an implicit object • 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 • response of type javax.servlet.ServletResponse • pageContext • session • application

  19. Other JSP features • Ability to access JavaBean components using JSP tags, e.g. • <jsp:useBean> • <jsp:setProperty> • <jsp:getProperty> • 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 • 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 • 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 • http://www.sun.com • http://www.friendster.com

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

More Related