jsp architecture n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
JSP Architecture PowerPoint Presentation
Download Presentation
JSP Architecture

Loading in 2 Seconds...

play fullscreen
1 / 27

JSP Architecture - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

JSP Architecture. JSP is a simple text file consisting of HTML or XML content along with JSP elements JSP packages define the interface for the compiled JSP page JSPPage HttpJspPage Three main methods jspInit() jspDestroy() _jspService(HttpServletRequest request,

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'JSP Architecture' - tiara


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
jsp architecture
JSP Architecture
  • JSP is a simple text file consisting of HTML or XML

content along with JSP elements

  • JSP packages define the interface for the compiled JSP page
    • JSPPage
    • HttpJspPage
  • Three main methods
    • jspInit()
    • jspDestroy()
    • _jspService(HttpServletRequest request,

HttpServletResponse response)

elements of a javaserver page
Elements of a JavaServer Page
  • JSP Elements
  • “Template Text” – HTML
  • When request is processed
    • template text & dynamic content merged
    • result sent as response to browser
  • JSP Elements
    • Directive
    • Action
    • Scripting
elements of a javaserver page1
Elements of a JavaServer Page
  • Directives
    • provide global information to the page
  • Action
    • perform action based on up-to-date information
  • Scripting
    • Declarative
      • for page-wide variable and method declaration
    • Scriptlets
      • the Java code embedded in the page
    • Expressions
      • Format the expression as a string for inclusion in the output of the page.
jsp syntax html comment
JSP Syntax HTML Comment
  • Comment can be viewed in the HTML source file

<!-- comment <% expression%> -->

  • Example:

<!-- this is just Html comment -->

<!-- This page was loaded on <%= (new java.util.Date

()).toLocaleString()%> -->

  • View source:

<!-- this is just Html comment -->

<!-- This page was loaded on January 1, 2002 -->

jsp syntax hidden comment
JSP Syntax Hidden Comment
  • Everything between the <%-- and --%> is ignored by the JSP container
  • Comment cannot be viewed in the HTML source

file

<% -- expression -- %>

  • Example:

<html>

<body>

<h2>A Test of Comments</h2>

<%--This comment will be invisible in page source --%>

</body>

</html>

jsp directives
JSP Directives
  • A JSP directive is a statement that gives the JSP engine information for the page.
  • Used to provide information about the general set-up of the page. e.g. inclusion of header files, name of page to report errors, whether session tracking is required etc
  • Syntax
    • <%@ directive {attribute= “value” } %>
  • For Example:
    • scripting language used
    • session tracking required
    • name of page for error reporting
types of directive elements
Types of Directive Elements
  • <%@ page ... %>
    • Define page-dependent attributes, such as scripting language, error page & buffering requires
  • <%@ include ... %>
    • Include a file during the translation phase.
  • <%@ taglib ... %>
    • Declares a tag library, containing custom actions, used in the page
  • Each directive has a set of associated attributes (similar to some tags in HTML)
  • Full list of attributes available at: http://java.sun.com/products/jsp/syntax/1.1/syntaxref11.html
jsp syntax include directive
JSP Syntax Include Directive
  • Includes a static file

<%@ include file=“relativeURL” %>

  • Example:

main.jsp: <html><body>

Current date and time is:

<%@include file=“date.jsp” %>

</body></html>

date.jsp: <%@page import =“java.util.*” %>

<% =(new java.util.Date()).toLocaleString() %>

Output : Current date and time is:

Mar 5, 2000 4:56:50

attribute for the page directive and possible values
Attribute for the page directive and possible values
  • language

-- The language attribute defines the scripting language to be used in the page. The value "java" is the only value currently defined and is the default.

  • extends

-- The extends attribute defines the (fully-qualified) class name of the superclass of the servlet class that is generated from this JSP page.

  • import

-- The import attribute defines the set of classes and packages that must be imported in the servlet class definition.

attribute for the page directive and possible values1
Attribute for the page directive and possible values
  • session

-- The session attribute defines whether the JSP page is participating in an HTTP session. The value is either true (the default) or false.

  • Buffer

-- The buffer attribute defines the size of the buffer used in the output stream (a JspWriter object). The value is either none or Nkb.

  • autoFlush

-- The autoFlush attribute defines whether the buffer output should be flushed automatically when the buffer is filled or whether an exception is thrown. The value is either true (automatically flush) or false (throw an exception). The default is true.

attribute for the page directive and possible values2
Attribute for the page directive and possible values
  • isThreadSafe

-- The isThreadSafe attribute defines whether the JSP servlet implements the SingleThreadModel interface. The value is either true or false. The default is true.

  • Info

-- The info attribute defines an informational string about the JSP page.

  • errorPage

-- The errorPage attribute indicates another JSP page that will handle all runtime exceptions thrown by this JSP page. The value is a URL that is either relative to the current Web hierarchy or relative to the context root. For example, errorPage="error.jsp" (this is relative to the current hierarchy) or

errorPage="/error/formErrors.jsp" (this is relative to the Web application's context root).

attribute for the page directive and possible values3
Attribute for the page directive and possible values
  • isErrorPage

-- The isErrorPage attribute defines that the JSP page has been designed to be the target of another JSP page's errorPage attribute. The value is either true or false (default). All JSP pages that "are an error page" automatically have access to the exception implicit variable.

jsp action elements
JSP action elements
  • Action elements are an important syntax element in JSP
  • They are represented by tags (as is HTML)
  • They assist JSP developers to develop in tags rather thanscriplet programming
  • Instead of <%, they just use the < character (like HTML)

For example:

  • <prefix:action_name> body
  • </prefix:action_name >
jsp action elements1
JSP action elements
  • Full syntax of JSP Action Elements is:

<prefix:action_name attr1 = “value” attr2 = “value2”>action_body</prefix:action_name>

If the element doesn’t have a body, can lose the end tag and use shorthand syntax of:

<prefix:action_name attr1 = “value” attr2 = “value2” />

jsp action elements2
JSP action elements
  • There are two types of JSP action elements:

1. JSP Pre – defined tags, also called Standarded Action Elements.

2. External tag library: Custom or JSTL(Java Standard tag library)

jsp standard action elements
JSP standard action elements
  • jsp:useBean
  • jsp:setProperty
  • jsp:getProperty
  • jsp:param
  • jsp:include
  • jsp:forward
  • jsp:plugin
  • jsp:params
  • jsp:fallback
jsp standard action elements1
JSP standard action elements
  • Standard Action Example: <JSP: include> tag
  • Example:
  • <HTML> <BODY>
  • Going to include hellouser.jsp...<BR>
  • <jsp:include page="hellouser.jsp"/>
  • </BODY>
  • </HTML>
  • Executes the included JSP page and adds its output into the this page
jsp standard action elements2
JSP standard action elements
  • What’s difference from using the “include” derective?
  • The include directive includes the contents of another file at compilation time. Good for including common static code e.g. header file, footer file. Good on performance  included only once.
  • But, what if including dynamic common code (e.g. a navigation bar where links are read from the dB?).. need to re-run the file each time a request is madeJSP: include
jsp standard action elements3
JSP standard action elements
  • Standard Action Example: <JSP: forward> tag
  • Example:
  • <HTML> <BODY>
  • Error occurred…please wait<BR>
  • <jsp:forward page=“errorhandle.jsp"/>
  • </BODY>
  • </HTML>
  • Stops processing of one page and starts processing the page specified by the page attribute
jsp declaration
JSP Declaration
  • The definition of class-level variables and methods
  • Syntax

<%! Declaration %>

  • Example

<%! String var1 = “hi”;

int count = 0;

private void incrementCount() {

count++;

}

%>

jsp scriptlets
JSP Scriptlets
  • Scriptlets are defined as any block of valid Java code that resides between <% and %> tags
  • This code will placed in the generated servlet _jspService() method.
  • Example

<%

String var1 = request.getParameter(“lname”);

out.println(var1);

%>

jsp expressions
JSP Expressions
  • A JSP expression is a nice tool for embedding values within your HTML code.
  • Syntax

<%= expression %>

  • Example

<% for (int i = 0; i < 10; i++) { %>

<BR>

Counter value is <%= i %>

<% } %>

scripting implicit jsp objects
Scripting: Implicit JSP Objects
  • Request

-- The HttpServletRequest object associated with the request

  • Response

-- The HttpServletResponse object associated with the response that is sent back to the browser

  • pageContext

-- This object encapsulates the environment of a single request for this JSP page

  • Session

-- The HttpSession object associated with the session for the given user of the request. This variable is only meaningful if the JSP page is participating in an HTTP session

scripting implicit jsp objects1
Scripting: Implicit JSP Objects
  • Application

-- The ServletContext object for the Web application

  • Out

-- The JspWriter object associated with the output stream of the

response

  • Config

-- The ServletConfig object associated with the servlet for this JSP page

  • Exception

-- The Throwable object that was thrown by some other JSP page. This variable is only available in a "JSP error page."

scripting implicit jsp objects2
Scripting: Implicit JSP Objects
  • Example

<%@ page errorPage="errorpage.jsp"%>

<HTML>

<HEAD>

<TITLE>Use Out</TITLE>

</HEAD>

<BODY>

<%

//print a simple message using the implicit out object

out.println( "<center><b>Hello!</b></center>" );

%>

</BODY>

</HTML>

use of implicit objects
Use of Implicit Objects…
  • The following information was part of this request

<li> Request method <%= request.getMethod() %>

<li> Request URI <%= request.getRequestURI() %>

<li> Request Protocal <%= request.getProtocol() %>

<li> Server Name <%= request.getServerName() %>

<li> Server Port <%= request.getServerPort() %>

<li> Remote Address <%= request.getRemoteAddr() %>

<li> Remote Host <%= request.getRemoteHost() %>

use of implicit objects1
Use of Implicit Objects…
  • Example

<%@ page errorPage="errorpage.jsp"%>

<HTML>

<HEAD>

<TITLE>Use Request</TITLE>

</HEAD>

<BODY>

<%

//print a simple message using the implicit out object

out.println( "<b>Hello!</b>" + request.getParameter("user") + "</b>" );

%>

</BODY>

</HTML>