Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Maha Annadurai

Ranch Hand
+ Follow
since Oct 27, 2002
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Maha Annadurai

one use case could be, you could have loaded a servlet using web.xml <load-on-startup> element. But no body cared to invoke it at all.
Maha Anna
After my above post, I looked into the Jsp Spec carefully and found that the JSP spec clearly says that as a first step, an attempt is made to locate an object with the given 'id' in the given scope. In this first step the container should not care about the class or type.
Here is the spec info:
Section Jsp 4.1
The actions performed in a jsp:useBean action are:
1. An attempt to locate an object based on the attribute values id and scope . The
inspection is done synchronized per scope namespace to avoid non-deterministic
1. A scripting language variable of the specified type (if given) or class (if type
is not given) is defined with the given id in the current lexical scope of the
scripting language.
2. If the object is found, the variable�s value is initialized with a reference to the located object, cast to the specified type . If the cast fails, a java.lang.ClassCastException
shall occur
. This completes the processing of this jsp:useBean action.
3. If the jsp:useBean element had a non-empty body it is ignored. This completes
the processing of this jsp:useBean action.
4. If the object is not found in the specified scope and neither class nor beanName
are given, a java.lang.InstantiationException shall occur. This completes the
processing of this jsp:useBean action.

So Tomcat is implementing the 'find logic' according to spec.
Maha Anna
[ December 16, 2002: Message edited by: Maha Annadurai ]
It is an error and listed in this errta link
Page 190, Third paragraph:
The text in bold is missing. The pragraph should read as follows:
Because the JSP page is converted into a servlet, we can call all the methods in a JSP page that we can call on a servlet. Hence we can get the ServletContext object via getServletConfig().getServletContext(). However, in tomcat and in many other containers, the base class of the page's generated class also implements the ServletConfig interface. Thus, in both methods, jspInit() and jspDestroy(), we get the ServletContext object by using the method getServletContext(), which is actually defined in the javax.servlet.ServletConfig interface.

This means, just writing getServletContext() inside a jsp file is not a portable way!. Just beacase Tomcat's jsp implementation base class so happened to be a HttpServlet (which implemnets HttpJspPage), this code works. Othewise it will break.
Maha Anna
Jsp 1.2 spec does clearly say that the values are case dependent. Here is the info.
JSP.7.4 The Tag Library Descriptor Format
tagdependent The body of the action is passed verbatim to be interpreted by the
tag handler itself, and is most likely in a different �language�, e.g.
embedded SQL statements. The body of the action may be empty.
No quoting is performed.
JSP The body of the action contains elements using the JSP syntax.
The body of the action may be empty.
empty The body must be empty
The default value is �JSP�.
The syntax is:
<!ELEMENT body-content (#PCDATA) >
#PCDATA ::= tagdependent | JSP | empty.
Values are case dependent.

So if some containers are not so strict, that means they are not strictly following the spec.
I think we should follow the spec for the exam.
Maha Anna
We can not have 2 vars with the same name in the same visiblity scope. In the above code, we are not declaring 2 vars with same name as "temp". There is ONLY ONE varablie inside _jspService(HttpServletRequest,HttpServletResponse) method.
The String var can take ANY value. In java we do not restrict a String var taking the same value as its name. Right? In the above code it's just so happened a String var named 'temp' takes a value of 'temp'.
The following code will not work since it tries to declare a 2nd var named as 'temp' in the same _jspService(req,res) method. Please take a look at the generated java file for the above jsp to have better understanding.

This code too will not work.

Maha Anna
[ December 16, 2002: Message edited by: Maha Annadurai ]
I understand your doubt. The important point here is , "how the lookup coding is done?". As you said, the generated servlet looks for an object with the same name as of 'id' attribute in the given scope with given class. How it is really coded is the key here.
Tomcat does its coding something like below.

The SCWCD book also assumes the coding will be done like above in order to find the object. I guess we can use object.instanceOf(aType)
way also to make the decision. In that case, we will not get any ClassCastException at all. Something like below:

Is this what your doubt Rasika?
Any comments on this? Others ?
Maha Anna
[ December 16, 2002: Message edited by: Maha Annadurai ]
Hi Aleks,
Here is an example I played with.

Output at Browser:
1111 Hello 222 Hello 333 Hello
Please post your code in both files for us to further discuss. Thanks.
Maha Anna
If there is any new lines before and after your code , the converted class tries to write to JspWriter. Like out.write("\r\n"); According to spec, if the output is buffered , it must be cleared before forwarding. So just writing few lines to JspWriter is not a problem since it will be cleared. But make sure you do not have any newlines after your forward call .
Also note that, the default value for "autoFlush" is true for <%@ page directive, which means if the written content is greater than the buffer size, then the output will be automatically commited. Also once a request is forwarded to another resource, it is said to be committed, so we should not try to write to JspWriter after that.
So please try the following
1. Make sure there is no space / new lines after the forward call.
2. Try not to write anything before the forward call.
3. you can just write out.clear() to make sure the buffer is cleared , before the forward call.

Maha Anna
[ December 15, 2002: Message edited by: Maha Annadurai ]
No. Space is not same as empty for tags.
Maha Anna
If there is a variable named as output defined before this (either as a scriptlet var or as a declaration var) , this code will work fine. Meaning the value of a varibale 'output' will be written to JspWriter.
For example the following jsp will work fine.

If there is no variable defined before, then we will get a compilation error.
If we just want to get this String "output" itself to browser then we can code like this .
Maha Anna
[ December 13, 2002: Message edited by: Maha Annadurai ]
/servlet/servletName is the right standard method to invoke a servlet in any server. There is no 's' after 'servlet'.
It all started way back with JSDK. (Java Servlet Development Kit- the very first sdk released for servlet development from Sun) It is unfortunate that the current Servlet spec does not say anything about this concept.
Here is a link to the old reference documentation I got hold of which explains the standard way to invoke a servlet.
Maha Anna
[ December 12, 2002: Message edited by: Maha Annadurai ]
1. We need not declare all servlets of a web application under web.xml. We can always access it through the standard way.
http://server ort/servlet_context_name/servlet/FullyQualifiedServletClassName
Example :
1. http://localhost:8080/myApp/servlet/HelloServlet
2.Regarding your page 72 question, I assume the web application is mapped to default application. There are container specific ways to map a web app to default app. So in this case the servletContext name is just ""
If you have Tomcat installed check on conf\server.xml for these lines which does this kind of mapping.
<!-- Tomcat Root Context -->
<Context path="" docBase="ROOT" debug="0"/>

3. Reg. page 77 question, /blue/ does not have a * after the slash , meaning we have to have the exact uri mapping of /blue/. On the other hand /red/* means either /red or /red/ or /red/test will be mapped right.
4. The uri attribute <%@ taglib .... /> directive can in fact have absolute / root relative / non-root relative URIs.
But the important point is if this attribute is assigned with an absolute URI then it MUST have a mapping in web.xml like below.
<taglib-uri> absoluteURI here </taglib-uri>
<taglib-location> some val </taglib-location>
If there is no mapping found, then only we will get an error.
Please read that section more closely. The section is written as an if ..... else if .... way/ Notice the tabbed format of the bulleted points.
Maha Anna
It's kind of tricky implementation I guess. In Java we can declare a class as abstract for 2 reasons.
1. If the class has any unimplemented abstract
2. If you do not want anyone to instantiate your class and use it. The reason behind this could be anything. In our case, the reason is we must extend the HttpServlet class and then use it. Because the mostly used doGet(..) doPost(..) methods in HttpServlet class have dummy implementation which is useless and the authors want us to extend the class and take care of it.
If you see the source code of HttpServlet class you can see the default implementation is to call sendError() with a message of "Method Not implemented".
Instead of doing this way, the author could have very well made doGet,doPost,doDelete,doHead methods as abstract. But if they had done this way, then we MUST implement all these methods in our servlet eventhough all we care about is either doGet/doPost. But now all we do is override doGet / doPost or both only.
Probably this could be the reason which convinced the author to give the dummy implementations for the do** methods in HttpServlet.
Just a thought
Maha Anna
Yes. The container generated servlet of a jsp page is HttpServlet.
Here is the API documentation of Tomcat's implementation.
Maha Anna
I am using Tomcat 4.0.3 and able to access a simple class which is NOT in a package without any problem.
Here is a sample url I tested.