0% found this document useful (0 votes)
31 views

JSP ImplicitObjects

Uploaded by

Ashok Nutalapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
31 views

JSP ImplicitObjects

Uploaded by

Ashok Nutalapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 22
Implicit Objects in JSP These objects are created by the web container that are available to all the jsp pages. These objects are also called as Pre-Defined Variables in JSP. JSP implicit objects are created during the translation phase of JSP to the servlet. There are 9 types of implicit objects available in the JSP : lout 6.application 2.request 7.pageContext 3.response 8.page 4.session 9.config 5.exception Out Object: * Out is one of the implicit objects to write the data to the buffer and send output to the client(browser) in response. * Itis the object of JspWriter Class. Example: “outobj.jsp” <% out.print("This is Example
"); out.print(" Demo for OUT Object"); %> aimee: ae Sl € > © O tecathost8080/spex, tO: This is Example Demo for OUT Object Request Object: * The request is an implicit object of type HttpServletRequest. It was created for each jsp request by the web container. + It can be used to get request information from web page or document. * By using this request object ,we can able read values from input elements in JSP. Example: “requestobj.html” Request Object

requestobj.jsp <% String name=request.getParameter("uname"); out.print("welcome "+name); %> Output: Ba fey x + ae Picea tm € FS O bocahostsvsnjspevrequestobintmi te @ € 2 SO locahoseste0/spertequs: Bin ® welcome Adin Response Object: + In JSP, response is an implicit object of type HttpServletResponse. It is created by the web container for each jsp request. + It can be used to add or manipulate response such as redirect response to another resource, send error etc. Example: Response Object
URL:
responseobj.jsp <% String url=request.getParameter("url"); response.sendRedirect("https://"+url); %> Output: Bh egore o x € > CO lecahost8080/ [URL {wa google com * Session Object: + In JSP, session is an implicit object of type HttpSession. We can use this object to set, get or remove attribute or to get session information. Example: Session.html Bivwensmsincouet * a € > SO bocathost Madhu T ”
session.jsp <% String name=request.getParameter("uname"); out.print("Welcome "+name); session.setAttribute("user",name); %> € > © O localhost: Welome Madu T
Nabe Next Page ‘- second.jsp <% String name=(String)session.getAttribute("use: out.print("Hello "+name); %> FA locathost608 < SO becathost Hello Madhu T Exception Object: * The exception is normally an object that is thrown at runtime. Exception Handling is the process to handle the runtime errors. There may occur exception any time in your web application. * In JSP, exception is an implicit object of type java.lang.Throwable class. This object can be used to print the exception. But it can only be used in error pages. Example: exception.html BB locatnosteoe0/s2vexeptonn: x € > SO locathost: Nol:i2
No1: No2:
Node divide process.jsp <%@ page errorPage="error.jsp" %> <% String num1=request.getParameter("n1"); String num2=request.getParameter("n2"); int a=Integer.parselnt(num1); int b=Integer.parselnt(num2); int c=a/b; out.print("division of numbers is: "+c); %> € SO bocathost: division of numbers is: 2 error.jsp <%@ page isErrorPage="true" %> aa

Sorry an exception occured!

Exception is: <%= exception %> € Sorry an exception occured! Exception is: java lang ArithmeticException: / by zero Application Object: * This is basically used for getting initialization parameters and for sharing the attributes & their values across the entire JSP application. + which means any attribute set by application implicit object would be available to all the JSP pages. * getAttribute(String attributeName): It returns the object stored in a given attribute name. Ex:String s = (String)application.getAttribute("MyAttr"); * setAttribute(String attributeName, Value): It sets the value of an attribute or in other words it stores an attribute and its value in application context, which is available to use across JSP application. Ex:application.setAttribute(“MyAttr”, “value”); applnobj.jsp <% Integer counter= (Integer)application.getAttribute("numOfVisits"); if( counter ==null | | counter == 0 ){ counter = 1; Jelse{ counter = counter+ 1; } application.setAttribute("numOfVisits", counter); >

Total number of hits to this Page is: <%= counter%>

By lcahoresjppeapinns | ot € > © © becahoses080/sper/aprindiin tO ‘Total number of hits to this Page is: 1 cok Be loco + € 2 © O kecahoses0sojspevappinobjsp tt @ ‘Total number of hits to this Page is: 2 pageContext Object: + In JSP, pageContext is an implicit object of type PageContext class. + The pageContext object can be used to set, get or remove attribute from one of the following scopes: Y page — Scope: PAGE_CONTEXT v request — Scope: REQUEST_CONTEXT v session — Scope: SESSION_CONTEXT Y application — Scope: APPLICATION_CONTEXT * In JSP, page scope is the default scope. Methods of pageContext Implicit Object * getAttribute (String AttributeName, int Scope) * setAttribute(String AttributeName, AttributeValue, int Scope) pagecontextobj.html Login Page
Enter User-Id:

Enter Password:

of &
- “Ve € > S @ bocaihoses080/ ageconte.. y @ Enter User-Id: Madhu Enter Password: Login pagecontextobj.jsp <% String id=request.getParameter("uid"); String pass=request.getParameter("upass"); out.printin("hello "+id); pageContext.setAttribute("uname", id, PageContext.SESSION_SCOPE); pageContext.setAttribute("pwd", pass, PageContext.SESSION_SCOPE); %> Click here to see Password ] i toca x € SO locathost:s08r ite. o& % @ hello Madhu Click here to see Password display.jsp <% String uname= (String) pageContext.getAttribute("uname", PageContext.SESSION_SCOPE); String pwd= (String) pageContext.getAttribute("pwd", PageContext.SESSION_SCOPE); out.printIn("Hi "+uname); out.printIn("Your Password is: "+pwd); > — . oicmea € > © © tocatnosts0s0yjsperdisplayjsp * @: Hii Madhu Your Password is: 1234 page Objec * Page implicit variable holds the currently executed servlet object for the corresponding jsp. * Acts as this object for current jsp page. * This object is very rarely used. Example: <% String pageName = page.toString(); out.printIn("Page Name is " +pageName);%> =| 0 x + € > © © tecatnost8080/jspex/pageotiis * @ Page Name is org apache jsp pageobj_jsp@49fadl config Object: * In JSP, config is an implicit object of type ServletConfig. This object can be used to get initialization parameter for a particular JSP page. * The config object is created by the web container for each isp page. * Generally, it is used to get initialization parameter from the web.xml file. Example: “configobj.jsp” <% String sname=config.getServietName(); out.print("Servlet Name is: "+sname); %> web.xml SampleServlet /configobj.jsp SampleServlet /config ie CO kecalhost: Servlet Name is: SampleServlet

You might also like