Wednesday, 18 September 2013

JSP implicit objects

JSP implicit objects


We are using jsp implicit objects, when there are communication required  for your application. This could be a communication the application and client and jsp page.
It could be a communication between a servlet and which sent a request to a jsp page and your jsp page. Or it could be communicaiton between jsp page and number of other objects like servlets and jsps
Here a communication means that there is some kind of the data sharing which is required. So whenever there is some kind of data sharing is required for your application, you are born to use implicit objects in your jsps.
For example in your servlets you query a database to get some results and you want to show that results in your jsp page, so you can set that value in your servlets and retrieve the value in your jsps using one of the implicit objects
Now we can see what are the implicit objects we have, how to use them?

1. What is an implicit object?
Ans:
An implicit object is a set of predefined objects, readily available for use. Here no need to create the object like general java format, directly we can use the objects, without using new operator. Without creating object we can use, bcoz servlet engine provides the implicit objects.

2. List out the implicit objects?

   1. out : instance of JspWriter
   2. request : instance of HttpRequest
   3. response : instance of HttpResponse
   4. session : instance of HttpSession
   5. config : instance of ServletConfig
   6. application : instance of ServletContext
   7. page : instance of JSP page
   8. pageContext : instance of Special object

Following is the basic example of how we can use the implicit objects in jsp page:


<html>
 <head>
   <title> JSP Implicit objects </title>
 </head>
 <body>
  <%
   if("YES" equals(request.getAttribute("SCOPE")))
   {
     out.println("REQUEST");
   }
   else
    {
     if("YES" equals(session.getAttribute("SCOPE")))
     {
      out.println("SESSION");
     }
    else
     {
       out.println("NONE");
     }
   }
 %>
 </body>
</html>


As you know that in jsp whenever you want to write java or you want to put a java code.  You use these scriptlets tags: <%........%>
The above is my simple jsp page where the body of the jsp page im using a scriptlet and Im checking whether the value of the attribute "SCOPE" is equal to "YES" or not?, if it is true im printing "REQUEST" or I'm going to print "SESSION".
So basically here you have a look: request, out, session are the implicit objects

if("YES" equals(request.getAttribute("SCOPE")))
   {
     out.println("REQUEST");
   }

...
if("YES" equals(session.getAttribute("SCOPE")))
....

You can see that there is nothing special about this implicit objects, you can use it as any other normal objects. You create a object of a class and you can call the methods of that class so there is nothing special the way you used for but only thing is that already available objects and it is predefined. So you can directly you these objects without construction.

So first implicit object is out

1. out



  • If you look our example, we used "out" object to print REQUEST, and we used to print SESSION here, that indicates that out is an implicit object which is instance of JSPWriter class.
  • So whenever you want to write any kind of statement to the jsp page, you are going to use the out implicit object.
  • You can also use expression to display, and other way is out is the implicit object.
  • We know that every jsp page at the end is converted into a html, and it is rendered to the client. So a jsp is converted to a servlet but ultimately when a page is rendered to the end client it is nothing but an html page because your browser , web browser can understand only html language. So it is goint to send html page to your web browser. A simple html terms when you want to display on your page , what you do that you write that text whatever you want to be displayed in the body element of the html.
  • Similarly this is java way of displaying the text on the web page , so there is dynamic text which needs to be displayed on your html page you definately can not use simple html because html static but not dynamic. When you want dynamic to print on the page, you can use the "out " implicit object

2.  request:

A request implicit object is nothing but an instance of HttpServletRequest class. We know that for every servlet , HttpServletRequest and HttpServletResponse objects are passed by the Web container to every servlet. Since a jsp is nothing but a servlet at the end. You should have access to all the objects which you have in a servlet so for that reason you have access to the Http request object in your jsps and you can access the using implicit object request.
And you use these attributes stored in request object for display/basic validation in jsp. 


