Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

!!My JSP Doubts!!

 
Srinivasan Rengan
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) My First question is, why has the scope of 'page' being implemented in JSP?

2) My second question goes in this way. Assume that Shape is a super class and Circle is a sub-class. A situation : Circle c = new Shape(), requires a downcasting, else this will show up a compile time error. Now, in JSP, when I write the following:
<jsp:useBean id="something" class="Shape" type="Circle" scope="something"</jsp:useBean>;

The generated code performs an automatic downcasting. Thus I am able to access all the attributes of the class Circle also. Why should there be a automatic downcasting performed?

This situation is similar to <jsp:useBean> id="something" class="Circle" scope="something"</jsp:useBean>. How do you share this concern?

3) Thirdly, HttpServlet class implements ServletConfig interface. Now, the HttpServlet class also has a init() method to which the container passes the ServletConfig object. In this case what is the need of the former?



Thanks in Advance!!
Srini
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) The "page" scope is some kind of instance variable for the current jsp. Only current page (and statically included resources) will have an access to this scope, so you can easily include and forward dynamic resources from your page, which can use the same names for attributes and variables as your page.
So, it's definetely very useful scope.
2) jsp:useBean's class "attribute" must always be a subclass (or the same class) of type "attribute". Your example is wrong and must not compile.
3) The HttpServlet class implements ServletConfig for easing the access to ServletConfig object. But you can write your own servlets without extending HttpServlet. HttpServlet is a concrete implementation of Servlet interface, which has init method. So, there is no redundancy, only facilitation.
 
Srinivasan Rengan
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sergey Tyulkin...




But, I still see my second question's outcome confusing..
I wrote the same jsp tag and it got compiled and it worked fine as any other code...
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The quote from specification:
At least one of type and class must be present, and it is not valid to provide
both class and beanName. If type and class are present, class must be assignable to type (in the Java platform sense). For it not to be assignable is a translationtime error.

Your Shape class is not assignable to Circle class (as Circle is a subclass of Shape). Then, you must get translation-time error. If you don't, then you use incomplete implementation of JSP specification. It occurs everywhere, but when you will have the exam, you must taking into account only specification.
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is BeanName the same as Class?

Thanks!
Jenny
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, they are different. I quote the whole phrase. This is a part of phrase, that relates to Srinivasan's question: "If type and class are present, class must be assignable to type (in the Java platform sense). For it not to be assignable is a translationtime error."

beanName can be a name of the class to instantiate , or a name of serialized JavaBean (a file with .ser extension).
Again, quote from the JSP specification:
The attribute beanName specifies the name of a Bean, as specified in the
JavaBeans specification. It is used as an argument to the instantiate method in the java.beans.Beans class. It must be of the form a.b.c, which may be either a class, or the name of a resource of the form a/b/c.ser that will be resolved in the current ClassLoader.
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sergey Tyulkin,

Thanks for your answer!

-Jenny
 
Srinivasan Rengan
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Sergey Tyulkin
you say that if i am going to give the jsp tag in this way
<jsp:useBean scope="request" class="Circle" type="Shape" id="someattribute">


</jsp:useBean>
This would not work without any problem....Is that so?
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah,
this one (from the first post)
<jsp:useBean id="something" class="Shape" type="Circle" scope="something"/>
will result in translation error

and this one (from the last post)
<jsp:useBean id="something" class="Circle" type="Shape" scope="request"/>
will work without problems
 
Srinivasan Rengan
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exactly...
sorry about my first query...i did not mean that at all... Now, my question is, the tag which you have written would yield an object creation something as below...
Shape s = new Circle();

Now, with this object s you would be able to access methods in class Shape, but not in class Circle, unless you perform a downcasting. Why is the generated code performing an automatic downcasting?

This is my intended question???
 
rahul karwal
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not downcasting its upcasting
Shape is the super class
Circle is sub class
Shape s = new Circle(); -- its upcasting.
but if u do this: -
Circle c =(Circle)s;
then this is downcasting and JSP is not doing this for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic