+ All Categories
Home > Documents > CS6320 – JSP

CS6320 – JSP

Date post: 08-Jan-2016
Category:
Upload: ginata
View: 36 times
Download: 1 times
Share this document with a friend
Description:
CS6320 – JSP. L. Grewe. Java Server Pages. Servlets require you to write out entire page delivered with print statements JSP embedded in static html content File extension .jsp Not compiled - PowerPoint PPT Presentation
Popular Tags:
17
1 CS6320 – JSP CS6320 – JSP L. Grewe L. Grewe
Transcript
Page 1: CS6320 – JSP

11

CS6320 – JSPCS6320 – JSP

L. GreweL. Grewe

Page 2: CS6320 – JSP

22

Java Server PagesJava Server Pages Servlets require you to write out entire page Servlets require you to write out entire page

delivered with print statementsdelivered with print statements JSP embedded in static html contentJSP embedded in static html content File extension .jspFile extension .jsp Not compiledNot compiled Deploy as part of webapp but, location of jsp files Deploy as part of webapp but, location of jsp files

variable (see deployment and your server variable (see deployment and your server information)information)

Language of tags, can use JavaLanguage of tags, can use Java JSP run as servlets when envokedJSP run as servlets when envoked Invokes special _jspService() method …do not Invokes special _jspService() method …do not

have the doGet (do*) methods.have the doGet (do*) methods.

Page 3: CS6320 – JSP

33

How it works.How it works.

JSP engine on server receives request for JSP engine on server receives request for a .jsp page it:a .jsp page it:

1.1. Reads in the page, and transforms the contents to a Reads in the page, and transforms the contents to a ServletServlet

2.2. Even the static HTML is converted to print Even the static HTML is converted to print statements, printing to the output stream statements, printing to the output stream associated with the JSP’s _jspService() method.associated with the JSP’s _jspService() method.

3.3. This translation is done the first time the page is This translation is done the first time the page is requested.requested.

4.4. Then the server runs the resulting created Servlet Then the server runs the resulting created Servlet and returns the results to the client.and returns the results to the client.

Page 4: CS6320 – JSP

44

Elements of a Java Server PageElements of a Java Server Page Expressions: Expressions: <%= %><%= %>

• expression is evaluated and inserted into Servlet’s output. expression is evaluated and inserted into Servlet’s output. Scriptlets: Scriptlets: <% %><% %>

• This code is directly inserted into the produced Servlet’s This code is directly inserted into the produced Servlet’s _jspService() method._jspService() method.

• This method is automatically called by the Servlet’s This method is automatically called by the Servlet’s service() method.service() method.

Comments: Comments: <%-- --%><%-- --%>• User readable comments, not parsed by the JSP CompilerUser readable comments, not parsed by the JSP Compiler• Difference between this an HTML comment is this is inserted into Difference between this an HTML comment is this is inserted into

the Servlet being produced by evaluating the .jsp file. the Servlet being produced by evaluating the .jsp file.

Page 5: CS6320 – JSP

55

Elements of a Java Server PageElements of a Java Server Page

Directives: Directives: <%@ %><%@ %>• • Provide global information to the pageProvide global information to the page

• • Import statementsImport statements

• • Scripting languageScripting language Declarations: Declarations: <%! %><%! %>

• For page-wide variable and method declarationFor page-wide variable and method declaration• The code is inserted into the body of the The code is inserted into the body of the

produced Servlet, outside of any existing produced Servlet, outside of any existing method (like _jspService()) method (like _jspService())

Page 6: CS6320 – JSP

66

2 forms of tags2 forms of tags

<%= your_expression %><%= your_expression %>

  

OR the XML compliant versionOR the XML compliant version

<jsp:expression><jsp:expression>

your_expressionyour_expression

</jsp:expression></jsp:expression>

Page 7: CS6320 – JSP

77

JSP Expression ExampleJSP Expression Example

<%@ page language="java" import="java.util.*" <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">Transitional//EN">

<html><html>

<head> <head>

<title>HelloWorld</title><title>HelloWorld</title>

</head></head>

<body><body>

<p> The time is now <p> The time is now <%= new Date() %>.<%= new Date() %>.

</body></body>

</html></html>

Page 8: CS6320 – JSP

88

JSP ScriptletsJSP Scriptlets

JSP scriptlets are defined as block of JSP scriptlets are defined as block of Java code embedded between a pair Java code embedded between a pair ofof

tags, tags, <% <% and and %%>.>. Example:Example:

<%<%java.util.Date d = new java.util.Date();java.util.Date d = new java.util.Date();out.println(d);out.println(d);%>%>

Page 9: CS6320 – JSP

99

JSP DirectivesJSP Directives General syntax:General syntax:

<%@ directive {attribute = "value"} %><%@ directive {attribute = "value"} %> Possible values for Possible values for directives directives are:are:

• Page - Page - Information for the pageInformation for the page Include - Include - Specifies the files whose Specifies the files whose

contents are to be included in the outputcontents are to be included in the output• e.g., e.g., <%@ include file="header.html" %><%@ include file="header.html" %>

