

Popular server side scripting language.
 Provides dynamic web contents.
 Make use of script lets and XML based tags.
JSP Page
Preprocessed

.java file (Servlet Code)
compiled

.class file
servlet


Ease of deployment.
 Support multithreading.
 Re-usable components.
 Support cross platform.


HTML
 Java scriptlets
 JSP directives
 JavaScript



Scripting- Insert java statements directly into the
program.
Directive- give direction to the JSP translator
 Two ty...


Java statements are enclosed in special tags
<% ……. %>
<% =……. %>
<% !……. %>

Eg:

<% java.util.Date ct=new java,util.D...
 A set of attributes which are applicable for the entire

jsp page.
 <%@ directive attr_name=value %>
 3 types of direc...
<html> <body> <P> The following is a JSP declaration<P>
<%! int x = 5;
private int aMethod(int y) {
return x * y;
}
%>
<P>...


Defines page-dependent attributes, such as scripting
language, error page, and buffering requirements



Syntax

<%@ p...
<%@ page
[ language=”Scripting Language” ]
[ extends=”ClassName” ]
[ import=”{package.class | package.*}, ...” ]
[ session...
Attribute

Description

buffer

Specifies a buffering model for the output stream.

autoFlush

Controls the behavior of th...


Includes a file during the translation phase



Syntax

<%@ import file="relative url" %>
or
<jsp:directive.include fi...


Declares a tag library, containing custom actions, used
in the page
 User defined tags to implement custom behaviour.
...


Core Tags
 Formatting tags
 SQL tags
 XML tags
 JSTL Functions
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
Tag

Description

<c:out >

Like <%= ... >, but for expressions.

<c:set >

Sets the result of an expression evaluation in...


The JSTL formatting tags are used to format and
display text, the date, the time, and numbers for
internationalized Web...
Tag

Description

<fmt:formatNumber>

To render numerical value with specific precision or format.

<fmt:parseNumber>

Par...




The JSTL SQL tag library provides tags for
interacting with relational databases (RDBMSs)
such as Oracle, mySQL, or ...
Element

Description

<jsp:useBean>

Makes a JavaBeans component available in a page.

<jsp:getProperty>

Gets a property ...
Tag

Description

<sql:setDataSource>

Creates a simple DataSource suitable only for prototyping

<sql:query>

Executes th...


Are object which can use in JSP pages without
declaring them explicitly.

Object

Description

request

This is the Htt...
Subclass - javax.servlet.ServletRequest
Methods are
REQUEST:

 getParameter()
 getParameterValues()
 getParameterNames(...
Subclass - javax.servlet.ServletResponse
Methods are:
 findAttribute
 getAttribute
 getAttributesScope
 getAttributesN...
Subclass - javax.servlet.http.HttpSession
Methods are:
 getId
 getValue
 getValueNames
 putValue
Subclass - javax.servlet.ServletContext
Scope of the object is within the application.
Methods are:
 getMimeType
 getRea...
Subclass - javax.servlet.jsp. JspWriter
 Clear
 clearBuffer
 Flush
 getBufferSize
 getRemaining
Subclass - javax.servlet.ServletConfig
Methods are:
 getInitParameter
 getInitParameterNames
Subclass - java.lang.Object
Methods are:
 Authors does not use this object.

Subclass - java.lang.Throwable
Methods are:
...
<@page isErrorPage=”true” %>
<HTML>
<HEAD>
<TITLE>Error</TITLE>
</HEAD>
</BODY>
The following error has occurred: <%= exce...
<%@ page language="java" contentType="text/html" %>
<%! int globalCounter = 0; %>
<html>
<head>
<title>A page with a count...
Can do by the following methods,
 JavaBean properties
 Request Information
 Looping over arrays
<%@ page language="java" contentType="text/html" %>
<html>
<body bgcolor="white">
<jsp:useBean id="clock" class="java.util...
<%@ page language="java" contentType="text/html" %>
<html>
<body bgcolor="white">
<% if (request.getParameter(“id”)!=1) { ...
<%@ page language="java" contentType="text/html" %><html>
<body bgcolor="white">
<form action="loop.jsp">
<input type="che...
<% out.println();%> or <%=variable to be printed%>
<%@ page import="com.ora.jsp.util.*" %>
...
<tr>
<td>Name:</td>
<td><in...
Use of JavaBean components in JSP are,
 User input validation
 Inserting files
 Forwarding user data into pages
 Handl...
JavaBean components in JSP are,
 <jsp:useBean>
 <jsp:getProperty>
 <jsp:setProperty>
 <jsp:include>
 <jsp:forward>
 ...
<jsp:useBean>
Syntax:
<jsp: useBean
id=”name” scope=” page| request| session| application”
class =”test” type=“” >
</jsp: ...
package chapter09.beans;
public class userInfo implements
java.io.Serializable {
private String last_name;
private String ...
<%@ page import=”chapter09.beans.userInfo” %>
<%@ page import=”java.util.*” %>
<jsp:useBean id=”user” class=”userInfo” sco...
By two different ways,
Passing control between pages
Passing data between pages
a. Passing control between pages
By using ...
Introduction to JSP
Introduction to JSP
Introduction to JSP
Introduction to JSP
Upcoming SlideShare
Loading in …5
×

Introduction to JSP

441 views

Published on

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
441
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
35
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Introduction to JSP

  1. 1.  Popular server side scripting language.  Provides dynamic web contents.  Make use of script lets and XML based tags.
  2. 2. JSP Page Preprocessed .java file (Servlet Code) compiled .class file servlet
  3. 3.  Ease of deployment.  Support multithreading.  Re-usable components.  Support cross platform.
  4. 4.  HTML  Java scriptlets  JSP directives  JavaScript
  5. 5.   Scripting- Insert java statements directly into the program. Directive- give direction to the JSP translator  Two types are available, Include, Page, Taglib  Actions – Used to convert JSP elements to java servlet codes. Tag Libraries provide additional action elements.
  6. 6.  Java statements are enclosed in special tags <% ……. %> <% =……. %> <% !……. %> Eg: <% java.util.Date ct=new java,util.Date() %> <%=%>
  7. 7.  A set of attributes which are applicable for the entire jsp page.  <%@ directive attr_name=value %>  3 types of directives are defined over jsp Page Directive Include Directive Tablib Directive
  8. 8. <html> <body> <P> The following is a JSP declaration<P> <%! int x = 5; private int aMethod(int y) { return x * y; } %> <P> The following is a JSP expression, notice the lack of semi-colon<P> <P> This is a basic jsp<P> <%= new java.util.Date() %> <P> The following is a JSP scriptlet<P> <UL> <% for (int i = 0; i < 3; i++) { %> <LI><%= aMethod(i) %> <% } %> </UL></body></html>
  9. 9.  Defines page-dependent attributes, such as scripting language, error page, and buffering requirements  Syntax <%@ page attribute="value"%> or <jsp:directive.page attribute="value" />// XML Markup
  10. 10. <%@ page [ language=”Scripting Language” ] [ extends=”ClassName” ] [ import=”{package.class | package.*}, ...” ] [ session=”true|false” ] [ buffer=”none|8kb|sizekb” ] [ autoFlush=”true|false” ] [ isThreadSafe=”true|false” ] [ info=”text” ] [ errorPage=”relativeURL” ] [ contentType=”mimeType [ ;charset=characterSet ]” | “text/html ; charset=ISO-8859-1” ] [ isErrorPage=”true|false” ] %>
  11. 11. Attribute Description 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 runtime exceptions. isErrorPage 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 info Defines a string that can be accessed with the servlet's getServletInfo() 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 page will be ignored.
  12. 12.  Includes a file during the translation phase  Syntax <%@ import file="relative url" %> or <jsp:directive.include file="relative url" /> // XML Markup
  13. 13.  Declares a tag library, containing custom actions, used in the page  User defined tags to implement custom behaviour.  Syntax <%@ taglib uri="uri" prefix="prefixOfTag" > or <jsp:directive.taglib uri="uri" prefix="prefixOfTag" /> // XML Markup
  14. 14.  Core Tags  Formatting tags  SQL tags  XML tags  JSTL Functions
  15. 15. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  16. 16. Tag Description <c:out > Like <%= ... >, but for expressions. <c:set > Sets the result of an expression evaluation in a 'scope' <c:remove > Removes a scoped variable (from a particular scope, if specified). <c:catch> Catches any Throwable that occurs in its body and optionally exposes it. <c:if> Simple conditional tag which evalutes its body if the supplied condition is true. <c:choose> Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> <c:when> Subtag of <choose> that includes its body if its condition evalutes to 'true'. <c:otherwise > Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'. <c:import> Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'. <c:forEach > The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality . <c:forTokens> Iterates over tokens, separated by the supplied delimeters. <c:param> Adds a parameter to a containing 'import' tag's URL. <c:redirect > Redirects to a new URL.
  17. 17.  The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Web sites.  Syntax <%@ taglib prefix="fmt“ uri= "http ://java. Sun .com / jsp/jstl/fmt" %>
  18. 18. Tag Description <fmt:formatNumber> To render numerical value with specific precision or format. <fmt:parseNumber> Parses the string representation of a number, currency, or percentage. <fmt:formatDate> Formats a date and/or time using the supplied styles and pattern <fmt:parseDate> Parses the string representation of a date and/or time <fmt:bundle> Loads a resource bundle to be used by its tag body. <fmt:setLocale> Stores the given locale in the locale configuration variable. <fmt:setBundle> Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable. <fmt:timeZone> Specifies the time zone for any time formatting or parsing actions nested in its body. <fmt:setTimeZone> Stores the given time zone in the time zone configuration variable <fmt:message> To display an internationalized message.
  19. 19.   The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server. Syntax <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
  20. 20. Element Description <jsp:useBean> Makes a JavaBeans component available in a page. <jsp:getProperty> Gets a property value from a JavaBeans component and adds it to the response. <jsp:s etProperty> Sets a JavaBeans property value. <jsp:include> Includes the response from a servlet or JSP page during the request processing phase. <jsp:forward> Forwards the processing of a request to a servlet or JSP page. <jsp:param> Adds a parameter value to a request handed off to another servlet or JSP page using <jsp:include>or <jsp:forward> <jsp:plugin> Generates HTML that contains the appropriate client browser-dependent elements (OBJECT or EMBED) needed to execute an Applet with the Java Plugin software.
  21. 21. Tag Description <sql:setDataSource> Creates a simple DataSource suitable only for prototyping <sql:query> Executes the SQL query defined in its body or through the sql attribute. <sql:update> Executes the SQL update defined in its body or through the sql attribute. <sql:param> Sets a parameter in an SQL statement to the specified value. <sql:dateParam> Sets a parameter in an SQL statement to the specified java.util.Date value. <sql:transaction > Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.
  22. 22.  Are object which can use in JSP pages without declaring them explicitly. Object Description request This is the HttpServletRequest object associated with the request. response This is the HttpServletResponse object associated with the response. out This is the PrintWriter object used to send output to the client. session This is the HttpSession object associated with the request. application This is the ServletContext object associated with application context. config This is the ServletConfig object associated with the page. pageContext This encapsulates use of server-specific features like higher performance JspWriters. page This is simply a synonym for this, and is used to call the methods defined by the translated servlet class. Exception The Exception object allows the exception data to be accessed by designated JSP.
  23. 23. Subclass - javax.servlet.ServletRequest Methods are REQUEST:  getParameter()  getParameterValues()  getParameterNames() RESPONSE: Subclass - javax.servlet.ServletResponse
  24. 24. Subclass - javax.servlet.ServletResponse Methods are:  findAttribute  getAttribute  getAttributesScope  getAttributesNamesInScope
  25. 25. Subclass - javax.servlet.http.HttpSession Methods are:  getId  getValue  getValueNames  putValue
  26. 26. Subclass - javax.servlet.ServletContext Scope of the object is within the application. Methods are:  getMimeType  getRealPath
  27. 27. Subclass - javax.servlet.jsp. JspWriter  Clear  clearBuffer  Flush  getBufferSize  getRemaining
  28. 28. Subclass - javax.servlet.ServletConfig Methods are:  getInitParameter  getInitParameterNames
  29. 29. Subclass - java.lang.Object Methods are:  Authors does not use this object. Subclass - java.lang.Throwable Methods are:  getMessage  getLocalizedMessage  printStackTrace
  30. 30. <@page isErrorPage=”true” %> <HTML> <HEAD> <TITLE>Error</TITLE> </HEAD> </BODY> The following error has occurred: <%= exception.toString() %> </BODY> </HTML> Or <%page errorPage=”/jspError.jsp” %>
  31. 31. <%@ page language="java" contentType="text/html" %> <%! int globalCounter = 0; %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor="white"> This page has been visited: <%= ++globalCounter %> times. <p> <% int localCounter = 0;%> This counter never increases its value: <%= ++localCounter %> </body> </html>
  32. 32. Can do by the following methods,  JavaBean properties  Request Information  Looping over arrays
  33. 33. <%@ page language="java" contentType="text/html" %> <html> <body bgcolor="white"> <jsp:useBean id="clock" class="java.util.Date" /> <% if (clock.getHours( ) < 12) { %>Good morning!<% } else if (clock.getHours( ) < 17) { %>Good day!<% } else { %>Good evening!<% } %> </body> </html>
  34. 34. <%@ page language="java" contentType="text/html" %> <html> <body bgcolor="white"> <% if (request.getParameter(“id”)!=1) { %>Not a valid member! <% } else if (request.getParameter(“id”)=2) { %>Welcome !<% } else { %>Welcome Guest!<% } %> </body> </html>
  35. 35. <%@ page language="java" contentType="text/html" %><html> <body bgcolor="white"> <form action="loop.jsp"> <input type="checkbox" name="fruits" value="Apple">Apple<br> <input type="checkbox" name="fruits" value="Banana">Banana<br> <input type="checkbox" name="fruits" value="Orange">Orange<br> <input type="submit" value="Enter"> </form> <% String[] picked = request.getParameterValues("fruits"); if (picked != null && picked.length != 0) { %> You picked the following fruits: <ul> <% for (int i = 0; i < picked.length; i++) { out.println(picked[i]); } %></ul><% } %></body></html>
  36. 36. <% out.println();%> or <%=variable to be printed%> <%@ page import="com.ora.jsp.util.*" %> ... <tr> <td>Name:</td> <td><input type="text" name="userName" value="<%= StringFormat.toHTMLString(userInfo.getUserName( )) %>" > </td> </tr>
  37. 37. Use of JavaBean components in JSP are,  User input validation  Inserting files  Forwarding user data into pages  Handling data base access
  38. 38. JavaBean components in JSP are,  <jsp:useBean>  <jsp:getProperty>  <jsp:setProperty>  <jsp:include>  <jsp:forward>  <jsp:plugin>
  39. 39. <jsp:useBean> Syntax: <jsp: useBean id=”name” scope=” page| request| session| application” class =”test” type=“” > </jsp: useBean>
  40. 40. package chapter09.beans; public class userInfo implements java.io.Serializable { private String last_name; private String first_name; private String phone; private String email; public userInfo() {} public String getLastName() {return last_name;} public void setLastName(String lastName) {last_name = lastName;} public String getFirstName() {return first_name;} public void setFirstName(String firstName) {first_name= firstName;} public String getPhone() {return phone;} public void setPhone(String p) {phone = p;} public String getEmail() {return email;} public void setEmail(String e) {email = e; } }
  41. 41. <%@ page import=”chapter09.beans.userInfo” %> <%@ page import=”java.util.*” %> <jsp:useBean id=”user” class=”userInfo” scope=”page” /> <HTML> <HEAD> </HEAD> <BODY> <B>Last Name:</BR><INPUT TYPE=”Text” NAME=”lname” VALUE=”<%= user.getLastName() %>” <B>First Name:</BR> INPUT TYPE=”Text” NAME=”fname” VALUE=”<%= user.getFirstName() %>” ”><B>Phone:</BR></TD> <TD><INPUT TYPE=”Text” NAME=”phone” VALUE=”<%= user.getPhone() %>” INPUT TYPE=”Text” NAME=”email” VALUE=”<%= user.getEmail() %>” </BODY></HTML>
  42. 42. By two different ways, Passing control between pages Passing data between pages a. Passing control between pages By using <jsp:forward> Attributes are, page Example : “test.jsp” name “name” value “give any value” <jsp:forward page="userinfoinput.jsp" > <jsp:param name="msg" value="Invalid email address" /> </jsp:forward>

×