Wednesday 25 September 2013

Error Handling in JSP

Error Handling in JSP


  • Basically what kind of errors can expect in jsp page?
  • We know that jsp is nothing but a html with java so tipically you would get some java type errors and some html type errors in jsp page
  • Lets we talk about java type errors:






Errors in java code in scriptlets:


  • here every one writes certain java logic or java code in jsp page in terms of scriptlets , expressions , declarations and all those stuff. But most of the time if at all writing any kind of business logic in your jsp page will be writing in a scriptlet.
  • Scriptlet is the place where most of your java programming errors in a jsp page would reside and how do you handle these errors? We can write your basic system print statements to track your different variables modifying all these but if at all having your huge amount of code in your jsp page , huge amount of java code in your jsp page and not able to figure of the issue, then it is highly recommended to write the code in side the try ..catch block and the even you will sup-rise to figure out the defects with minutes when you use the try/catch block in jsp page.
  • And most of these errors occur at run time so basically when you are testing you probably would not come across certain scenarios which result in these errors and suddenly out of the blue they pop out the no where .
  • So these are typically difficult to identify but there is one type of error which is easy to identify and those are the html type errors


Errors in Html





  • we know that we got term of html scripting elements in html, we have different element html <body>, <table> and all those things.
  • So you might by mistake write element name incorrectly, or forget to close some element or something like that so those kind of small html issues could occur in jsp page. And you will find out a html error as soon as you the moment you launch your page so if at all any html errors in the jsp page when you launch your page , it is not going to show up properly and that is the problem where we can immediately identify that there was some error in html and you can fix that error.
  • So these are very easy to figure out and they can be caught during testing
1Q: Why is error handling so important in JSPs?


Ans:
We know that a JSP is nothing but a html and this is what the end users sees right, so that means the end user entire world the jsp page is the face of your web application. So the end user really does not care what kind of complex algorithms you are using in the in the back. He does not really be care what kind of logic is used in the back end, the only thing the end user is worried about is what he sees on the page? whatever there on the page, end user likes it, whatever is there on the screen, the end user does not like it? Whatever business logic is write on the back-end is of no use goes in vain, so that is the reason why error handling in JSP pages extremely important.


......................................................---------------------------------------
Lets just have a look at two different error pages which are shown and lets see which one is better than that one?


1. Error page 1.
  1. Error page 1:

so the user has requested the page and the request could not be processed because there was some error but the system has given some very beautiful error message page saying that we are sorry the page is not available and so..and so....


How do you achieve it to display the error message 1?
  • Error pages comes to the rescue
<%@page isErrorPage=”true” %>
<html>
<head>
<title>My Error Page</title>
<head>
<body>
We are sorry, the page you are trying to access is currently not available .
Please try after some time .
</body>
</html>


  • error pages are nothing but simple jsp pages but small difference
  • And the only difference is you have this page directive attribute(ie. IsErrorPage and you said that this attribute is “true”
  • So as soon as iserrorPage attribute is “true” the page directive jsp page become some error page now. And it can be used an error page.
  • This error page which would be showing to the end client
  • So we know that to show that some fancy error page to the client whenever there is an error and we also developed error page now
  • But how do you display this error page to the end user? , the end user actually requested for some other page we did not request for this page, so how do you actually submit this page to the user?


We can do it in two ways:

  1. specify your error page in your main page
  2. specify your error page in web.xml




1. Specify your error page in your main page:




<%@page errorPage=”errPage.jsp”%>
<html>
<head>
<title>This is the main page</title>
</head>
<body>
.......................
..........................
...........................
//ERROR CODE GOES HERE


.............................
...........................
</body>
</html>


  • So whenever an error is in my jsp page , I want to redirect it into error page which I just coded now and show that beautiful message to the end user. How do I do that ? I do that errorPage attribute on the page directive.
  • So whenever there is a error in this jsp page the jsp engine is going to look at the page directive and see if there is any error page defined(errorPage=”errPage.jsp”). And if there is any error page is defined, then it is going to redirect to the this error page ex: errPage.jsp.
  • So whenever there is an error jsp page I'm redirected to this error page and that error page is going to the end user which is a brief description of the error message to the end user.




2. specify error page In web.xml






  • Now lets do that based on error I want to redirect the user to different error pages so if the page is not found I will display some error message to the user and there is some kind of null pointer exception or something like that I will display some other page to the end user . How can you do that you can do that, using the web.xml
<web-app>
................
................
<error-page>
<exception-type>com.a.myExp</exception-type>
<location>/error.jsp</location>
</error-page>






<error-page>
<error-code> 404</error-code>
<location>/errorServlet</location>
</error-page>






.......................
......................
</web-app>



  • So you can define error page even in web.xml
  • So within the <error- page > element , we write an <exception-type> and say that whenever there is an exception of type myExp in one the jsp page I want to redirect it to the this error page I.e error.jsp
  • And we also alternative say whenver there is an error code 404, I want to redirect it this error page (I.e error.jsp)
  • Here again we would have noticed that two ways I can define this, 1. using <exception-type> 2. using the <error-code>

2. error page number 2:



t simply displayes the 404 error message, error message which does not give any information so you do not really know this page available at all with this website down or something is wrong with the code or . So really dont have any idea of is happening behind this in this scenario 2 .


but in Scenario 1 system has displayed clearly neat message saying the reason for the error come back .


  • So end user point of view error page 1 is far more better than the error page 2 so you should give a user brief description of an error message of why that error has happened or ask them to come back of try after some time or something like that instead of just throwing 404 error page can not be displayed error message .
How do you achieve it to display the error message 1?


<%@page isErrorPage=”true” %>
<html>
<head>
<title>My Error Page</title>
<head>
<body>
We are sorry, the page you are trying to access is currently not available .
Please try after some time .
</body>
</html>


Tuesday 24 September 2013

jsp:setProperty action

<jsp:setProperty>


Defn:
Sets property values in a Bean.


  • In a similar manner I would , there could be a need where I want to set the property values in my jsp page again so that I can use this bean again the property value is someother place later point of time, How can i do that? I can do that using the jsp setProperty() method so similarty jsp:getProperty set the property values in a bean and basically setProperty and getProperty actions are used in consuntion with jsp:useBean action so whenever you use jsp:useBean you have the setProperty and getProperty actions, can't exists by its own.
  • So syntax is similar to getProperty:

Syntax:
<jsp:setProperty name="{beanInstanceName}"
         property="*"/property="propertyName"
        param="{parameter name"}
        value="{param value}" }/>


  •  This syntax is similar to <jsp:getProperty>, so  name again would be beanInstanceName 
  • Now lets ignore property= *(star), and you can either set a value for single property name (i.e: property="propertyName"). Lets say I want to set a value for user name , that is property="username"
  • And value would be whatever value you want to set to the "username" attribute 
  • Now alternatively I can also set a value based on certain input entered in my page so lets say that I have  a field for username for my page and I want to update that value whatever value value was entered in that field for username to this property in this userBean , so How can I do that ? I can basically map that to the "param", (i.e param="{parameter name"}) so here this jsp  setProperty element also have the param attribute( see the syntax) , so i can simply map that field name , where i want to , which would be updated for the user to this "param" attribute. In this case whatever value is entered in that username input textbox would be updated to this bean for that particular property (ie. property="*"/property="propertyName")  if we say "username" here to be updated to the username property. 

  • Now What does the *(star) does, if I put a * here means? it means that basically for all the property values in my bean, simply map (param) all parameters to the same name, so lets say that I have two properties in my bean that is user id and username and similarly I have two parameters two input fields in my jsp page , here one is username and one is user id so here the catches the you should have the same name in the input field for the so that it matches the input field name with the property name so  if have the properties as username and user id my input text box name should also be username and user id so that this can automatically map these values to that input fields and update the value of the bean with whatever values you entered input fields 
  • So that's how you can use the * version of the properties attribute so basically you can use jsp:setProperty element in side the jsp useBean  body and and out side the jsp useBean body as well. What is the difference between using inside and outside? Ans is when you use it out side the jsp use Bean body it automatically sets the value of that particular property to the value which you said irrespective of what value it was earlier when it was brought over from the servlet right ! so lets say that i set a value of userid =1 in servlet  and I passed this to the jsp  page and in the jsp usebean action it instantiate the useBean and the value of the id brought in us 1 (one) and suddenly in my page I again do a setProperty i say userid=2 now and it simply sets the value to 2 now , it changes the value whatever was brought but if I use <jsp:setProperty> action inside my jsp use bean body it will change the value or basically it will set the value of that property only when the userBean was not  found so we said earlier that this user useBean action if there is already bean with this name (i.e beanInstanceName)  instantiated its simply gets the reference to that but if it does not find any thing it is going to create a new object and it is going to instantiate the bean class so if you use the <jsp:setProperty> within the bean class , it sets the property value only if it didnt find the bean class  and instantiated the bean class in to the jsp otherwise it would not set the value of that property to the new value simply it is going to retain whatever it is brought from the servlet .


Example:

<jsp:useBean id="userBean"
        scope="session"
        class="com.sharmani.UserBean">
<jsp:setProperty name="userBean"
       property="userName"
       value="Rajendra"/>
</jsp:userBean>
<jsp:getProperty name="userBean" property="userName"/>


  • Jsp:useBean tag with set and get property, i just explained earlier here we are using the setProperty action  (i.e: <jsp:setProperty name="userBean")  within the body of the useBean jsp, here you see, whatever id i have specified in the <jsp:useBean> element i have used as name as setProperty and getProperty actions 

Monday 23 September 2013

jsp:getProperty action

defn:
Gets property values of a Bean object.
Syntax:
<jsp:getProperty name="{beanInstanceName}" property="{propertyName">
It is used to get the property values of a bean and basically whatever value you put in the attribute for the jsp:useBean element would go in the name for jsp get property element.


<jsp:useBean id="userBean" //this element would goes to the jsp get property element
      scope="request"
      class="ub.UserBean"
     type="{package.class}"
     beanName=""{package.beanName}">
</jsp:useBean>

that is <jsp:getProperty name="userBean" property="{propertyName">
Basically saying that you want to set ceretain, you want to get bean values for this particular (ie: beanInstanceName ex: <jsp:getProperty name="{beanInstanceName}" property="{propertyName">)
And in property basically give whatever property name whatever value you want to , example it could be username and set you want to display on jsp page so the property would be username here.

Example program for getProperty tag
Next: <jsp:setProperty> http://improve-jsp.blogspot.in/2013/09/jsp-implicit-object-jspsetproperty.html

jsp:useBean

 <jsp:useBean>

One of the most important and most widely used jsp action is <jsp:useBean>

  • Lets see what is this jsp:useBen? 




  • Lets say that you have a web application and as soon as the user logs in the request post to the user servlet or the user servlet whatever, and we know that Bean is nothing but a set of getters and setter methods(getId(),setId(),getName(),setName(..)) which identifies the state of an object. Lets say that this is a user bean( picture of person) which has id and name stored in this bean  so I have getters and setters for user id and user name.
  • So whenever a user logs into a system, this servlet invokes a user Bean and it sets the values of the user id and user name to whatever the user has enter in login screen.
  • Now when the request is passed to a jsp page


  • Ultimately I want to pass this user name and id to this jsp page so that it can be displayed on the jsp page, How can I do it? How this jsp page use this Bean values ? which is set at the servlet
  • We know that we have different scopes : request, sessions, page, applicaiton . We can set attributes values to that scope as well.
  • So one thing I can do is, "request.setattribute( "id") whatever id,  and pass those request attributes  whatever r1, r2 to the jsp page and display the those values using "request.get attributes" 
  • So it is a small applicaiton, just have to get two values from my jsp pages , i can use request attribute do something but later I have 10 values, to be passed to jsp page from servlet to jsp, am I use ten request attributes statements? to get 10 different attributes? No..right. So what Im going to do is Im going to create set all the values in the Bean(in setters methods) and Im going to pass that Bean to the jsp page.
  • So Im going to say, request.setAttribute("userBean", userBean) and set this bean value in my user bean attribute, and Im going to pass that bean to my jsp page
  • I can now fetch values of the id and name from the user bean in my jsp page.
  • In my servlet I can say request.setAttribute("userBean", userBean)
  • on the jsp page, how do I display that user Bean , how can I the value of id and name, Here I can use the <jsp:useBean../> action to get the values of the user bean object

  • Ok, lets see the jsp:useBean action, I just explained that when you have a bean in your servlet and you set some properties to that bean in your servlet and you want to save properties value, and you want to write on jsp page you can do that using the <jsp:useBean> action but it actually the jsp:useBean action not used to retrieve values of the bean class.
  • Ok, lets see the jsp:useBean action, I just explained that when you have a bean in your servlet and you set some properties to that bean in your servlet and you want to save properties value, and you want to write on jsp page you can do that using the <jsp:useBean> action but it actually the jsp:useBean action not used to retrieve values of the bean class.