As we discussed, lets say that when the client makes the request to particular application, the servlet gets invokes first, and the servlet takes some helper object which is nothing but another java class and lets say that it is connecting to the data base to get some values, which is the value and assigns the value a is 10. 
Now value a =10 is passed back to the servlet. Now how do you pass this information to the jsp page to be displayed. Now I want to display value of vari a to the jsp page. Now I can display variable a value to the jsp page by using implicit object "request" . Example: request.setAttribute("a", "10");
So in my servlet Im going to set the attribute a with the value of 10 with the request scope and forward that request to the jsp page.
Now request implicit object i can say request.setAttribute("a","10"); and simply print that value to the jsp page. So why did I use a request attribute here, in this scenario because only place which I concern the value of a is in this jsp page.

And it is very important that for which lever or what scope you are going to set these attributes values in your application because if you are setting in a request level (i.e request.setAttribute()) that value is only available for that particular request only after that it is not stored in any where in the memory, it is discarded. If you want to availabe after request is over, but this will be discussed later, right now i dont want to waste memory .Unless you need this values then no need to set it.

2. response implicit object





  • We also have response object like request object.
  • You also have the access to HttpServletResponse in your jsps and that is through the response implicit object. Basically the response implicit object is used to send the response back to the client . Sending response back to the client is nothing but a html page but we also know that apart from html there are lot of other types of files we can send back to the client (we can send jar, zip, jpj, avi, excel spreadsheet and lot of other things)
  • In order to send all these different types of files to the end client you should use the "response" implicit object. So basically when you are sending these different types of files to the client , you are going to set the MIME type first in your response, so by default MIME type is nothing but html, but if you want to send a jar file then you have to change the MIME type .  If you want to send a movie file you have to change the MIME type and you send appropriate response in the response object.


Second advantage:


  • One thing we need to talk about the response object is, the use send redirect, while discussing servlets we came across the user request dispatcher, and I said that when a particular servlet in your application can not serve the request any more , then it can send request to some other servlets which can handle that request. In a similar manner even for a jsp you can send that request some other jsp page or some other servlet which can handle the request.

  • So lets say you have a page, that you have a shopping card web page in your application, you have changed the where you want to display the shopping card application, you changed the lot of things around that, and you also changed the jsp page for the shopping card and it is a new jsp page. So whenever people come back to that old url for shopping card and they would not see anything in that page because you moved everything to the new page. Now you want to redirect your clients to the new page, in that case you can use "response" implicit object.  Pass the new url to the sendRedirect() method. So when you are using sendRedirect()  you are actually sending the request back to the client, in the case of Request dispatcher you are not sending the request back to the client and here the client is nothing but the browser but not the end client.

requestDispatcher() and sendRedirect():

  • Now we have requestDispatcher and sendRedirect(), in requestDispatcher it is directly forward to servlet2 without any kind of interruption or sending back request to the client but when you are using sendRedirect() jsp1 can not serve the request, it is going to send the response back to the client which is nothing but the browser. Jsp1 says that I can not serve the request but I know that particular "url" , this jsp can serve a request, please make a request again to this new url, and your browser now is going to make a second request to this new url (i.e sendRedirect("url")) and new JSP is displayed to the end user.
  • So for end user prospective, using requestDispatcher(), sendRedirect() does not really make any sense because all he is sees a new jsp page he does not know what internally happens whether the application send back the response to the browser or directly forward to the request to the new page, really he does not know. 


Difference between sendRedirect() and requestDispatcher()

1. when you are using sendRedirect() you can redirect the page to completely different application which is out side your application, that is you can send a request from yahoo to google because it is nothing but a url.
1. when you are using requestDispatcher(), you can only send the request to, you can only forward request to another page within your same application


3. session




  • So when we talk about the data sharing in request we said that in previous example, since this value of a is only being used in jsp page only, I'm going to set this attribute a at the request scope.
  • So now lets say that I want that value of a to be printed multiple times in different jsp pages in my application, lets say that it is user name now to be printed . So when the user logs in to the system on each and every page I want to display the username at right hand top of the page.
  • Here the user makes request 1 and my application sends back a response back , and user makes another request, my application sends back another response so this can happen a number of times .
  • So you are constantly communicating with the user between different requests, we know that Http is a stateless protocol so Http really does not understand or does not really know one particular request is used from same user or different user. So Http protocol treats every new request is a completely different request and it is not able to differentiate between different request whether it is coming from same user or not? But when you are using "session" tracking in your application you will have ability to track your request, you will have ability to find whether the particular request is coming from a same client or different client.


