1 / 23

Java Server Pages

Java Server Pages. Server - Apache Tomcat Server Server-side scripts - Java Server Pages. What are Java Server Pages?. Java Server Pages technology combines Java code and HTML tags in the same document to produce a JSP file. =. JSP. +. Java. <HTML>. Why use JSP Technology?.

yagil
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 Server - Apache Tomcat Server Server-side scripts - Java Server Pages

  2. What are Java Server Pages? • Java Server Pages technology combines Java code and HTML tags in the same document to produce a JSP file. = JSP + Java <HTML>

  3. Why use JSP Technology? • Convenient: • Integrates Java and HTML • Provides an extensive infrastructure for: • Tracking sessions. • Managing cookies. • Reading and sending HTML headers. • Parsing and decoding HTML form data. • Efficient: • Every request for a JSP is handled by a simple Java thread. Hence, the time to execute a JSP document is not dominated by starting a process.

  4. Why use JSP technology? • Portable • JSPs follow a standardized API. • The Java VM, which is used to execute a JSP file, is supported on many architectures and operating systems. • Inexpensive • There are a number of free or inexpensive Web Servers that are good for commercial-quality websites. • Apache Tomcat.

  5. Typical html Request/Response cycle 1. requests URL for html page 2. retrieves html page server client 3. sends html page to client 4. browser interprets html page & displays

  6. Request/Response for page - JavaScript commands 2. retrieves html page 1. requests URL for html page server with embedded JavaScript commands client (browser) 3. responds w. page to client 4. interprets page - executes Java script commands eg: check for bad or missing data on form CLIENT-SIDE SCRIPT

  7. Request/Response for file with Java Server Page parts 1 - requests JSP page 2 - retrieves page may have embedded JSP server script & Java-client-Script browser server 3 - responds with html+results in page SERVER-SIDE SCRIPT executes server-side script

  8. Request/Response for Java Server Page 1. sends URL for JSP page 2. retrieves page from storage compiles embedded JSP code * client server 3. sends html + results to client executes JSP code replaces code with exec results 4. browser displays page *compiled first time only - thereafter uses compiled copy experiment on effect of extensions like .jsp or .html

  9. 1 - requests JSP page browser server 3 - responds & html + results executes client-side script executes server-side script Scripts in Web Pages 2 - retrieves page using URL addr & server configuration

  10. Page Content Upon Arrival JSP versus JavaScript - client v. server execution 1. Start Apache Tomcat server listening on a port (often 8080) 2. Request a Java Server page from server - source file will have Results Example: Add2Int (shortly) 3. Request html page with JavaScript - source page will have theJavaScript Example: Countdown

  11. .jsp page retrieved is: Making a Request for a JSP File <html> <head> <title> current server time </title> </head> <font face = "Arial" size =4> The current Date and time on the web server are: <BR> <%= new java.util.Date() %> </font> </body> </html> • jsp instruction • - executed • on "server-side" • result replaces code • view source shows date that is printed, not scripting element Scripting Element Page on server has embedded jsp instruction

  12. 1. "source" as shown in browser <html> <head> <title> current server time </title> </head> <font face = "Arial" size =4> The current Date and time on the web server are: <BR> Wed Nov 27 20:27:02 EST 2002 </font> </body> </html> 2. Note how Date’s text replaces original JSP in page sent to browser

  13. Making a Request for Java Script HTML Page requested source page is same as displayed in browser <HTML> <HEAD> <TITLE>Client-side script </TITLE></HEAD> <BODY> THE TIME ON THE CLIENT IS: Current time is: <%= new java.util.Date( ) %> <script language="JavaScript" > document.write (new Date() ) </script> </BODY> </HTML> Why not executed on server ? sent to browser and executed on browser File type is html Example: DateTime.html

  14. from current directory of original request Auto-Refresh Example requested every 5 sec <HTML> <HEAD> <TITLE> server-side scripts </TITLE> <META HTTP-EQUIV = "REFRESH" CONTENT = "5, URL=CurrentDateTime.jsp"> </HEAD> <BODY> The time on the server is: <%= new java.util.Date( ) %> </BODY> </HTML> JSP result replaces this code & is sent to browser URL: http://acad.kutztown.edu:10001/JSP/CurrentDateTime.jsp

  15. Deciphering the URL URL: http://acad.kutztown.edu:10001/JSP/CurrentDateTime.jsp Requested File’s address on the server http://acad.kutztown.edu 10001 JSP helloWorld.js WHERE WHICH WHAT IP address of server remainder of file address path port that server listens on Path from webapps directory in Tomcat installation

  16. Structure of a JSP file. • Four basic tags: • Scriplet • Expression • Declaration • Definition

  17. JSP Comments • Regular Comment • <!-- comment --> • Hidden Comment • <%-- comment --%> Example.jsp: <html> <!-- Regular Comment --> <%-- Hidden Comment --%> </html> <html> <!-- Regular Comment --> </html>

  18. Declaration Element • Form: <%! Declaration %> • Used to declare class members: • variables • methods. • Declaratives only. • These declarations last as long as the class object is alive. • Example: <%! int x = 0; int square(int x){ return x * x; } %>

  19. Expression Elements • Form: <%= expression %> • An expression in this context is not a complete java statement; it is just a part of it. • Examples: <p> The square of <%= x%> is <%= square(x) %> as calculated by a JSP program.</p> <html> <body> <p><%= Integer.toString( 5 * 5 ) %></p> </body> </html> <html> <body> <p>25</p> </body> </html> Note: no semi-colon “;” following expression.

  20. Scriptlets • A scriptlet is a piece of Java code sandwiched between <% and %> • Embeds Java code in the JSP document that will be executed each time the JSP page is processed. • A scriptlet can make use of any java API as long as it is appropriate for the purpose. • Variables defined in a scriptlet are local. • Example: <html> <body> <p>Hello World!</p> <p>Hello World!</p> </body> </html> <html> <body> <% for (int i = 0; i < 2; i++) { %> <p>Hello World!</p> <% } %> </body> </html>

  21. Implicit Objects • A JSP container provides the tools necessary for a JSP document to interact with the environment surrounding it. • Three most commonly used implicit objects • Session • used to handle the current session • request • the incoming request • response • the outgoing response

  22. Processing HTML Forms • JSP eliminates manual parsing of data submitted from a form on a client browser. • Instead: • request.getParameter(“param-name”)

  23. JSP Directives • Form: <%@ directive %> • There are three directives defined by JSP; include, page, and taglib • Examples: <%@ page language=“java” import=“java.util.*”%> <%@ include file=“filename”%> • See StatesDB JSP Example

More Related