Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Server Pages.

Similar presentations


Presentation on theme: "Java Server Pages."— Presentation transcript:

1 Java Server Pages

2 JSP Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic and platform-independent Web based applications. Support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags.

3 JSP

4 JSPs Vs Servlets JSP is nothing but a high-level abstraction of servlets. You can do almost anything that can be done with servlets using JSP--but more easily!

5 JSP page translation and processing phases
Translation phase Hello.jsp Read Request Client Server helloServlet.java Generate Response Execute Processing phase helloServlet.class

6 JSP Life Cycle

7 JSP Comments JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out" part of your JSP page. Following is the syntax of JSP comments: <%-- This is JSP comment --%>

8 JSP Elements Java Server Pages are HTML pages embedded with snippets of Java code. It is an inverse of a Java Servlet Elements are used in constructing JSPs Scripting Elements Implicit Objects Directives Prevention: locks at doors, window bars, walls round the property Detection: stolen items are missing, burglar alarms, closed circuit TV Reaction: call the police, replace stolen items, make an insurance claim … Prevention: encrypt your orders, rely on the merchant to perform checks on the caller, don’t use the Internet (?) … Detection: an unauthorized transaction appears on your credit card statement Reaction: complain, ask for a new card number, etc.

9 Scripting Elements :Types
There are three kinds of scripting elements Declarations Scriptlets Expressions Prevention: locks at doors, window bars, walls round the property Detection: stolen items are missing, burglar alarms, closed circuit TV Reaction: call the police, replace stolen items, make an insurance claim … Prevention: encrypt your orders, rely on the merchant to perform checks on the caller, don’t use the Internet (?) … Detection: an unauthorized transaction appears on your credit card statement Reaction: complain, ask for a new card number, etc.

10 Expressions Basics Used to write dynamic content back to the browser.
If the output of expression is Java primitive the value is printed back to the browser If the output is an object then the result of calling toString on the object is output to the browser Embedded in <%= and %> delimiters Example: <%=“Fred”+ “ “ + “Flintstone” %> prints “Fred Flintstone” to the browser <%=Math.sqrt(100)%> prints 10 to the browser Prevention: locks at doors, window bars, walls round the property Detection: stolen items are missing, burglar alarms, closed circuit TV Reaction: call the police, replace stolen items, make an insurance claim … Prevention: encrypt your orders, rely on the merchant to perform checks on the caller, don’t use the Internet (?) … Detection: an unauthorized transaction appears on your credit card statement Reaction: complain, ask for a new card number, etc.

11 Declarations Basics Declarations are used to define methods & instance variables Embedded in <%! and %> delimiters Example: <%! int data=50; %> <%= "Value of the variable is:"+data %> <%! int cube(int n){ return n*n*n; } %> <%= "Cube of 3 is:"+cube(3) %> Prevention: locks at doors, window bars, walls round the property Detection: stolen items are missing, burglar alarms, closed circuit TV Reaction: call the police, replace stolen items, make an insurance claim … Prevention: encrypt your orders, rely on the merchant to perform checks on the caller, don’t use the Internet (?) … Detection: an unauthorized transaction appears on your credit card statement Reaction: complain, ask for a new card number, etc.

12 Scriptlets Used to embed java code in JSP pages.
Code should comply with syntactical and semantic constuct of java Any code written inside the scriptlet tags go into the _jspService() method. Embedded in <% and %> delimiters Example: <% int x = 5; int y = 7; int z = x + y; %> System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); Hello! The time is now <%= date %> Prevention: locks at doors, window bars, walls round the property Detection: stolen items are missing, burglar alarms, closed circuit TV Reaction: call the police, replace stolen items, make an insurance claim … Prevention: encrypt your orders, rely on the merchant to perform checks on the caller, don’t use the Internet (?) … Detection: an unauthorized transaction appears on your credit card statement Reaction: complain, ask for a new card number, etc.

13 JSP Directives The page directive The include directive
A JSP directive affects the overall structure of the servlet class. It usually has the following form: directive attribute="value" %> The page directive  The include directive

14 JSP Directives Page Directive Example :
Page directive specifies the instructions which applies to the entire source file , to the JSP engine. Example : page language="java“ import="java.util.*> Attributes

15 Attribute Purpose buffer Specifies a buffering model for the output stream. autoFlush Controls the behavior of the servlet output buffer. contentType Defines the character encoding scheme. errorPage Defines the URL of another JSP that reports on Java unchecked runtime exceptions. isErrorPage Indicates if this JSP page is a URL specified by another JSP page's errorPage attribute. extends Specifies a superclass that the generated servlet must extend import Specifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes. info Defines a string that can be accessed with the servlet's getServletInfo() method. isThreadSafe Defines the threading model for the generated servlet. language Defines the programming language used in the JSP page. session Specifies whether or not the JSP page participates in HTTP sessions isELIgnored Specifies whether or not EL expression within the JSP page will be ignored. isScriptingEnabled Determines if scripting elements are allowed for use

16 JSP Directives include Directive:
The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. Example: include file="header.jsp" %> <p>Thanks for visiting my page.</p> include file="footer.jsp" %>

17 Java Implicit Objects Scope
Implicit objects provide access to server side objects e.g. request, response, session etc. There are four scopes of the objects Page: Objects can only be accessed in the page where they are referenced Request: Objects can be accessed within all pages that serve the current request. (Including the pages that are forwarded to and included in the original jsp page) Session: Objects can be accessed within the JSP pages for which the objects are defined Application: Objects can be accessed by all JSP pages in a given context Prevention: locks at doors, window bars, walls round the property Detection: stolen items are missing, burglar alarms, closed circuit TV Reaction: call the police, replace stolen items, make an insurance claim … Prevention: encrypt your orders, rely on the merchant to perform checks on the caller, don’t use the Internet (?) … Detection: an unauthorized transaction appears on your credit card statement Reaction: complain, ask for a new card number, etc.

18 Java Implicit Objects request: Reference to the current request
response: Response to the request session: session associated woth current request application: Servlet context to which a page belongs pageContext: Object to access request, response, session and application associated with a page config: Servlet configuration for the page out: Object that writes to the response output stream page: instance of the page implementation class (this) exception: Available with JSP pages which are error pages Prevention: locks at doors, window bars, walls round the property Detection: stolen items are missing, burglar alarms, closed circuit TV Reaction: call the police, replace stolen items, make an insurance claim … Prevention: encrypt your orders, rely on the merchant to perform checks on the caller, don’t use the Internet (?) … Detection: an unauthorized transaction appears on your credit card statement Reaction: complain, ask for a new card number, etc.

19 The out Object: Following are the important methods which we would use to write boolean char, int, double, object, String etc. Method Description out.print(dataType dt) Print a data type value out.println(dataType dt) Print a data type value then terminate the line with new line character. out.flush() Flush the stream.

20 Methods of Request object
getParameter(): You call request.getParameter() method to get the value of a form parameter. getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getParameterNames(): Call this method if you want a complete list of all parameters in the current request. getInputStream(): Call this method to read binary data stream coming from the client.

21 DEMO


Download ppt "Java Server Pages."

Similar presentations


Ads by Google