TaglibTaglib• The URI for a library of custom tags that The URI for a library of custom tags that

may be used in the pagemay be used in the page

Page 10: CS6320 – JSP

1010

Directive ExamplesDirective Examples

<%@ page language="java“ import =“java.util.*” %><%@ page language="java“ import =“java.util.*” %>• Imports classesImports classes

<%@ include file="relative-URL" %><%@ include file="relative-URL" %> • Includes filesIncludes files

<jsp:forward page="next.jsp"><jsp:forward page="next.jsp">    Zero or more <jsp:param name="x" value="y"> tags     Zero or more <jsp:param name="x" value="y"> tags

</jsp:forward></jsp:forward>• Forwards with parameters to another resourceForwards with parameters to another resource

See JSP documentation for moreSee JSP documentation for more

Page 11: CS6320 – JSP

1111

Directive Examples (Contd.)Directive Examples (Contd.)

<%@ page session = "true | false" %><%@ page session = "true | false" %>• true true indicates that session data is available to the indicates that session data is available to the

pagepage• By default, this is set to trueBy default, this is set to true

<%@ page buffer = "none | 16kb | sizekb" %><%@ page buffer = "none | 16kb | sizekb" %>• Determines the size of the output stream bufferDetermines the size of the output stream buffer• Defaults to 8kbDefaults to 8kb• Use with Use with autoFlushautoFlush

<%@ page autoFlush = "true | false" %><%@ page autoFlush = "true | false" %>• When set to true, flushes the output buffer when When set to true, flushes the output buffer when

it is full, rather than raising an exceptionit is full, rather than raising an exception

Page 12: CS6320 – JSP

1212

JSP DeclarationsJSP Declarations

Class and instance variables (of the Class and instance variables (of the generated servlet class) may be generated servlet class) may be specified using the JSP Declaration tag:specified using the JSP Declaration tag:

<%! String name = “Web Applications";<%! String name = “Web Applications";int index = 10;int index = 10;

int count = 0; %>int count = 0; %> Methods may also be specified:Methods may also be specified:

<%!<%!private int getNextIndex()private int getNextIndex(){return index ++;} %>{return index ++;} %>

Page 13: CS6320 – JSP

1313

JSP PreDefined ObjectsJSP PreDefined Objects

When writing scriptlets and expressions, When writing scriptlets and expressions, the following objects (called the following objects (called implicit implicit objectobjects) are available by default:s) are available by default:

request request javax.servlet.http.HttpServletRequestjavax.servlet.http.HttpServletRequest

response response javax.servlet.http.HttpServletResponsejavax.servlet.http.HttpServletResponse

out out javax.servlet.jsp.JspWriterjavax.servlet.jsp.JspWriter session session javax.servlet.http.HttpSessionjavax.servlet.http.HttpSession application application javax.servlet.ServletContextjavax.servlet.ServletContext exception exception java.lang.Throwablejava.lang.Throwable

Page 14: CS6320 – JSP

1414

JSP Session ExampleJSP Session Example<html><head><title><html><head><title>Visitor Count -- JSP Session </title>Visitor Count -- JSP Session </title></head> <body bgcolor="#FFFFFF"></head> <body bgcolor="#FFFFFF"><font face="Helvetica"> <h2><font face="Helvetica"> <h2>Visitor Count </h2>Visitor Count </h2><p><p>This JSP page demonstrates session management by This JSP page demonstrates session management by

incrementing a counter each time a user accesses a incrementing a counter each time a user accesses a page.page.

<p><p><%!<%!private int totalHits = 0;private int totalHits = 0;%>%>

Page 15: CS6320 – JSP

1515

JSP Session ExampleJSP Session Example

<% session = request.getSession(true);<% session = request.getSession(true);

Integer ival = Integer ival = (Integer)session.getValue("jspsession.counter(Integer)session.getValue("jspsession.counter");");

if (ival == null)if (ival == null)

{ ival = new Integer(1);}{ ival = new Integer(1);}

elseelse

{ival = new Integer(ival.intValue() + 1);}{ival = new Integer(ival.intValue() + 1);}

session.putValue("jspsession.counter", ival);session.putValue("jspsession.counter", ival);

%>%>

Page 16: CS6320 – JSP

1616

JSP Session ExampleJSP Session Example

<center><center><font size=+2><font size=+2>You have hit this page You have hit this page <%= ival %><%= ival %>timetime<%= (ival.intValue() == 1) ? "" : "s" %><%= (ival.intValue() == 1) ? "" : "s" %>,,out of a total of out of a total of <%= ++totalHits %><%= ++totalHits %> page pagehithit<%= (totalHits == 1) ? "" : "s" %><%= (totalHits == 1) ? "" : "s" %>!!</font></font></center></center><p><p></font></font></body></body></html></html>

Page 17: CS6320 – JSP

1717

More…..More…..

SEE the course website, and the SEE the course website, and the current JSP api for more details.current JSP api for more details.


Recommended