Java Server Pages <ul><li>Java server pages is a technology for developing portable java web applications that runs on a j...
Java Server Pages <ul><li>JSP enables you to separate the dynamic content of a web page from its presentation. It caters t...
JSP Life Cycle <ul><li>JSP life cycle includes following steps/phases: </li></ul><ul><li>JSP page translation – generate s...
Deploying JSP <ul><li>User don’t need to register jsp pages in deployment descriptor file web.xml. Just place them in the ...
Deploying JSP <ul><li>Now you can access jsp page in following two ways </li></ul><ul><li>http://localhost:8080/myapp/inst...
 
JSP API <ul><li>Translated JSP Servlet implements javax.serlvet.jsp.HttpJspPage interface, which is a sub interface of jav...
Java Server Pages <ul><li>JSP Elements </li></ul><ul><li>Directives </li></ul><ul><li>Scripting Elements </li></ul><ul><li...
JSP Scripting Elements <ul><li>There are four types of scripting elements defined </li></ul><ul><li>Declaration </li></ul>...
Declaration <ul><li>Declares a variable or method valid in the scripting language used in the JSP page.  </li></ul><ul><li...
Declaration <ul><li>A declaration declares one or more variables or methods that you can use in Java  code later in the JS...
Expression <ul><li>Contains an expression valid in the scripting language used in the JSP page.  </li></ul><ul><li>JSP Syn...
Expression <ul><li><%= new java.util.Date()%> </li></ul><ul><li>When this is generated into servlet code, the resulting ja...
Script lets <ul><li>Contains a code fragment valid in the page scripting language.  </li></ul><ul><li>Scriptlets allows yo...
Scriptlets <ul><li>Examples </li></ul><ul><li><%   String name = null; </li></ul><ul><li>     if (request.getParameter(&qu...
Comments <ul><li>To denote any lines you want to be completely ignored by the JSP translation process. </li></ul><ul><li>E...
JSP Directives <ul><li>Directives are instructions to jsp compiler. Those intructions tell the compiler that some action n...
Page Directive <ul><li>Page Directive- Defines attributes that apply to an entire JSP page. </li></ul><ul><li>JSP Syntax <...
Page Directive <ul><li>Description </li></ul><ul><li>The page directive applies to an entire JSP page and any of its  stat...
Page Directive <ul><li>Attributes </li></ul><ul><li>language=&quot; java &quot;  </li></ul><ul><ul><li>The scripting langu...
Page Directive <ul><li>import=&quot;{ package.class  |  package.* }, ...&quot;  </li></ul><ul><ul><li>A comma-separated li...
Page Directive <ul><li>isThreadSafe=&quot; true |false&quot;  </li></ul><ul><ul><li>Whether thread safety is implemented i...
Page Directive <ul><li>isErrorPage=&quot;true| false &quot;  </li></ul><ul><ul><li>Whether the JSP page displays an error ...
Include Directive <ul><li>Includes a static file in a JSP page, parsing the file's JSP elements.  </li></ul><ul><li>JSP Sy...
Include Directive <ul><li>date.jsp </li></ul><ul><li><%@ page import=&quot;java.util.*&quot; %>  </li></ul><ul><li><%= (ne...
Include Directive <ul><li>Attributes </li></ul><ul><li>file=&quot; relativeURL &quot;  </li></ul><ul><ul><li>The pathname ...
taglib directive <ul><li>Defines a tag library and prefix for the custom tags used in the JSP page.  </li></ul><ul><li>JSP...
taglib Directive <ul><li>Attributes </li></ul><ul><li>uri =&quot; URI &quot;- </li></ul><ul><li>The Uniform Resource Ident...
JSP ACTIONS <ul><li>Servlet container provides many built in functionality to ease the development of the applications </l...
JSP ACTIONS <ul><li>jsp:getProperty  The  jsp:getPropertyB  is used to get specified property from the JavaBean object.   ...
Simple JSP Example <ul><li><%@page contentType=&quot;text/html&quot; import=&quot;java.util.*&quot; %>   </li></ul><ul><li...
<ul><li><html> <head> <title>Enter your name</title> </head> <body> <p>&nbsp;</p> <form method=&quot;POST&quot; action=&qu...
<ul><li><%@page contentType=&quot;text/html&quot; %>   </li></ul><ul><li><!-- </li></ul><ul><li>http://www.roseindia.net/j...
jsp:forward <ul><li>Forwards a request to a web resource.  </li></ul><ul><li>JSP Syntax </li></ul><ul><li><jsp:forward pag...
jsp:forward <ul><li>The <jsp:forward> element forwards the request object containing the client request information from o...
jsp:include <ul><li>Includes a static file or the result from another web component.  </li></ul><ul><li>JSP Syntax </li></...
jsp:include <ul><li>The <jsp:include> element allows you to include either a  static  or  dynamic   resource  in a JSP pag...
jsp:useBean <ul><li>Locates or instantiates a bean with a specific name and scope.  </li></ul><ul><li>JSP Syntax </li></ul...
jsp:useBean <ul><li>Examples </li></ul><ul><li><jsp:useBean id=&quot;cart&quot; scope=&quot;session&quot; class=&quot;sess...
jsp:useBean <ul><li>The <jsp:useBean> element locates or instantiates a JavaBeans component. <jsp:useBean> first attempts ...
jsp:useBean <ul><li>The body of a <jsp:useBean> element often contains a <jsp:setProperty> element that sets property valu...
jsp:useBean <ul><li>scope=&quot; page |request|session|application&quot;  </li></ul><ul><ul><li>The scope in which the bea...
jsp:useBean <ul><li>class=&quot; package.class &quot;  </li></ul><ul><ul><li>Instantiates a bean from a class, using the n...
jsp:useBean <ul><li>class=&quot; package.class &quot; type=&quot; package.class &quot;  </li></ul><ul><ul><li>Instantiates...
jsp:setProperty <ul><li>Sets a property value or values in a bean.  </li></ul><ul><li>JSP Syntax </li></ul><ul><li><jsp:se...
jsp:setProperty <ul><li>Description </li></ul><ul><li>The < jsp:setProperty > element sets the value of one or more proper...
jsp:setProperty <ul><li>Attributes and Usage </li></ul><ul><li>name=&quot; beanInstanceName &quot;  </li></ul><ul><ul><li>...
jsp:setProperty <ul><li>property=&quot; propertyName &quot; [ param=&quot; parameterName &quot; ]  </li></ul><ul><ul><li>S...
jsp:getProperty <ul><li>Inserts the value of a bean property into the result.  </li></ul><ul><li>JSP Syntax </li></ul><ul>...
jsp:getProperty <ul><li>The < jsp:getProperty > element gets a bean property value using the property's getter methods and...
jsp:useBean Example <ul><li>< html >< body > </li></ul><ul><li>< form  method=‘post’ action=‘usebeanexample.jsp’> </li></u...
jsp:useBean Example <ul><li>public   class  Student{ </li></ul><ul><li>String name,address,rollNo,dob; </li></ul><ul><li>p...
jsp:usebean Example <ul><li>Usebeanexample.jsp </li></ul><ul><li><%@page  language=‘java’ %> </li></ul><ul><li>< jsp:useBe...
JSP Implicit Objects <ul><li>Object Name Type </li></ul><ul><li>request HttpServletRequest </li></ul><ul><li>response Http...
JSP session object
Upcoming SlideShare
Loading in...5
×

JSP

5,966

Published on

Published in: Technology
2 Comments
4 Likes
Statistics
Notes
No Downloads
Views
Total Views
5,966
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
418
Comments
2
Likes
4
Embeds 0
No embeds

No notes for slide

JSP

  1. 1. Java Server Pages <ul><li>Java server pages is a technology for developing portable java web applications that runs on a java web server. </li></ul><ul><li>Main motivation behind developing JSP was to develop a technology so that a non-java programmer or a person with little knowledge of java could write web applications for java plaform. </li></ul><ul><li>Initially JSP was developed to replace servlet but now common practice is to use servlet and jsp together using MVC(model-view-controller) pattern. </li></ul><ul><li>JSP allows developer to write html (static content),jsp action tags and java (dynamic content) code in the same page. </li></ul><ul><li>JSPs are compiled into java servlets by a jsp compiler, thus internally jsp page is converted into servlet, it is must to understand java servlet technology throughly for becoming a good jsp programmer. </li></ul>
  2. 2. Java Server Pages <ul><li>JSP enables you to separate the dynamic content of a web page from its presentation. It caters to different types of developes. </li></ul><ul><li>Web designers, who are responsible for the graphical design of the page </li></ul><ul><li>Java developers, who write the code to generate dynamic content. </li></ul>
  3. 3. JSP Life Cycle <ul><li>JSP life cycle includes following steps/phases: </li></ul><ul><li>JSP page translation – generate servlet source code </li></ul><ul><li>The translation occurs only when jsp page has to serve its first request. Translation does not have to happen again unless the page is updated. </li></ul><ul><li>JSP page compilation – compile generated code into byte code </li></ul><ul><li>Load class </li></ul><ul><li>Create instance </li></ul><ul><li>Call the jspInit method </li></ul><ul><li>Call the _jspService method - for each request </li></ul><ul><li>Call the jspDestroy method </li></ul>
  4. 4. Deploying JSP <ul><li>User don’t need to register jsp pages in deployment descriptor file web.xml. Just place them in the accessible area of a web application directory structure and user can access them. </li></ul><ul><li>How ever you can register a jsp page same way as a servlet. You even use the <servlet> element with one vital difference – where the <servlet-class> would apper,you substitute <jsp-file> instead. </li></ul><ul><li><servlet> </li></ul><ul><li><servlet-name>JspName1<servlet-name> </li></ul><ul><li><jsp-file>/instanceCheck.jsp</jsp-file> </li></ul><ul><li></servlet> </li></ul><ul><li><servlet-mapping> </li></ul><ul><li><servlet-name>JspName1</servlet-name> </li></ul><ul><li><url-pattern>/jspName2</url-pattern> </li></ul><ul><li></servlet-mapping> </li></ul>
  5. 5. Deploying JSP <ul><li>Now you can access jsp page in following two ways </li></ul><ul><li>http://localhost:8080/myapp/instanceCheck.jsp </li></ul><ul><li>OR </li></ul><ul><li>http://localhost:8080/myapp/jspName2 </li></ul><ul><li>if you want to supress direct access to jsp , locate jsp page under </li></ul><ul><li>WEB-INF directory. </li></ul>
  6. 7. JSP API <ul><li>Translated JSP Servlet implements javax.serlvet.jsp.HttpJspPage interface, which is a sub interface of javax.servlet.jsp.JspPage. </li></ul><ul><li>The javax.servlet.jsp.JspPage interface contains two methods: </li></ul><ul><li>1. public void jspInit() - This method is invoked when the JSP is initialized and the page authors are free to provide initialization of the JSP by implementing this method in their JSPs. </li></ul><ul><li>2. public void jspDestroy() - This method is invoked when the JSP is about to be destroyed by the container. Similar to above, page authors can provide their own implementation. </li></ul><ul><li>The javax.servlet.jsp.HttpJspPage interface contains one method: </li></ul><ul><li>public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException </li></ul><ul><li>This method generated by the JSP container is invoked, every time a request comes to the JSP. The request is processed and the JSP generates appropriate response. This response is taken by the container and passed back to the client. </li></ul>
  7. 8. Java Server Pages <ul><li>JSP Elements </li></ul><ul><li>Directives </li></ul><ul><li>Scripting Elements </li></ul><ul><li>JSP Actions </li></ul><ul><li>Templates </li></ul>
  8. 9. JSP Scripting Elements <ul><li>There are four types of scripting elements defined </li></ul><ul><li>Declaration </li></ul><ul><li>Expression </li></ul><ul><li>Scriptlets </li></ul><ul><li>Comments </li></ul>
  9. 10. Declaration <ul><li>Declares a variable or method valid in the scripting language used in the JSP page. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><%! declaration; [ declaration; ]+ ... %> </li></ul><ul><li>XML Syntax </li></ul><ul><li><jsp:declaration>   </li></ul><ul><li>   code fragment [ declaration; ]+ ... </li></ul><ul><li></jsp:declaration> </li></ul><ul><li>Examples </li></ul><ul><li><%! int i = 0; %> </li></ul><ul><li><%! int a, b, c; %> </li></ul><ul><li><%! Circle a = new Circle(2.0); %> </li></ul>
  10. 11. Declaration <ul><li>A declaration declares one or more variables or methods that you can use in Java code later in the JSP page. You must declare the variable or method before you use it in the JSP page. </li></ul><ul><li>You can declare any number of variables or methods within one declaration element, as long as you end each declaration with a semicolon. The declaration must be valid in the Java programming language. </li></ul><ul><li>You can use declaration to write anything that would be placed in the generated servlet outside of the _jspService method. </li></ul><ul><li>You can place in your declaration any code that can appear in a servlet soure file; instance methods, static methods, inner classes . You can also use declarations to override some methods that appear further up in the jsp servlet hierarchy – namely jspInit and jspDestroy. </li></ul>
  11. 12. Expression <ul><li>Contains an expression valid in the scripting language used in the JSP page. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><%= expression %> </li></ul><ul><li>XML Syntax </li></ul><ul><li><jsp:expression>     expression </jsp:expression> </li></ul><ul><li>Description </li></ul><ul><li>An expression element contains a scripting language expression that is evaluated, converted to a String, and inserted into the response where the expression appears in the JSP page. Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP page. </li></ul><ul><li>Example </li></ul><ul><li>Welcome, <%=userName%> </li></ul><ul><li>Output: </li></ul><ul><li>Welcome, James </li></ul>
  12. 13. Expression <ul><li><%= new java.util.Date()%> </li></ul><ul><li>When this is generated into servlet code, the resulting java stmt will probably look like this: </li></ul><ul><li>out.print(new java.util.Date()); </li></ul><ul><li>Out is an instance of javax.servlet.http.jsp.JspWriter and it is associated with the response for the generated servlet. </li></ul><ul><li>Cotents of your jsp expression are used directly as the parameter to the print() method.so it must not end with semicolon. </li></ul>
  13. 14. Script lets <ul><li>Contains a code fragment valid in the page scripting language. </li></ul><ul><li>Scriptlets allows you to include a series of java statements inside the _jspService method that are executed on every request to the page.these java stmts are incorporated into _jspService method as it is.so you must terminate them with semicolon. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><% code fragment %> </li></ul><ul><li>OR </li></ul><ul><li><jsp:scriptlet>     code fragment </jsp:scriptlet> </li></ul>
  14. 15. Scriptlets <ul><li>Examples </li></ul><ul><li><%  String name = null; </li></ul><ul><li>     if (request.getParameter(&quot;name&quot;) == null) { %> </li></ul><ul><li> <%@ include file=&quot;error.html&quot; %> </li></ul><ul><li><%   } </li></ul><ul><li> else { </li></ul><ul><li>    userObject.setName(request.getParameter(&quot;name&quot;));     </li></ul><ul><li>} </li></ul><ul><li>%> </li></ul>
  15. 16. Comments <ul><li>To denote any lines you want to be completely ignored by the JSP translation process. </li></ul><ul><li>Example </li></ul><ul><li><%-- Author: James Gosling --%> </li></ul>
  16. 17. JSP Directives <ul><li>Directives are instructions to jsp compiler. Those intructions tell the compiler that some action needs to be taken. </li></ul><ul><li>General syntax for directives is: </li></ul><ul><li><%@ Directive-name attribute-value pairs %> </li></ul><ul><li>There are three main directives defined in jsp </li></ul><ul><li>Page </li></ul><ul><li>Include </li></ul><ul><li>Taglib </li></ul>
  17. 18. Page Directive <ul><li>Page Directive- Defines attributes that apply to an entire JSP page. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><%@ page  language=&quot; java &quot; import=&quot;{ package.class | package.* }, ...&quot;   , </li></ul><ul><li>   session=&quot; true |false&quot;  %> </li></ul><ul><li>OR </li></ul><ul><li><jsp:directive.page pageDirectiveAttrList /> </li></ul><ul><li>where pageDirectiveAttrList is the same as the list in the JSP syntax. </li></ul><ul><li>XML Syntax </li></ul><ul><li><jsp:directive.page pageDirectiveAttrList /> where pageDirectiveAttrList is the same as the list in the JSP syntax. </li></ul><ul><li>Examples </li></ul><ul><li><%@ page import=&quot;java.util.*, java.lang.*&quot; %> </li></ul><ul><li> <%@ page buffer=&quot;5kb&quot; autoFlush=&quot;false&quot; %> </li></ul><ul><li><jsp:directive.page errorPage=&quot;error.jsp&quot; /> </li></ul>
  18. 19. Page Directive <ul><li>Description </li></ul><ul><li>The page directive applies to an entire JSP page and any of its static include files , which together are called a translation unit . A static include file is a file whose content becomes part of the calling JSP page. The page directive does not apply to any dynamic resources; see < jsp:include > for more information. </li></ul><ul><li>You can use the page directive more than once in a translation unit, but you can only use each attribute, except import, once. Because the import attribute is similar to the import statement in the Java programming language, you can use a page directive with import more than once in a JSP page or translation unit. </li></ul><ul><li>No matter where you position the page directive in a JSP page or included files, it applies to the entire translation unit. However, it is often good programming style to place it at the top of the JSP page. </li></ul>
  19. 20. Page Directive <ul><li>Attributes </li></ul><ul><li>language=&quot; java &quot; </li></ul><ul><ul><li>The scripting language used in scriptlets, declarations, and expressions in the JSP page and any included files. In v2.0, the only allowed value is java. </li></ul></ul><ul><li>extends=&quot; package.class &quot; </li></ul><ul><ul><li>The fully qualified name of the superclass of the Java class this JSP page will be compiled to. Use this attribute cautiously, as it can limit the JSP container's ability to provide a specialized superclass that improves the quality of the compiled class. </li></ul></ul>
  20. 21. Page Directive <ul><li>import=&quot;{ package.class | package.* }, ...&quot; </li></ul><ul><ul><li>A comma-separated list of Java packages that the JSP page should import. The packages (and their classes) are available to scriptlets, expressions, and declarations within the JSP page. If you want to import more than one package, you can specify a comma-separated list after import or you can use import more than once in a JSP page. </li></ul></ul><ul><ul><li>The following packages are implicitly imported, so you don't need to specify them with the import attribute: </li></ul></ul><ul><ul><li>java.lang.* </li></ul></ul><ul><ul><li>javax.servlet.* </li></ul></ul><ul><ul><li>javax.servlet.jsp.* </li></ul></ul><ul><ul><li>javax.servlet.http.* </li></ul></ul><ul><ul><li>You must place the import attribute before the element that calls the imported class. </li></ul></ul>
  21. 22. Page Directive <ul><li>isThreadSafe=&quot; true |false&quot; </li></ul><ul><ul><li>Whether thread safety is implemented in the JSP page. The default value is true, which means that the JSP container can send multiple, concurrent client requests to the JSP page. You must write code in the JSP page to synchronize the multiple client threads. If you use false, the JSP container sends client requests one at a time to the JSP page. </li></ul></ul><ul><li>info=&quot; text &quot; </li></ul><ul><ul><li>A text string that is incorporated verbatim into the compiled JSP page. You can later retrieve the string with the Servlet.getServletInfo() method. </li></ul></ul><ul><li>errorPage=&quot; relativeURL &quot; </li></ul><ul><ul><li>A pathname to a JSP page that this JSP page sends exceptions to. If the pathname begins with a /, the path is relative to the JSP application's document root directory and is resolved by the web server. If not, the pathname is relative to the current JSP page. </li></ul></ul>
  22. 23. Page Directive <ul><li>isErrorPage=&quot;true| false &quot; </li></ul><ul><ul><li>Whether the JSP page displays an error page. If set to true, you can use the exception object in the JSP page. If set to false (the default value), you cannot use the exception object in the JSP page. </li></ul></ul><ul><li>contentType=&quot; mimeType [; charset= characterSet ]&quot; | </li></ul><ul><ul><li>    &quot; text/html;charset=ISO-8859-1 &quot; </li></ul></ul><ul><ul><li>The MIME type and character encoding the JSP page uses for the response. You can use any MIME type or character set that are valid for the JSP container. The default MIME type is text/html, and the default character set is ISO-8859-1. </li></ul></ul><ul><li>pageEncoding=&quot; characterSet | ISO-8859-1 &quot; </li></ul><ul><ul><li>The character encoding the JSP page uses for the response. The default character set is ISO-8859-1. </li></ul></ul>
  23. 24. Include Directive <ul><li>Includes a static file in a JSP page, parsing the file's JSP elements. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><%@ include file=&quot; relativeURL &quot; %> OR </li></ul><ul><li><jsp:directive.include file=&quot; relativeURL &quot; /> </li></ul><ul><li>XML Syntax </li></ul><ul><li><jsp:directive.include file=&quot; relativeURL &quot; /> </li></ul><ul><li>Examples </li></ul><ul><li>include.jsp: </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>An Include Test</title> </li></ul><ul><li></head> </li></ul><ul><li><body bgcolor=&quot;white&quot;> </li></ul><ul><li><font color=&quot;blue&quot;> </li></ul><ul><li>The current date and time are <%@ include file=&quot;date.jsp&quot; %> </li></ul><ul><li></font> </body> </html> </li></ul>
  24. 25. Include Directive <ul><li>date.jsp </li></ul><ul><li><%@ page import=&quot;java.util.*&quot; %> </li></ul><ul><li><%= (new java.util.Date() ).toLocaleString() %> </li></ul><ul><li>Displays in the page: The current date and time are Aug 30, 1999 2:38:40 </li></ul><ul><li>Description </li></ul><ul><li>An include directive inserts a file of text or code in a JSP page at translation time, when the JSP page is compiled. When you use the include directive, the include process is static . A static include means that the text of the included file is added to the JSP page. The included file can be a JSP page, HTML file, XML document, or text file. If the included file is a JSP page, its JSP elements are translated and included (along with any other text) in the JSP page. Once the included file is translated and included, the translation process resumes with the next line of the including JSP page. </li></ul>
  25. 26. Include Directive <ul><li>Attributes </li></ul><ul><li>file=&quot; relativeURL &quot; </li></ul><ul><ul><li>The pathname to the included file, which is always a relative URL. A relative URL is just the path segment of an URL, without a protocol, port, or domain name, like this: </li></ul></ul><ul><ul><li>&quot;error.jsp&quot; &quot;/templates/onlinestore.html&quot; &quot;/beans/calendar.jsp&quot; </li></ul></ul><ul><ul><li>If the relative URL starts with /, the path is relative to the JSP application's context, which is a javax.servlet.ServletContext object that is in turn stored in the application object. If the relative URL starts with a directory or file name, the path is relative to the JSP page. </li></ul></ul>
  26. 27. taglib directive <ul><li>Defines a tag library and prefix for the custom tags used in the JSP page. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><%@ taglib {uri=&quot; URI &quot; | tagdir=&quot;/WEB-INF/tags[/ subdir ]+&quot;} prefix=&quot; tagPrefix &quot; %> </li></ul><ul><li>Examples </li></ul><ul><li><%@ taglib uri=&quot;http://www.jspcentral.com/tags&quot; prefix=&quot;public&quot; %> </li></ul><ul><li><public:loop>    ... </public:loop> </li></ul><ul><li>Description </li></ul><ul><li>The taglib directive declares that the JSP page uses custom tags, names the tag library that defines them, and specifies their tag prefix. </li></ul><ul><li>You must use a taglib directive before you use the custom tag in a JSP page. You can use more than one taglib directive in a JSP page, but the prefix defined in each must be unique. </li></ul>
  27. 28. taglib Directive <ul><li>Attributes </li></ul><ul><li>uri =&quot; URI &quot;- </li></ul><ul><li>The Uniform Resource Identifier (URI) that uniquely locates the TLD that describes the set of custom tags associated with the named tag prefix. </li></ul><ul><li>Prefix =‘tagprefix’ </li></ul><ul><li>The prefix that precedes the custom tag name, for example, public in <public:loop>. Empty prefixes are illegal. </li></ul><ul><li>If you are developing or using custom tags, you cannot use the tag prefixes jsp, jspx, java, javax, servlet, sun, and sunw, as they are reserved by Sun Microsystems. </li></ul>
  28. 29. JSP ACTIONS <ul><li>Servlet container provides many built in functionality to ease the development of the applications </li></ul><ul><li>Programmers can use these functions in JSP applications. The JSP Actions tags enables the programmer to use these functions. The JSP Actions are XML tags that can be used in the JSP page. </li></ul><ul><li>Here is the list of JSP Actions: </li></ul><ul><li>jsp:include  The jsp:include action work as a subroutine, the Java servlet temporarily passes the request and response to the specified JSP/Servlet. Control is then returned back to the current JSP page.   </li></ul><ul><li>jsp:param   The jsp:param action is used to add the specific parameter to current request. The jsp:param tag can be used inside a jsp:include, jsp:forward or jsp:params block.  </li></ul><ul><li>jsp:forward   The jsp:forward tag is used to hand off the request and response to another JSP or servlet. In this case the request never return to the calling JSP page.    </li></ul>
  29. 30. JSP ACTIONS <ul><li>jsp:getProperty  The jsp:getPropertyB is used to get specified property from the JavaBean object.    </li></ul><ul><li>jsp:setProperty   The jsp:setProperty tag is used to set a property in the JavaBean object. </li></ul><ul><li>   </li></ul><ul><li>jsp:useBean   The jsp:useBean   tag is used to instantiate an object of Java Bean or it can re-use existing java bean object. </li></ul>
  30. 31. Simple JSP Example <ul><li><%@page contentType=&quot;text/html&quot; import=&quot;java.util.*&quot; %> </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><p>&nbsp;</p><div align=&quot;center&quot;><center> </li></ul><ul><li><table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;460&quot; bgcolor=&quot;#EEFFCA&quot;> </li></ul><ul><li><tr> </li></ul><ul><li><td width=&quot;100%&quot;><font size=&quot;6&quot; color =&quot;#008000&quot;>&nbsp;Date Example</font></td> </li></ul><ul><li></tr> </li></ul><ul><li><tr><td width=&quot;100%&quot;><b>&nbsp;Current Date and time is:&nbsp; </li></ul><ul><li><font color=&quot;#FF0000&quot;> <%= new java.util.Date() %> </li></ul><ul><li></font></b></td></tr></table></center></div></body></html> </li></ul>
  31. 32. <ul><li><html> <head> <title>Enter your name</title> </head> <body> <p>&nbsp;</p> <form method=&quot;POST&quot; action=&quot;showname.jsp&quot;> <p><font color=&quot;#800000&quot; size=&quot;5&quot;>Enter your name:</font><input type =&quot;text&quot; name=&quot;username&quot; size=&quot;20&quot;></p> <p><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;></p> </form> </body> </html> </li></ul>
  32. 33. <ul><li><%@page contentType=&quot;text/html&quot; %> </li></ul><ul><li><!-- </li></ul><ul><li>http://www.roseindia.net/jsp </li></ul><ul><li>--> </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><p><font size=&quot;6&quot;>Welcome :&nbsp; <%=request.getParameter(&quot;username&quot;)%></font></p> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  33. 34. jsp:forward <ul><li>Forwards a request to a web resource. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><jsp:forward page=&quot;{ relativeURL | <%= expression %>}&quot; /> </li></ul><ul><li>or </li></ul><ul><li><jsp:forward page=&quot;{ relativeURL | <%= expression %>}&quot; >     </li></ul><ul><li><jsp:param name=&quot; parameterName &quot;       value=&quot;{ parameterValue | <%= expression %>}&quot; /> </li></ul><ul><li></jsp:forward> </li></ul><ul><li>Examples </li></ul><ul><li><jsp:forward page=&quot;/servlet/login&quot; /> </li></ul><ul><li><jsp:forward page=&quot;/servlet/login&quot;>    </li></ul><ul><li>  <jsp:param name=&quot;username&quot; value=&quot;jsmith&quot; /> </li></ul><ul><li></jsp:forward> </li></ul>
  34. 35. jsp:forward <ul><li>The <jsp:forward> element forwards the request object containing the client request information from one JSP page to another resource. The target resource can be an HTML file, another JSP page, or a servlet, as long as it is in the same application context as the forwarding JSP page. The lines in the source JSP page after the <jsp:forward> element are not processed. </li></ul><ul><li>You can pass parameter names and values to the target resource by using a <jsp:param> clause. An example of this would be passing the parameter name username (with name=&quot;username&quot;) and the value scott (with value=&quot;scott&quot;) to a servlet as part of the request. If you use <jsp:param>, the target resource should be a dynamic resource that can handle the parameters. </li></ul><ul><li>Attributes </li></ul><ul><li>page=&quot;{ relativeURL | <%= expression %>}&quot; </li></ul><ul><ul><li>A String or an expression representing the relative URL of the component to which you are forwarding the request. The component can be another JSP page, a servlet, or any other object that can respond to a request. </li></ul></ul><ul><ul><li>The relative URL looks like a path--it cannot contain a protocol name, port number, or domain name. The URL can be absolute or relative to the current JSP page. If it is absolute (beginning with a /), the path is resolved by your web or application server. </li></ul></ul>
  35. 36. jsp:include <ul><li>Includes a static file or the result from another web component. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><jsp:include page=&quot;{ relativeURL | <%= expression %>}&quot;    flush=&quot;true| false&quot; /> </li></ul><ul><li>or </li></ul><ul><li><jsp:include page=&quot;{ relativeURL | <%= expression %>}&quot;    flush=&quot;true| false&quot; >    </li></ul><ul><li>  <jsp:param name=&quot; parameterName &quot;       value=&quot;{ parameterValue | <%= expression %>}&quot; /> </li></ul><ul><li></jsp:include> </li></ul>
  36. 37. jsp:include <ul><li>The <jsp:include> element allows you to include either a static or dynamic resource in a JSP page. The results of including static and dynamic resources are quite different. If the resource is static, its content is included in the calling JSP page. If the resource is dynamic, it acts on a request and sends back a result that is included in the JSP page. When the include action is finished, the JSP container continues processing the remainder of the JSP page. </li></ul><ul><li>You cannot always determine from a pathname if a resource is static or dynamic. For example, http://server:8080/index.html might map to a servlet through a server alias. The <jsp:include> element handles both types of resources, so it is convenient to use when you don't know whether the resource is static or dynamic. </li></ul><ul><li>If the included resource is dynamic, you can use a <jsp:param> clause to pass the name and value of a parameter to the resource. As an example, you could pass the string username and a user's name to a login form that is coded in a JSP page. </li></ul>
  37. 38. jsp:useBean <ul><li>Locates or instantiates a bean with a specific name and scope. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><jsp:useBean id=&quot; beanInstanceName &quot;    scope=&quot; page |request|session|application&quot; </li></ul><ul><li>{ </li></ul><ul><li>    class=&quot; package.class &quot; [ type=&quot; package.class “ ]| </li></ul><ul><li>     beanName=&quot;{ package.class | <%= expression %>}“ </li></ul><ul><li>        type=&quot; package.class &quot; |     </li></ul><ul><li> } </li></ul><ul><li>{ /> | > other elements </jsp:useBean> } </li></ul>
  38. 39. jsp:useBean <ul><li>Examples </li></ul><ul><li><jsp:useBean id=&quot;cart&quot; scope=&quot;session&quot; class=&quot;session.Carts&quot; /> </li></ul><ul><li><jsp:setProperty name=&quot;cart&quot; property=&quot;*&quot; /> </li></ul><ul><li><jsp:useBean id=&quot;checking&quot; scope=&quot;session&quot; class=&quot;bank.Checking&quot; > </li></ul><ul><li>    <jsp:setProperty name=&quot;checking&quot; property=&quot;balance&quot; value=&quot;0.0&quot; /> </li></ul><ul><li></jsp:useBean> </li></ul>
  39. 40. jsp:useBean <ul><li>The <jsp:useBean> element locates or instantiates a JavaBeans component. <jsp:useBean> first attempts to locate an instance of the bean. If the bean does not exist, <jsp:useBean> instantiates it from a class or serialized template. </li></ul><ul><li>To locate or instantiate the bean, <jsp:useBean> takes the following steps, in this order: </li></ul><ul><li>Attempts to locate a bean with the scope and name you specify. </li></ul><ul><li>Defines an object reference variable with the name you specify. </li></ul><ul><li>If it finds the bean, stores a reference to it in the variable. If you specified type, gives the bean that type. </li></ul><ul><li>If it does not find the bean, instantiates it from the class you specify, storing a reference to it in the new variable. </li></ul><ul><li>If <jsp:useBean> has instantiated (rather than located) the bean, and if it has body tags or elements (between <jsp:useBean> and </jsp:useBean>), executes the body tags. </li></ul>
  40. 41. jsp:useBean <ul><li>The body of a <jsp:useBean> element often contains a <jsp:setProperty> element that sets property values in the bean. As described in Step 5 , the body tags are only processed if <jsp:useBean> instantiates the bean. If the bean already exists and <jsp:useBean> locates it, the body tags have no effect. </li></ul><ul><li>id=&quot; beanInstanceName &quot; </li></ul><ul><ul><li>A variable that identifies the bean in the scope you specify. You can use the variable name in expressions or scriptlets in the JSP page. </li></ul></ul><ul><ul><li>The name is case sensitive and must conform to the naming conventions of the scripting language used in the JSP page. If you use the Java programming language, the conventions in the Java Language Specification . If the bean has already been created by another <jsp:useBean> element, the value of id must match the value of id used in the original <jsp:useBean> element. </li></ul></ul>
  41. 42. jsp:useBean <ul><li>scope=&quot; page |request|session|application&quot; </li></ul><ul><ul><li>The scope in which the bean exists and the variable named in id is available. The default value is page. The meanings of the different scopes are shown below: </li></ul></ul><ul><ul><li>page   You can use the bean within the JSP page with the <jsp:useBean> element or any of the page's static include files, until the page sends a response back to the client or forwards a request to another resource. </li></ul></ul><ul><ul><li>request   You can use the bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another resource. You can use the request object to access the bean, for example, request.getAttribute( beanInstanceName ). </li></ul></ul><ul><ul><li>session   You can use the bean from any JSP page in the same session as the JSP page that created the bean. The bean exists across the entire session, and any page that participates in the session can use it. The page in which you create the bean must have a page directive with session=&quot;true&quot;. </li></ul></ul><ul><ul><li>application   You can use the bean from any JSP page in the same application as the JSP page that created the bean. The bean exists across an entire JSP application, and any page in the application can use the bean. </li></ul></ul>
  42. 43. jsp:useBean <ul><li>class=&quot; package.class &quot; </li></ul><ul><ul><li>Instantiates a bean from a class, using the new keyword and the class constructor. The class must not be abstract and must have a public, no-argument constructor. The package and class name are case sensitive. </li></ul></ul><ul><li>type=&quot; package.class &quot; </li></ul><ul><ul><li>If the bean already exists in the scope, gives the bean a data type other than the class from which it was instantiated. The value of type must be a superclass of class or an interface implemented by class. </li></ul></ul><ul><ul><li>If you use type without class or beanName, no bean is instantiated. The package and class name are case sensitive. </li></ul></ul>
  43. 44. jsp:useBean <ul><li>class=&quot; package.class &quot; type=&quot; package.class &quot; </li></ul><ul><ul><li>Instantiates a bean from the class named in class and assigns the bean the data type you specify in type. The value of type can be the same as class, a superclass of class, or an interface implemented by class. </li></ul></ul><ul><ul><li>The class you specify in class must not be abstract and must have a public, no-argument constructor. The package and class names you use with both class and type are case sensitive. </li></ul></ul>
  44. 45. jsp:setProperty <ul><li>Sets a property value or values in a bean. </li></ul><ul><li>JSP Syntax </li></ul><ul><li><jsp:setProperty name=&quot; beanInstanceName &quot; </li></ul><ul><li>{    property=&quot;*&quot; |    property=&quot; propertyName &quot; [ param=&quot; parameterName &quot; ] |    property=&quot; propertyName &quot; value=&quot;{ stringLiteral | <%= expression %>}&quot; } </li></ul><ul><li>/> </li></ul><ul><li>Examples </li></ul><ul><li><jsp:setProperty name=&quot;mybean&quot; property=&quot;*&quot; /> </li></ul><ul><li><jsp:setProperty name=&quot;mybean&quot; property=&quot;username&quot; /> </li></ul><ul><li><jsp:setProperty name=&quot;mybean&quot; property=&quot;username&quot; value=&quot;Steve&quot; /> </li></ul>
  45. 46. jsp:setProperty <ul><li>Description </li></ul><ul><li>The < jsp:setProperty > element sets the value of one or more properties in a bean, using the bean's setter methods. You must declare the bean with < jsp:useBean > before you set a property value with <jsp:setProperty>. Because < jsp:useBean > and <jsp:setProperty> work together, the bean instance names they use must match (that is, the value of name in < jsp:setProperty > and the value of id in <jsp:useBean> must be the same). </li></ul><ul><li>You can use < jsp:setProperty > to set property values in several ways: </li></ul><ul><li>By passing all of the values the user enters (stored as parameters in the request object) to matching properties in the bean </li></ul><ul><li>By passing a specific value the user enters to a specific property in the bean </li></ul><ul><li>By setting a bean property to a value you specify as either a String or an expression that is evaluated at runtime </li></ul><ul><li>Each method of setting property values has its own syntax, as described in the next section. </li></ul>
  46. 47. jsp:setProperty <ul><li>Attributes and Usage </li></ul><ul><li>name=&quot; beanInstanceName &quot; </li></ul><ul><ul><li>The name of an instance of a bean that has already been created or located with a <jsp:useBean> element. The value of name must match the value of id in <jsp:useBean>. The <jsp:useBean> element must appear before <jsp:setProperty> in the JSP page. </li></ul></ul><ul><li>property=&quot;*&quot; </li></ul><ul><ul><li>Stores all of the values of request parameters in bean properties. The names of the bean properties must match the names of the request parameters. A bean property is usually defined by a variable declaration with matching getter and setter methods </li></ul></ul>
  47. 48. jsp:setProperty <ul><li>property=&quot; propertyName &quot; [ param=&quot; parameterName &quot; ] </li></ul><ul><ul><li>Sets one bean property to the value of one request parameter. In the syntax, property specifies the name of the bean property and param specifies the name of the request parameter by which data is being sent from the client to the server. </li></ul></ul><ul><ul><li>If the bean property and the request parameter have different names, you must specify both property and param. If they have the same name, you can specify property and omit param. </li></ul></ul><ul><ul><li>If a parameter has an empty or null value, the corresponding bean property is not set. </li></ul></ul><ul><li>property=&quot; propertyName &quot; value=&quot;{ string | <%= expression %>}&quot; </li></ul><ul><ul><li>Sets one bean property to a specific value. The value can be a String or an expression that is evaluated at runtime. If the value is a String, it is converted to the bean property's data type according to the conversion rules shown above in TABLE 1 . If it is an expression, its value must have a data type that matches the the data type of the value of the expression must match the data type of the bean property. </li></ul></ul>
  48. 49. jsp:getProperty <ul><li>Inserts the value of a bean property into the result. </li></ul><ul><li>JSP Syntax </li></ul><ul><li>< jsp:getProperty name=&quot; beanInstanceName &quot; property=&quot; propertyName &quot; /> </li></ul><ul><li>XML Syntax </li></ul><ul><li>< jsp:getProperty name=&quot; beanInstanceName &quot; property=&quot; propertyName &quot; /> </li></ul><ul><li>Examples </li></ul><ul><li><jsp:useBean id=&quot;calendar&quot; scope=&quot;page&quot; class=&quot;employee.Calendar&quot; /> <h2> Calendar of </li></ul><ul><li><jsp:getProperty name=&quot;calendar&quot; property=&quot;username&quot; /> </h2> </li></ul>
  49. 50. jsp:getProperty <ul><li>The < jsp:getProperty > element gets a bean property value using the property's getter methods and inserts the value into the response. You must create or locate a bean with < jsp:useBean > before you use <jsp:getProperty>. </li></ul>
  50. 51. jsp:useBean Example <ul><li>< html >< body > </li></ul><ul><li>< form method=‘post’ action=‘usebeanexample.jsp’> </li></ul><ul><li>Name:< input type=‘text’ name=‘name’/><br> </li></ul><ul><li>Branch:< input type=‘text’ name=‘address’/><br> </li></ul><ul><li>RollNo:< input type=‘text’ name=‘rollNo’/><br> </li></ul><ul><li>Date of Birth:< input type=‘text’ name=‘dob’/><br> </li></ul><ul><li>< input type=‘submit’/><input type=‘reset’/> </li></ul><ul><li></ form > </li></ul><ul><li></ body ></ html > </li></ul>
  51. 52. jsp:useBean Example <ul><li>public class Student{ </li></ul><ul><li>String name,address,rollNo,dob; </li></ul><ul><li>public Student(){} </li></ul><ul><li>String getName(){ return name;} </li></ul><ul><li>String setName(String name){this.name=name;} </li></ul><ul><li>String geAddress(){ return address;} </li></ul><ul><li>String setAddress(String address){this.address=address;} </li></ul><ul><li>String getRollNo(){ return rollNo;} </li></ul><ul><li>String setRollNo(String rollNo){this.rollNo=rollNo;} </li></ul><ul><li>String getDOB(){ return dob;} </li></ul><ul><li>String setDOB(String dob){this.dob=dob;} </li></ul><ul><li>} </li></ul>
  52. 53. jsp:usebean Example <ul><li>Usebeanexample.jsp </li></ul><ul><li><%@page language=‘java’ %> </li></ul><ul><li>< jsp:useBean id=‘stuobj’ class=“Student” scope=‘request’> </li></ul><ul><li>< jsp:setProperty name=‘stuobj’ property=‘name’/> </li></ul><ul><li>< jsp:setProperty name=‘stuobj’ property=‘address’/> </li></ul><ul><li>< jsp:setProperty name=‘stuobj’ property=‘rollNo’ param=‘rollNo’/> </li></ul><ul><li>< jsp:setProperty name=‘stuobj’ property=‘DOB’ param=‘dob’/> </li></ul><ul><li></jsp:useBean> </li></ul><ul><li><% out.print(“student name is”+stuobj.getName()); %> </li></ul>
  53. 54. JSP Implicit Objects <ul><li>Object Name Type </li></ul><ul><li>request HttpServletRequest </li></ul><ul><li>response HttpServletResponse </li></ul><ul><li>application ServletContext </li></ul><ul><li>Session HttpSession </li></ul><ul><li>page Object </li></ul><ul><li>config ServletConfig </li></ul><ul><li>pageContext PageContext </li></ul><ul><li>Out JspWriter </li></ul><ul><li>Exception Throwable </li></ul>
  54. 55. JSP session object
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×