About Question enthuware.jwpv6.2.1209 :

Moderator: admin

Post Reply
vs2013
Posts: 9
Joined: Wed Jul 31, 2013 1:57 pm
Contact:

About Question enthuware.jwpv6.2.1209 :

Post by vs2013 »

HI,

Aren't we able to override _jspService() when writing a scriptlet? A scriptlet code is placed in _jspService() is it not?

<% out.println("hello"); %>

Translated into:

_jspService()
{
out.println("hello");
}

Then we do override _jspService() or no?

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1209 :

Post by admin »

No, <% out.println("hello"); %> is not translated into:

_jspService()
{
out.println("hello");
}


<% out.println("hello"); %> is translated into: out.println("hello"); and is put inside _jspService(), which already exists.

So a scriptlet does not override _jspService.

HTH,
Paul.

vs2013
Posts: 9
Joined: Wed Jul 31, 2013 1:57 pm
Contact:

Re: About Question enthuware.jwpv6.2.1209 :

Post by vs2013 »

Hi,

Sorry for confusion. I gave entire _jspService() function example as though it's also created. No, I meant only out.println("hello") is placed into _jspService(), just as you indicate.

Having said that, arent' we still overriding _jspService() by virtue of modifying its contents when placing out.println("hello") into it? This was my line of reasoning when I was answering this question.

Thanks.

ADDED: Here I mean "placing" into _jspService() through scriptlet code.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1209 :

Post by admin »

I am not sure what you mean by "we". The servlet class that is created by parsing your jsp is created by the container. It creates _jspService method. There is only one class i.e. the servlet class created by the container that has the declaration as well as definition of the _jspService method. So where do think are you overriding this method?

Overriding has a specific meaning and that is not happening here. It is possible that the container has a base class for the created servlet class with the declaration of the method but that is a fact that we don't know.

HTH,
Paul.

vs2013
Posts: 9
Joined: Wed Jul 31, 2013 1:57 pm
Contact:

Re: About Question enthuware.jwpv6.2.1209 :

Post by vs2013 »

Hi. I just would like to understand this better - I look at one of translated JSPs I created, index_jsp.java it extends org.apache.jasper.runtime.HttpJspBase

I then look at the API docs for HttpJspBase:
http://tomcat.apache.org/tomcat-4.1-doc ... pBase.html

It has _jspService() method.

I understand this may be vendor-specific, but thinking on servlet analogy, when we create a servlet extending HttpServlet, we don't override service() in our servlet. If we override it but need to call super() then. In fact, even the base class Servlet has service() in it and we can override it if we create a servlet that extends Servlet class (but need to call super().

Same with JSPs? Whenever a JSP we create is translated into servlet code this JSP must extend a base class that must have default _jspService() otherwise it makes no sense to have translated _jspService() in JSP we create without this translated JSP to extend a class with _jspService() in it? I mean this is how Servlet technology works in general?

Thanks,
Victor.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1209 :

Post by admin »

1. In case of extending HttpServlet, you can very well override the service method and code it however you want. This will be the regular "overriding" of a method.

2. In case of a JSP page, as per section 11.1.1:
JSP.11.1.1.1 Protocol Seen by the JSP Page Author
The JSP specification defines the contract between the JSP container and the JSP page author. This contract defines the assumptions an author can make for the actions described in the JSP page. The main portion of this contract is the _jspService method that is generated automatically by the JSP container from the JSP page. The details of this contract
are provided in Chapter JSP.9, “Scripting”.
So basically, what it is saying is that the there will be class created by the container, which will have _jspService method. This class may or may not necessarily be overriding _jspService method of its base class. It is certainly possible that there is no base class, in which case there is no overriding happening.

Now, even if there is a base class, the class created by the container overrides that base class's _jspService method. Does that mean "you" i.e. the author of the page overrides the _jspService class? IMHO, no because you don't create the method, the container does based on your code.

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests