WEB PROGRAMMING WITH JSP SERVLET JSP IMPLICIT OBJECTS
WEB PROGRAMMING WITH JSP & SERVLET JSP IMPLICIT OBJECTS
CONTENTS • • Implicit Objects Input & Output Objects Scope Communication Objects Servlet Objects The Error Objects Workshops Exercises
IMPLICIT OBJECTS – IB • Does not initialize or declare the IB • Are loaded by the Web Container automatically and maintains them in a JSP page (Available for scriptlets or expressions) • Created using directives and accessible according to the specified scopes • The names of the implicit objects are reserved words of JSP • The scopes for the IB in JSP page including page, request, session, and application • Access dynamic content using Java. Beans • Syntax: Implicit. Object. method(params) • Types of Implicit Objects – Input & Output Objects • The objects control page input and output • Are request, response and out – Scope Communication Objects: provide access to all objects available in the given scope – Servlet Objects • Provides information about the page context • Processes request objects from a client and sends the response objects back to the client
IMPLICIT OBJECTS – IB (cont) • Types of Implicit Objects (cont) – The Error Objects • The object handles errors in a JSP page (exception) • Can access this object by declaring your JSP page as an error page <%@page is. Error. Page=“true” %> Object Class / Interface page javax. servlet. jsp. Http. Jsp. Page config javax. servlet. Servlet. Config request javax. servlet. http. Http. Servlet. Request response javax. servlet. http. Http. Servlet. Response out javax. servlet. jsp. Jsp. Writer session javax. servlet. http. Http. Session application javax. servlet. Servlet. Context page. Context javax. servlet. jsp. Page. Context exception java. lang. Throwable
IB – INPUT & OUTPUT OBJECTS Objects Descriptions request - Refer to the current request made by the client that is being processed by JSP container. The container passed the request IB to JSP page as a parameter to the _jspservice(). - Implement the javax. servlet. http. Http. Servlet. Request interface - Syntax: request. method(params) - Scope: request -Ex: request. get. Parameter(“username”); response - Refers the result that is returned to the user after a JSP processed - Implement the javax. servlet. http. Http. Servlet. Response interface - Syntax: response. method(params) - Scope: page - Ex : response. add. Cookie(cookie) out - Represent the output stream for the JSP page (send to client) - Implement the javax. servlet. jsp. Jsp. Writer interface (the buffer size is supported by Servlet) - Syntax: out. method(params) - Scope: page - Ex: out. println(“output stream”)
EXAMPLE <% response. set. Date. Header ("Expires", 0); response. set. Header ("Pragma", "no-cache"); if(request. get. Protocol (). equals ("HTTP/1. 1")){ response. set. Header ("Cache-Control", "no-cache"); out. println ("<b>The protocol used is: </b> " + request. get. Protocol ()); } %>
IB – SCOPE COMMUNICATION OBJECTS Objects Descriptions session - Specify data and store information in the current session. - Implement the javax. servlet. http. Http. Session interface - Syntax: session. method(params) - Scope: session - Ex: session. set. Attribute(“username”, “Aptech”); application - Represent the application of the required JSP page and represent the servlet context about Web Application in which it is running. - Implement the javax. servlet. Servlet. Context interface - Syntax: application. method(params) - Scope: application - Ex: application. set. Attribute(“username”, “Aptech”); - An instance of Pages (javax. jsp. Page. Context) - Enable access to the JSP page and the attributes associated with that page. Context - provides following fields to find the scope or specify the scope of the objects (PAGE, REQUEST, SESSION, and APPLICATION) - Syntax: page. Context. method(params) - Ex: page. Context. get. Attributes(“username”);
EXAMPLE <% if(page. Context. get. Attribute ("page. Count")==null){ page. Context. set. Attribute ("page. Count", new Integer(0)); } if(session. get. Attribute ("session. Count")==null){ session. set. Attribute ("session. Count", new Integer(0)); } if(application. get. Attribute ("app. Count")==null){ application. set. Attribute ("app. Count", new Integer(0)); } %>
EXAMPLE (cont) <% Integer count = (Integer)page. Context. get. Attribute ("page. Count"); page. Context. set. Attribute ("page. Count", new Integer(count. int. Value ()+1)); Integer count 2 = (Integer)session. get. Attribute ("session. Count"); session. set. Attribute ("session. Count", new Integer(count 2. int. Value ()+1)); Integer count 3 = (Integer)application. get. Attribute ("app. Count"); application. set. Attribute ("app. Count", new Integer(count 3. int. Value ()+1)); %> <b>Page Count =</b> <%= page. Context. get. Attribute ("page. Count") %><br/> <b>Session Count =</b> <%= session. get. Attribute ("session. Count") %><br/> <b>Application Count =</b> <%= application. get. Attribute ("app. Count") %><br/> <b>Time =</b> <%= new java. sql. Time(System. current. Time. Millis ()) %><br/>
SERVLET OBJECTS Objects Descriptions page - Represents the servlets and the initialization parameters of the servlet are stored in the config IB - Use “this” reference and the page IB represents it. - Implements the javax. lang. object interface - Syntax: <%@ page info = “information” %> - Scope: page config - Represent the configuration of the servlet data - Implement the javax. Servlet. Config interface - Access objects through config. get. Init. Parameter(“par”) - Scope: page
EXAMPLE <%@ page language="java" content. Type="text/html" %> <%@ page import="java. util. Calendar, java. util. Date" %> … <% Calendar cal = Calendar. get. Instance (); %> Today's Date and Time is: <%= new Date() %><br/> The JSP Page is created by <%= ((Http. Servlet)page). get. Servlet. Info () %><br/>
IB – ERROR OBJECTS Objects Descriptions - Refer to the runtime exception in an error page - Is availble only on pages that are assigned as error page using the is. Error. Page attribute of the page directive. - Implement the javax. lang. Throwable interface - Exception methods are supported exception + get. Message() : return the error message associated with the exception + to. String() : Return a string with the class name of the exception within the error message. + print. Stack. Trace(): prints the execution stack in effect when the exception was thrown to the designated output stream
EXAMPLE • A JSP page traps errors occurred <%@ page error. Page="error. jsp" %> <html> <head> <title>Tag Example</title> </head> <body> <%! String name; %> <% name = request. get. Parameter("name"); if(name==null) name="World“; %> <h 1>Hello, <%= name %></h 1> <% out. println("<H 1>Hello " + name + "</H 1>"); %> <% int num=Integer. parse. Int("a"); %> </body> </html>
EXAMPLE (con’t) • The error page (error. jsp) <%@ page is. Error. Page="true" import="java. io. *" %> <html> <head> <title>Exception</title> </head> <body> <font color="red"> <%= exception. to. String() %> </font> </body> </html>
WORKSHOP ACTIVITIES Building the JSP files can do as • Using IB – Input & Output to present the used protocol • Using IB – Scope Communication to present the scope of Session, Page, and application • Using IB – Servlet to get the Information of Servlet to present the browser
EXERCISES • Do it again all of demos • Using JSP pages to write the programs that can do – Building the form with username, book. Type (using combo box), after the user click submit button the other JSP page presents the inputted username and book. Type – Building Login program – Building the ATM program with functions as checking pin and choose the correspondence transaction (deposit, withdrawn, …) – Display the name and IP address of computer (Hint: using request. get. Remote. Host() and request. get. Remote. Addr()) • Applying the error handling on JSP page to above exercices
- Slides: 16