Defn: 
Instantiate the bean class, use bean class properties , and set property values

Syntax:
<jsp:useBean id="{beanInstanceName}"
      scope="page/request/session/application"
      class="{package.class}"
     type="{package.class}"
     beanName=""{package.beanName}">
</jsp:useBean>

  • In fact we have jsp methods actions for that what purpus of the jsp useBean action is instantiate the bean class, we know that when your coding in java when you want to values of a particular object, first you need to create a reference of that object, Lets say i have to use bean class:

  • Here frist Im create ub reference variable for my userBean class and i can use the methods of userbean class, saying ub.getId(), or ub.getName()
  • In a similar manner thre has to be certain reference which you need to create in your jsp as well so if you want to use the userBean object with the values which had set, we need to have certain reference so that you can call methods that userBean and retrieve the values.
  • The following syntax creates the reference and instance 
UserBean ub=new UserBean();
<jsp:useBean id="{beanInstanceName}"
      scope="page/request/session/application"
      class="{package.class}"
     type="{package.class}"
     beanName=""{package.beanName}">
</jsp:useBean>
  • So in the previous example we said with attribute with name userBean (ex: request.setAttribute("userBean", userBean)) and we set the userbean object to that, so whatever attribute values we have here, we need to provide the same name in id for the user bean action element (ie. useBean id="userBean")
<jsp:useBean id="userBean"
      scope="request"
      class="ub.UserBean"
     type="{package.class}"
     beanName=""{package.beanName}">
</jsp:useBean>


  • In this example(request.setAttribute("userBean", userBean) ) we set the useBean attribute at the request scope so scope would be "request" and id would be "userBean" and class is nothing but a object class for example if "bean" is a object of UserBean, then class will become "UserBean" class, and what does type mean nothing but reference type , so you can have different type of class , you can have a different class name in the class, and you can have different class name like type as well because you know that you can create instance of a sub class lets say employee is a sub class and Person is a super class because Employee is a Person , here Employee is a subclass and Person is a super class , basically what you can do is ? when you say employee emp, person emp, Person p=new Employee(); you can do like this right, here Employee is nothing but a class where as type is nothing but Person. (i.e: class="Employee" type="Person" )  so you can have a differen class and you can have different object type(ex: Person p=new Employee();). and you can have different reference type as well. So here class is nothing but object type and the type is nothing but reference type.

Next is beanName:


  • When you put this syntax in jsp:useBean syntax in jsp page quickly tries to find bean with user bean name( in this example user bean name is : "UserBean")  but again it had not found any attribute set this attribute name in the given "scope "  its simply creates a new bean that name for you.
  • Now we going to see how to instantiate a bean in jsp page but how do you really use the values of the properties of those bean in your jsp page we can do that using the <jsp:getProperty> action (http://improve-jsp.blogspot.in/2013/09/implicit-objects-jspgetproperty-action.html)

jsp:param

<jsp:param>:

Defn:
Pass parameters to the included/forwarded page.

Syntax:
<jsp: include page="{PAGE_TO_INCLUDE}" flush="true">
 <jsp param name="{parameterName}" value="{paramValue}"/>
 </jsp include>




  • We have jsp include and jsp forward and even jsp include , we said that we can pass parameters to jsp include but we could pass parameters in jsp directive so how do you pass parameters to these two elements jsp include and forward.
  • Using the jsp param action element is used to pass parameters to  included or forwarded page.
  • Lets say that i want to include a page but the contents of the page is again the depends on certain values comes from the main page so lets say that in my included jsp page, in my footer page i want to display things like page 1 of 4 or page 2 of page . So I pass that value to the number of page using the jsp param so I pass page number in the parameterName and I pass  a value =1 or 2 or 3 in paramValue:  so display i can have certain kind of dynamic content based on the page which is actually included in my sub page in my included jsp page so it is not really completely static nature but it can also have dependancy  is on my main page  which is actually included this page.


Syntax 2:
<jsp:forward page="{PAGE_TO_FORWARD}" flush="true">
<jsp: param name="{parameterName}" value="{paramValue}"/>
</jsp:forward>

Saturday 21 September 2013

jsp:forward action tags

<jsp:forward> action

Defn :
forwards the request to the given page.

Syntax: <jsp:forward page="{PAGE_TO_FORWARD}"/>

  • And it is again similar to jsp include, it forwards the request to the given page.
  • When you say jsp include, your included jsp pages contents put your main jsp page and your main jsp page is displayed but here it does not include any contents of the included page but it simply forwards to the given page so that means that whatever contents you had your main jsp page would not be forwarded to the sub page so lets say that you had something wretten on your main jsp page  and ultimately the page forwarded so whatever content had your main jsp page will not be passed over to the forward jsp page.
  • I think you rarely use this "jsp:forward "action in your application.

Difference between include directive and include action

Difference between include directive and include action

----------------------------------------------------------------------------------------------------------
include directive include action
------------------------------------------------------------------------------------------------------------
1. Translation Time Run time
2. Copies the included file References to the included file
3. For static content For dynamic content
4. can not pass parameters can pass parameters
--------------------------------------------------------------------------------------------------------

1. Translation Time:
 Basically the page included means included your main jsp page at translation time, that means whenever your jsp translated to a servlet at that point of time even the included jsp pages content is got into the main jsp page and then it is tanslated to a servlet.

1. Run time:
It is not done at translation time so even though your servlet, eventhough your jsp translates into servlet it does not really content the included jsp content in the main page but it gets the content at run time when you execute that page

2. copies the included file:
Because it done at translation time it is simply copies all the content from your included page to main page

2. References to the included file:
Since we did not do at translation time simply keeps the included page at its own path and it has some kind of link or reference in the main page where it points to this included page and it craps this content of the included page  at that run time and then populates.

3. static content:

including the translation time itself, this is more likely to use  for static content,  because like for example that your sub page has changed but your main has not changed  and if you go and try to give this page  again  you wont see the latest change made in your sub page because the static content included page content was included in your main page at translation time and translation happens only once and it happens at first time and it happens whenever you changes your main page. Since you didnt make any chages in your main page, no translations happens again that means same static content present in your main page.

3. For dynamic content:

It gets the contents of the included page at run time, you can use it for dynamic content, lets say that your included page is being modified frequently, it used in lots of pages but again it is being modified frequently in that case you go for dynamic content because you dont have to really compile and change your main page so that your included page is , new content page can be reflected.

jsp:include

defn:
    Includes a page at the given location in the main page.
Syntax:
 <jsp:include page="{PAGE_TO_INCLUDE}" flush="true"/>

It is as simple as name suggests, simple include a give jsp or html page in your main jsp page at the given location in the main page.
For example, I have my main jsp page:
i have a code in my jsp page like below:
{
--//code-
---
//here im including another jsp page for my jsp page
<jsp:include page="{PAGE_TO_INCLUDE}" flush="true"/>
----
//code
....
}
Here i have a code, in between code in my main jsp page, Im including jsp code(another jsp page) using the jsp include action
So here what Im simply doing is , here im including the content of another jsp page in my main jsp page at the given location
So you would have heard of similar concept earlier in this class, it is during "jsp directive", i explained in "jsp directive" that we could include a page in my main page and where you would usually use this probably you can say you can have certain common features in every jsp page like a header or footer you can use those kind of things to include in your main jsp page. So every page a will have a same header or footer.

Difference between include directive and include action

----------------------------------------------------------------------------------------------------------
include directive include action
------------------------------------------------------------------------------------------------------------
1. Translation Time Run time
2. Copies the included file References to the included file
3. For static content For dynamic content
4. can not pass parameters can pass parameters
--------------------------------------------------------------------------------------------------------

1. Translation Time:
 Basically the page included means included your main jsp page at translation time, that means whenever your jsp translated to a servlet at that point of time even the included jsp pages content is got into the main jsp page and then it is tanslated to a servlet.

1. Run time:
It is not done at translation time so even though your servlet, eventhough your jsp translates into servlet it does not really content the included jsp content in the main page but it gets the content at run time when you execute that page

2. copies the included file:
Because it done at translation time it is simply copies all the content from your included page to main page

2. References to the included file:
Since we did not do at translation time simply keeps the included page at its own path and it has some kind of link or reference in the main page where it points to this included page and it craps this content of the included page  at that run time and then populates.

3. static content:

including the translation time itself, this is more likely to use  for static content,  because like for example that your sub page has changed but your main has not changed  and if you go and try to give this page  again  you wont see the latest change made in your sub page because the static content included page content was included in your main page at translation time and translation happens only once and it happens at first time and it happens whenever you changes your main page. Since you didnt make any chages in your main page, no translations happens again that means same static content present in your main page.

3. For dynamic content:



It gets the contents of the included page at run time, you can use it for dynamic content, lets say that your included page is being modified frequently, it used in lots of pages but again it is being modified frequently in that case you go for dynamic content because you dont have to really compile and change your main page so that your included page is , new content page can be reflected.

Thursday 19 September 2013

JSP actions

Action tags are set of tags which allows us to include some basic actions such as inserting some other page resource, forwarding the request to another page, creating or locating the Java Bean instances, and setting and retrieving bean properties, in JSP pages.
The action tags are specific to JSP page. When the JSP container encounters an action tag while converting a JSP page into a servlet, it generates the java code that corresponds to the required predefined task. For example when it comes across the ‘include’ action tag:
<jsp:include page=”myjsp.jsp” flush=”true”/>

The above jsp code is “the jsp:include action tag allows to include the myjsp.jsp page.

The action tags are:

1. <jsp:include> http://improve-jsp.blogspot.in/2013/09/blog-post.html
      Difference between include directive and include action
    (http://improve-jsp.blogspot.in/2013/09/difference-between-include-directive.html)
 2. <jsp: forward> action: http://improve-jsp.blogspot.in/2013/09/jspforward-action-tags.html
  3. <jsp:param> action: http://improve-jsp.blogspot.in/2013/09/jsp-implicit-objects_23.html
 4. <jsp:useBean> action: http://improve-jsp.blogspot.in/2013/09/jsp-implicit-objects_6038.html
Example program: Develop a web application for registration form using Beans (java beans) in a JSP
5. <jsp:getProperty action http://improve-jsp.blogspot.in/2013/09/implicit-objects-jspgetproperty-action.html
6. <jsp:setProperty> http://improve-jsp.blogspot.in/2013/09/jsp-implicit-object-jspsetproperty.html

implicit object: respone




  • 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

Difference between sendRedirect() and requestDispatcher()

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

implicit objects: request

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. 

  • 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

implicit objects: 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

pageContext object

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? 


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 .

Implicit objects: config and application

Implicit objects: config and application

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 .

Implicit objects: session

Implicit objects: 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
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 persistant 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.
  • Now Im going to forward that request to jsp, where again im retrieving it using the  session implicit object and display on that page.

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? 

    taglib directive

    (3.) taglib directive

    This is the last and most important directive
    Basially we provide certain tags in your jsp pages and implementing the taglib directive is going to help a lot because it is going to clean up your jsp page, it is going to look more pretty it is going to get a lot of loat of your shoulder as well.
    So lot of common custom logic which you have in your application can be written in a taglib directive and you simply use the taglib directive in your jsp pages instead of again writing all those logic repeatedly in each of the page .

    syntax: <%@ taglib uri="{TLD_FILE}" prefix="{PREFIX}"%>

    It starts angular brackets percentage atTheRate and followed by taglib and we have uri and prefix, we will see what is really mean?
    Components of taglib Libraries:

    1. Tag handler class (2) Descriptor configuration file (3) taglib directive

    1. Tag handler class
    Now let's say that we have a common functionality used in each and every page in my application so what I do this, I put this common chunk of code in certain file and I will use this common code in multiple jsp pages.
    So that common place where I'm puttin the common logic is the Tag Handler class. This is the place where I put the common custom logic and html generation.
    And I use the taglib directive in my jsp page to invoke the class which is implementing the common logic now they have to be some bridge between the taglib directive and my jsp file and actual tag handler class so for that reason I have the Descriptor configuration file.
    Descriptor configuration file is nothing but the property mapping file which has got information about the taglib directive and the tag handler class and what are the different property you can set for the tag libraries

    overview of taglib directive:
    jsp4.jpg
    //test.jsp
    <%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
    <sharmanj:txtbox length="10" name="userName"/>
    ....

    //sharmanj-taglib.tld
    <tag>
    <name>txtBox</name>
    <tagclass>TextBoxTag</tagclass>
    <bodycontent>EMPTY</bodycontent>
    <attribute>
    <name>name</name>
    <required>true</required>
    </attribute>
    ....
    </tag>


    //web.xml
    <web-app>
    <taglib>
    <taglib-uri>sharmanTags<taglib-uri>
    <taglib-location>
    sharmanj-taglib.tld
    </taglib-location>
    </taglib>
    </web-app>

    /*TxtBoxTag.java*/
    public class TxtBoxTag extends TagSupport
    {
       public int doStartTag()
       {
         ----
       }
     }

    See here an example i will show all the different pages  classes and files involved in a tag library and how each file is connected to the other file

    //test.jsp
    <%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
    ......
    <sharmanj:txtbox length="10" name="userName"/>
    ....

    So first thing is my test.jsp, here What I did is? I have a common logic for displaying the text box so in my application whenever someone wants to use a text box in a jsp page they should not use the predefined text box present in html whatever input. But they have to use the jsp tag called sharmanj:txtbox to add a text box in their html or jsp file.
    Lets say that I have provided some logic for that and that logic is present in the TxtBoxTag.java
    We will have a look at the whole life cycle of tag handlers and all those things, what are the different tag handlers we can have? and whole life cycle of tag handlers and the next future class we will discuss tag libraries more details but here I just wanted to show you the different components we have placed tag libraries and how communication takes place?  between these components.
    So my custom logic for the text box representation goes in the TxtBoxTag which is nothing but the tag handler class ( we just discussed).

    /*TxtBoxTag.java*/
    public class TxtBoxTag extends TagSupport
    {
     public int doStartTag()
     {
      ----
     }
    }

    Lets say that I have that logic is placed in doStartTag() method
    Now in my jsp I need to use the logic present in the TxtBoxTag.java to represent my text box.

    //test.jsp
    <%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
    ......
    <sharmanj:txtbox length="10" name="userName"/>
    ....

    So here I say I get the taglib directive and I say:" uri=sharmanjTags and prefix=sharmanj. When I say uri? it means that path to the tag libraries which I will be using and this path will be present in the web.xml, and which is the configuration for your servlets and jsps
    web.xml is the configuration file for your servlet and jsps

    //web.xml
    <web-app>
    <taglib>
    <taglib-uri>sharmanTags<taglib-uri>
    <taglib-location>
    sharmanj-taglib.tld
    </taglib-location>
    </taglib>
    </web-app>
    So when I say taglib uri="sharmanj", What the container is going to do is? The Web Container finds the sharmanTags in the web.xml file under the <taglib> element
    So web container is going to find the taglib uri with the name "sharmanjTags", so it finds a match now.
    Once the web container finds the match in web.xml file
    Now web container is going to see the <taglib-location>, so taglib-location is "sharmanj-taglib.tld"
    Now web container goes to sharmanj-taglib.tld location which is a configuration file again where we provide certain different configurations for the tag libraries

    //sharmanj-taglib.tld
    <tag>
    <name>txtBox</name>
    <tagclass>TxtBoxTag</tagclass>
    <bodycontent>EMPTY</bodycontent>
    <attribute>
    <name>name</name>
    <required>true</required>
    </attribute>
    ....
    </tag>
    And within the sharmanj-taglibraries , I have different tag attributes I can use in this particular tablibraries i.e sharmanj-taglib.tld
    And before that let me complete the text.jsp

    //test.jsp
    <%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
    ......
    <sharmanj:txtbox length="10" name="userName"/>
    ....
    I said that Im using the sharmanjTags lib and prefix can be anything example: sharmanj
    So whatever I put prefix I should use that prefix certain feature in the taglib that is when I say prefix "sharmanj", Iam using prefix sharmanj on every element where I want to use the taglib. That is when I say sharmanj:txtbox, so here Iam trying to use  txtbox.
    We know that taglib directive match to this particular location (i.e sharmanj-taglib.tld)  now Im going to use the "txtbox" taglib. Now it is going to search "txtbox" in the <name> tag in the file sharmanj-taglib.tld
    Going to see what is the corresponding <tagclass> and it sees that tag class is "TxtBoxTag " and it is going to find that "TxtBoxTag" class , implement the basically call the methods in the TxtBoxTag.java

    /*TxtBoxTag.java*/
    public class TxtBoxTag extends TagSupport
    {
     public int doStartTag()
     {
      ----
     }
    }
    So I put all my custom logic difining the TxtBox in my page.
    So this is how the flow navigates in the different components and how your tag handler class is identified by the jsp pages .

    Now finished the jsp taglibs