Java Implicit Objects Scope Implicit objects provide access
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
Java Implicit Objects List • • • 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 page. Context: 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
Java Implicit Objects Example <html> <head> <title>Implicit Objects</title> <p> Storing a string to the application. . . <% application. set. Attribute("name", "Meeraj"); %> </head> Retrieving the string from application. . . <body style="font-family: verdana; font-size: 10 pt"> <b>Name: </b> <p> <%= application. get. Attribute("name") %> Using Request parameters. . . </p> <b>Name: </b> <%= request. get. Parameter("name") %> <p> </p> Storing a string to the page context. . . <p> <% page. Context. set. Attribute("name", "Meeraj"); %> <% out. println("This is printed using the out implicit variable"); %> </p> <p> Storing a string to the session. . . <% session. set. Attribute("name", "Meeraj"); %> Retrieving the string from session. . . <b>Name: </b> <%= session. get. Attribute("name") %> </p> Retrieving the string from page context. . . </br> <b>Name: </b> <%= page. Context. get. Attribute("name") %> </p> </body> </html>
JSP Actions Basics & Types • Processed during the request processing phase. – As opposed to JSP directives which are processed during translation • Standard actions should be supported by J 2 EE compliant web servers • Custom actions can be created using tag libraries • The different actions are – Include action – Forward action – Param action – use. Bean action – get. Property action – set. Property action – plug. In action
JSP Actions Include • Include action used for including resources in a JSP page • – Include directive includes resources in a JSP page at translation time – Include action includes response of a resource into the response of the JSP page – Same as including resources using Request. Dispatcher interface – Changes in the included resource reflected while accessing the page. – Normally used for including dynamic resources Example – <jsp: include page=“inlcuded. Page. jsp”> – Includes the output of included. Page. jsp into the page where this is included.
JSP Actions Forward • Forwards the response to other web specification resources – • • Same as forwarding to resources using Request. Dispatcher interface Forwarded only when content is not committed to other web application resources – Otherwise an Illegal. State. Exception is thrown – Can be avoided by setting a high buffer size for the forwarding jsp page Example – <jsp: forward page=“Forwarded. html”> – Forwards the request to Forwarded. html
JSP Actions Param • Used in conjunction with Include & Forward actions to include additional request parameters to the included or forwarded resource • Example <jsp: forward page=“Param 2. jsp”> <jsp: param name=“First. Name” value=“Sanjay”> </jsp: forward> – This will result in the forwarded resource having an additional parameter First. Name with a value of Sanjay
JSP Actions use. Bean • Creates or finds a Java object with the defined scope. – • Object is also available in the current JSP as a scripting variable Syntax: <jsp: use. Bean id=“name” scope=“page | request | session | application” class=“class. Name” type=“type. Name” | bean=“bean. Name” type=“type. Name” | type=“type. Name” /> • – At least one of the type and class attributes must be present – We can’t specify values for bith the class and bean name. Example <jsp: use. Bean id=“my. Name” scope=“request” class=“java. lang. String”> <% first. Name=“Sanjay”; %> </jsp: use. Bean>
JSP Actions get/set. Property • get. Property is used in conjunction with use. Bean to get property values of the bean defined by the use. Bean action • Example (get. Property) – <jsp: get. Property name=“my. Bean” property=“first. Name” /> – Name corresponds to the id value in the use. Bean – Property refers to the name of the bean property • set. Property is used to set bean properties • Example (set. Property) – <jsp: set. Property name=“my. Bean” property=“first. Name” value=“Sanjay”/> – Sets the name property of my. Bean to Sanjay. Example (set. Property) – <jsp: set. Property name=“my. Bean” property=“first. Name” param=“fname”/> – Sets the name property of my. Bean to the request parameter fname – <jsp: set. Property name=“my. Bean” property=“*”> – Sets property to the corresponding value in request
JSP Actions plug. In • Enables the JSP container to render appropriate HTML (based on the browser type) to: – Initiate the download of the Java plugin – Execution of the specified applet or bean • plug. In standard action allows the applet to be embedded in a browser neutral fashion • Example <jsp: plugin type=“applet” code=“My. Applet. class” codebase=“/”> <jsp: params> <jsp: param name=“my. Param” value=“ 122”/> </jsp: params> <jsp: fallback><b>Unable to load applet</b></jsp: fallback> </jsp: plugin>
Example Loan Calculator Gets input to compute loan from user Selects the right jsp for calculating loan Computes loan based on simple interest simple. jsp error. jsp Handles error if exception is thrown Header. jsp Footer. jsp index. jsp controller. jsp Header. jsp Footer. jsp Calculate loan Computes loan based compound. jsp on compound interest error. jsp Handles error if exception is thrown Calculate loan
Loan Calculator index. jsp <html> <head> <title>Include</title> <tr> <td>Simple: </td> <td><input type="radio" name="type" value="S" /></td> </head> </tr> <body style="font-family: verdana; font-size: 10 pt; "> <tr> <%@ include file="header. html" %> <td>Period: </td> <form action="controller. jsp"> <td><input type="text" name="period"/></td> <table border="0" style="font-family: verdana; fontsize: 10 pt; "> <tr> <td>Amount: </td> <td><input type="text" name="amount" /> </tr> <td>Interest in %: </td> <td><input type="text" name="interest"/></td> </tr> <td>Compound: </td> <td><input type="radio" name="type" value="C" checked/></td> </tr> </table> <input type="submit" value="Calculate"/> </form> <jsp: include page="footer. jsp"/> </body> </html>
Loan Calculator Miscellaneous controller. jsp error. jsp <% <%@ page is. Error. Page="true" %> String type = request. get. Parameter("type"); if(type. equals("S")) { %> <jsp: forward page="/simple. jsp"/> <% } else { <html> <head> <title>Simple</title> </head> <body style="font-family: verdana; font-size: 10 pt; "> <%@ include file="header. html" %> %> <p style="color=#FF 0000"><b><%= exception. get. Message() %></b></p> <jsp: forward page="/compound. jsp"/> <jsp: include page="footer. jsp"/> <% } %> </body> </html> header. jsp <h 3>Loan Calculator</h 3> footer. jsp <%= new java. util. Date() %>
Loan Calculator simple. jsp <%@ page error. Page="error. jsp" %> <%! public double calculate(double amount, double interest, int period) { if(amount <= 0) { throw new Illegal. Argument. Exception("Amount should be greater than 0: " + amount); } if(interest <= 0) { throw new Illegal. Argument. Exception("Interest should be greater than 0: " + interest); } if(period <= 0) { throw new Illegal. Argument. Exception("Period should be greater than 0: " + period); } return amount*(1 + period*interest/100); } %> <html> <head> <title>Simple</title> </head> <body style="font-family: verdana; font-size: 10 pt; "> <%@ include file="header. html" %> <% double amount = Double. parse. Double(request. get. Parameter("amou nt")); double interest = Double. parse. Double(request. get. Parameter("inter est")); int period = Integer. parse. Int(request. get. Parameter("period")); %> <b>Pincipal using simple interest: </b> <%= calculate(amount, interest, period) %> <br/> <jsp: include page="footer. jsp"/> </body> </html>
Loan Calculator compound. jsp <%@ page error. Page="error. jsp" %> <%! public double calculate(double amount, double interest, int period) { if(amount <= 0) { throw new Illegal. Argument. Exception("Amount should be greater than 0: " + amount); } if(interest <= 0) { throw new Illegal. Argument. Exception("Interest should be greater than 0: " + interest); } if(period <= 0) { throw new Illegal. Argument. Exception("Period should be greater than 0: " + period); <html> <head> <title>Compound</title> </head> <body style="font-family: verdana; font-size: 10 pt; "> <%@ include file="header. html" %> <% double amount = Double. parse. Double(request. get. Parameter("amo unt")); double interest = Double. parse. Double(request. get. Parameter("inter est")); int period = Integer. parse. Int(request. get. Parameter("period")) ; %> } <b>Pincipal using compound interest: </b> return amount*Math. pow(1 + interest/100, period); <%= calculate(amount, interest, period) %> } %> <br/> <jsp: include page="footer. jsp"/> </body> </html>
Difference Between Include Directive & Include Action tags: Sl. No. Include Directive Include Action Tag 1 Syntax: <%@include file=“filename. jsp”%> Syntax: <jsp: include page=“filename. jsp”> 2 It inserts the given page and includes the content It is used to include the response in the generated Servlet page during the generated by executing the specified translation phase of the JSP life cycle. JSP page or Servlet. 3 It is used to include files, such as HTML, JSP, XML, or a simple. txt file, into a JSP page statically. 4 The file attribute is the only mandatory attribute The specified JSP page is included in available in the include directive tag & is used to request handing phase of the included refer to the file to be included. JSP page lifecycle. 5 It does not allow you to use the expression for giving the URL path, for including the resource in the JSP page. The response is included during the request processing phase, when the page is requested by the user. It accepts expressions. Ie. , with the include action tag, we can decide the page to be included at runtime.
Using a Bean in a JSP • Follow the steps to declare a bean in a JSP – Create a Bean – Declare the Bean in a JSP by using the <jsp: use. Bean> tag – Access the Bean properties – Generate Dynamic Content – Deploy and run the application
1. Creating a Bean 4. Generating Dynamic content Within a JSP 2. Declaring the Bean in a JSP 3. Accessing the Bean Properties
Laboratory Program 9: Write a Java JSP program to get student information through a HTML and create a Java Bean Class, populate Bean and display the same information through another JSP. Laboratory Program 10: Write a JAVA JSP Program which uses <jsp: plugin> tag to run a applet.
JSP Standard Tag Library (JSTL) • JSTL is a collection of custom tag libraries, which provide core functionality used for JSP documents. • JSTL reduces the use of scriplets in a JSP page. • The use of JSTL tags allows developers to use predefined tags instead of writing the Java code. • JSTL provides four types of tag libraries that can be used with JSP pages.
• The Core JSTL – Used to process a JSP page in an application. • The XML Tag library – Used for parsing, selecting, & transforming XML data in a JSP page. • The Format Tag library – Used formatting the data used in a JSP page. • The SQL Tag library – Is used to access the relational database used in a JSP page.
- Slides: 21