Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another q from mock

 
Raj Kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Identify true statements about the JSP code below?
<%log("Test message");%>
A Code will not compile
B Test message is printed to the log file
C Log cannot be used in scriptlets
D NullpointerException occurs
Answer given:
Test message is printed to the log file. B is true.
Is this correct?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.
as far i know "log" is also an implicit object v can access like session,application,page etc...
so this seem correct to me.
regards
maulin
 
Raj Kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But from sun's site:
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro7.html
log is not mentioned as an implicit variable.
 
Ian B Anderson
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I've tried this and it works ok.
Ian
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Acc to the Specs (final version) "log" is not an implicit object.
Ian:
What server and version are you running with?
Thanks.
- satya
 
Raj Kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I am using weblogic 5.1, and I get this error:
C:\weblogic\raj\classfiles\jsp_servlet\_test.java:74: cannot resolve symbol
probably occurred due to an error in /test.jsp line 1:
<%log("Test message");%>
I just have this one line of code in test.jsp
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats good.
IMO. Tomcat screws up this one.
I am trying to follow the generated
java source to figure out whats foing on.
So far I have no idea even how this is
getting compiled. So basically, I am
thinking of implementing my own Servlet code and verify. I am confident that the servlet code will fail.
My gut feeling is that Tomcat 4.0.1 does something special in the super class HttpJspBase
re this method (well it has to):

to setup the log method.
Strangely enough, the API doesn't specify any Exceptions also for this method.

- satya
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
sorry but 'log' is not an implicit object. i got wrong footprint in my memory about it. i read somewhere about this log thing that it automatically logs the data we pass but i mixed it upwith the implicit object....
i'm trying to remember where did i read sth about log....
regards
maulin.
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys -
OK, on TC 4.01 it does indeed work. Here's how:
- Examine the servlet code generated from the JSP. The class name is xxx$jsp. This is the class where the _jspService method is actually implemented.
- The xxx$jsp class extends HttpJspBase (which is really org.apache.jasper.runtime.HttpJspBase).
- Examine the apache source code for HttpJspBase. It extends HttpServlet and implements HttpJspPage (which is what specifies the _jspService method).
- In turn, HttpServlet extends GenericServlet and implements Servlet and ServletConfig.
- The ServletConfig interface specifies the log method.
If I have not chased that down correctly, please let me know.
My question is: Does this Tomcat implementation meet all the requirements of the specs? I have not really looked at the portions of the specs which indicate what a container provider must and can/cannot do.
Regards, Guy
P.S. - the page works exactly the same if you code:
<% application.log("A Log Message"); %>
whick seems more natural to me. It had never occurred to me that one could call ServletContext specified methods directly, without using 'application.'.
Let me know what you guys think.
 
Marlon Bravo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm not sure if I'm saying this right so just correct me if I'm wrong. I understand that a .jsp file is translated and compiled into a servlet. The code found in scriptlets are actually placed within the service method of the servlet. Since a Servlet implements ServletConfig, the log method must be implemented and becomes visible throughout the servlet, making the log method available within the service method. That's why it's valid to use <%log("A Log Message");%>
-Marlon
[ February 12, 2002: Message edited by: marlon bravo ]
[ February 12, 2002: Message edited by: marlon bravo ]
 
Raj Kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually log method is part of ServletContext interface, not ServletConfig. application is the JSP implicit variable for ServletContext. So I think, as Guy says, it should be
<%application.log("Message");%>
not
<%log("Message");%>
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj - You are right it is ServletContext, not ServletConfig. Which of course is not implemented by HttpServlet.
That being the case, my question becomes even more important and remains: Is this proper behavior by Tomcat?
I am uncomfortable with you running Weblogic 5.1, because 6.0, 6.1, and 7.0 are all out are they not? And now I am uncomfortable with me running TC 4.01.
Regards, Guy
 
Raj Kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, GenericServlet also has a log method which takes a string as a parameter. But this log method prepends the name of the servlet to the message. Did u get the servlet name in ur log?
- Raj
 
