Wednesday 20 November 2013

Syllabus

Overview Of Jsp

JSP elements overview

  types of elements
   1. jsp declarations (http://improve-jsp.blogspot.in/2013/09/jsp-declarations.html)
   2.jsp scriptlets (http://improve-jsp.blogspot.in/2013/09/jsp-scriptlets.html
  3. jsp expressions (http://improve-jsp.blogspot.in/2013/09/jsp-expressions.html)
  4. jsp directives http://improve-jsp.blogspot.in/2013/09/jsp-directives.html
     (i) page directive http://improve-jsp.blogspot.in/2013/09/directive-page-directive.html
     (ii) include directive http://improve-jsp.blogspot.in/2013/09/2-include-directive.html
     (iii)taglib directive http://improve-jsp.blogspot.in/2013/09/taglib-directive.html
3. Where does the JSP code is placed in the Servlet?(http://improve-jsp.blogspot.in/2013/09/jsp-elements.html)

4 What is JSP? (http://improve-jsp.blogspot.in/2013/09/what-is-jsp.html).

5. JSP implicit objects(http://improve-jsp.blogspot.in/2013/09/jsp-implicit-objects.html)

   1. out http://improve-jsp.blogspot.in/2013/09/implicit-objects-out.html
   2. requesthttp://improve-jsp.blogspot.in/2013/09/implicit-objects-request.html
   3. response http://improve-jsp.blogspot.in/2013/09/implicit-object-respone.html
Difference between sendRedirect() and requestDispatcher() 
http://improve-jsp.blogspot.in/2013/09/difference-between-sendredirect-and.html
   4. session (http://improve-jsp.blogspot.in/2013/09/implicit-objects-session.html)
   5. config and application objects(http://improve-jsp.blogspot.in/2013/09/implicit-objects-config-and-application.html)
  6. page http://improve-jsp.blogspot.in/2013/09/implicit-objects-page.html
  7. pageContext http://improve-jsp.blogspot.in/2013/09/pagecontext-object.html

What is java beans? and its advantages

JSP action http://improve-jsp.blogspot.in/2013/09/jsp-actions.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
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

Error Handling in Jsp http://improve-jsp.blogspot.in/2013/09/error-handling-in-jsp.html
  1. Error handling in jsp

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.