Now lets talk about the servlet sessions



  • When you want to share a particular data or particular information across multiple pages for the same client so when the same client sends multiple requests its nothing but a session so 'n' number of requests and responses from the same client is nothing but a one session. 
  • And you want some kind of data to be persistent across multiple requests for the same client you save that value in the session object, in this case I save the value in session object, so Im going to save user name at the session level. Session scope but not the request scope









  • When you want to share a particular data or particular information across multiple pages for the same client so when the same client sends multiple requests its nothing but a session so 'n' number of requests and responses from the same client is nothing but a one session. 

    • Now Im going to forward that request to jsp, where again im retrieving it using the  session implicit object and display on that page.

    Now we have config and application implicit objects

    servlet config and application implicit objects

    config and application :

    config implicit object is nothing but instance of ServletConfig,
    And application object is nothing but instance of ServletContext.
    We know that when something used to be shared across the jsp page , use the config object and when you want to share something across the whole application we use the context object.
    So similarly you use config and application implicit objects .

    page:


    • page is an instance of object
    • And you have one page implicit object for every jsp page,
    • Lets take one scenario, each and every page in your application you want to display some common text on the header and footer region. So instead of having that common text , present in each and every jsp page in your application it would be good design to have that comman header and footer text in a different header.jsp and footer .jsp file and you are going to add that header and footer to each and every page in the application .

    In main page we will code like below:

    <jsp:include page="/header.jsp"/>
    <jsp:include page="/footer.jsp"/>

    • Here Im using jsp:include action to add the header and footer pages in my main page, here we are going to see what are the different actions we have in future classes but now you can make a note that : jusp:include action to a different jsp page in your jsp page ok 
    • So ultimately when the end user sees the page it does not really know that sources for all these  , source for the jsp page coming from different jsp files, all the user knows that only one page.
    • So one of the advantage of this kind of design is that if at all if you want to make any kind of change in either the header or footer regions you don't really have to make changes in each and every page for your application but it would be fine if you just make the change in one place that is either the header.jsp or footer.jsp
    • So making change in one place is nothing but a 1000 of pages . So here the header.jsp will have its own page implicit object( ex: Hello<%=userName%>), the footer.jsp will have its own implicit object( ex: <table><tr>
                 <td>Contact Details</td>
                </tr></table>
    • And the rest of the main page will have its own page implicit objects (example<jsp:include page="/header.jsp"/>, <jsp:include page="/footer.jsp"/> )
    •  You  should not worry about the page object because we would rarely use the page object in jsps .

    pageContext object



    • Till now we discussed all the different ways we can share data between these applications , between the jsp pages. And we know that : first is page, page level, which is restricted to that jsp page only not even request
    • The next level we have : request object: that is request level, and the next level we have session level which comprises of a number of requests and ultimately we have application level which covered whole application , so basically application object covers all the jsp and servlets within the application .
    • Here from bottom to top, Im going from least visible to most visible object , least visible in terms of scope, most visible in terms of scope.
    • So lets say that in my servlet i store the value in the request object, but in the jsp im trying to retrieve the same value from the session object, will I get that value if I set it as request in session level in my jsp ? answer is "no", because if I set the value attribute value at request level in my jsp as well as I have to retrieve that value at the request level . If I set it as request level I have to retrieve it in request level. If i set it is as session level , i have to retrieve it at session level only , I can not simply set it in request and try to retrieve from that value from session level. So for that reason to avoid certain confusion we have the pageContext object.
    • pageContext implicit object is a convenient object which is available in jsp. Lets say that you saved the value in request object , in jsp you really know whether you have saved it in session or request level. So you dont know at which scope you saved , so when you use the pageContext object, basically try to retrieve at the page level first, if it does not retrieve in page level it goes and check at the request level , if it does not get at the request level, it goes to check at the session level to get the value, if pageContext object does not get the value at the session  level, it goes to application level to get the value.
    • So basically when you use the pageContext object it is going to check each and every level starting from least visible to most visible level , if at all match found in any of the scope, it is going to give you the value.
    • So in this case you dont have really worry in jsp, this particular attribute is set at session level, or request level or not? 

    No comments:

    Post a Comment