Raj Kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if weblogic 7.1 is out. Btw I heard that 6.1 is not stable as yet. I think BEAS has made lots of changes when they released 6.0. Has anyone worked with weblogic 6.0 or higher?
- Raj
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj - Good catch on GenericServlet implementing a log method. I think that is where it comes from, however my log file does
not
show the name of the servlet, only timestamp and "jsp:" and the message. I see in the API docs that the servlet name should be there, but it is not in the output.
I ran a couple of other experiments:
<% this.log("A message"); %>
works, and I expected it to.
<% page.log("A message"); %>
fails and this was a surprise. The failure is a compile time 'method not found' in the page object. I thought according to specs that 'page' was equivalent to 'this'???
I will try and find where the GenericServlet abstract class is in the TC sources.
Regards, Guy
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And actually GenericServlet implements ServletConfig. The log method stems from that.
But why does 'page.log' not compile? That really seems like a bug to me.
Guy
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marlon bravo:
[QB]Hi,
I'm not sure if I'm saying this right so just correct me if I'm wrong. I understand that a .jsp file is translated and compiled into a servlet. The code found in scriptlets are actually placed within the service method of the servlet. Since a Servlet implements ServletConfig, the log method must be implemented and becomes visible throughout the servlet, making the log method available within the service method. That's why it's valid to use <%log("A Log Message");%>
/QB]

Yes, I am too busy looking at trees and not seeing the forest.
Thanks, Guy
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletConfig Methods.
The log(...) method DOESNOT come from ServletConfig. PLEASE.............It comes from ServletContext Methods.
PLEASE CORRECT YOUR RESP. POINTERS.
THANKS.
- satya
 
Ian B Anderson
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya,
Yes I too was using Tomcat 4. Doh!!!
I should have thought about why it was working a bit more!
Ian
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guy Allard:
- In turn, HttpServlet extends GenericServlet and implements Servlet and ServletConfig.
- The ServletConfig interface specifies the log method.
If I have not chased that down correctly, please let me know.

Close what you missed is GenericServlet defines the need for log(String msg) and log(String msg, Throwable t) the suggested form is to simply pass this to the ServletContext methods for convience.
Since it wasn't specified that we extend a different class, the requirements state that the container's implementation must implement the HttpJspPage interface which extends HttpServlet....
If we had extended JspPage with a class and extended that with our Jsp Page, the contract could be lost though there would be a lot of work on the developers point.
So, without extending our own class for the Jsp Page, all containers would allow the use of the log method.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so the log(...) method is also defined in the GenericServlet class.
Having known this fact, what happens when I haven't configured any log file?
Ofcourse, from my experience, nothing happens.
Maybe the Exception if any is caught.
How do I setup the log file stuff in Tomcat?
Anyone did this. please. I haven't looked deep into this....
Thanks.
- satya
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Log files are configured in Server.xml. If you don't have a log file configured, then the output should be directed to the Console window the same as System.out Tomcat does however, I'm not positive other Servers handle this the same way.
 
Manjunath Subramanian
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote from Guy..
"But why does 'page.log' not compile? That really seems like a bug to me."
page is of type Object.Because there is no method
called log(String str) or log(String str,Throwable t) declared in the Object class, calling page.log(str) fails..

Hope this helps,
Manjunath
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
page is of type Object.Because there is no method
YUP.
With a slight modification code it works though.........

Note the runtime object for "page" is the Servlet Class. But page.log(...) will cause a Compile time error for the reason mentioned above.
And, BTW I couldn't find a file in the $CATALINA_HOME/logs dir which had the text "Junk Test". Still wondering where that string is written...
- satya
 
faiza haris
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem of locating the text of the log. Maybe it has something to do with the log level.....
In my doGet method i am using log("in the doGet"); and i get the following in the log file
2002-02-12 16:24:12 cgi: init: loglevel set to 6
I dont get the log message printed in the log file...rather it gives the level??
and i'm looking in the logs\localhost_log.todays date
faiza
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to chaeck your server.xml file.
The basic configuration (If you keep the defaults) look something like.

This means that the file localhost_log.txt under the CATALINA_HOME/logs directory is what most webapps usw unless, you have a specific log file created for that webapp such as

So, if you look there, you should be able to determine where your log messages are going.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

directory="logs" prefix="localhost_log." suffix=".txt"

Thanks, Carl. I see them in the file.
(Not sure why, on NT, Explorer->Find...->Advanced
->Containing text: "Junk Test" can't find it.)
Oh well, its okay.
- satya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic