If you have a servlet problem and can't find a solution, then
send it to me at paul@newatlanta.com with a subject of "Ask the Expert" and let me take a crack at
it. When sending a question, please be brief and to the point
and include a code snippet if it will shed some light on your
problem. I'll send you a response and the response will appear
in the next "Ask the Experts" column.
Q: I tried to unload dynamically loaded java class from a regular
java server. It worked fine. ( I nuke that class assigning it
to null) and then run Systemgc() When I tried EXACTLY the same
thing in servlet - it did not work. I have to stop/restart JSM
default every time I recompile dynamically loaded class.
A: Most servlet engines automatically reload servlets which are
placed in the Servlets directory. If your servlets are in the
Servlets directory and the main servlet class changes then the
Servlet Engine should automatically reload the servlet. If you
continue to have problems then you should contact your Servlet
Engine vendor.
Q: I have the following lines in my servlet:
File profile = new File("profile.ini");
try { Input = new FileInputStream(profile);
. . } catch (FileNotFoundException e) { System.out.println( "can't
found the file" );
} catch (IOException e) { System.out.println("I/O Error"); }
It always displays "can't found the file". But the profile.ini
file is in my current working directoy. I don't know if I can
load a file with its relative pathname.
A: The current working directory may be different from what you
expect. Try displaying the user.dir system property to determine
what is the current working directory.
Q: I have a servlet that uses Cookies, when I call the servlet
from an HTML page, it works fine, it gets all the cookies and
can also create cookies, but calling the SAME servlet from a java
Applet, the servlet never receives any cookies and does not seem
to create any cookies either. the Applet is using the standard
javax.servlet.http.* classes to communicate with the servlet.
Is there a limitation on passing of cookies to the servlet when
it is called/accessed thru an Applet vs. a HTML page.
A: When you call a servlet from an HTML page it is the browser
which handles the sending of the cookies in the HTTP request header
and the reading and storing of the cookies in the HTTP response
header. When you call a servlet from an applet you need to handle
adding the cookies to the HTTP request header and the reading
and storing of the cookies in the HTTP response header.
Q: I would like to know whether a servlet can instruct the browser
to go to a specified url. For example, after the servlet successfully
processes data submitted by a user, I would like to display a
thank you page in the browser. This thank you page would be a
.html page at some specific location (http://www.abc.com/thankyou.html)
with other frames such as those containing the top company logo,
other links of the site etc. As a workaround, what I am doing
right now is to read the html page line by line and write it to
the output stream. this has a problem that when the html page
has an img tag in it, the servletrunner displays an error message
"servlet img not found".
A: This can be done by using the sendRedirect() method of the response
object.
Q: I'm trying to write a form base authorization (username/password)
servlet. I'm using redirection to send users to the login.html
when they attempt to access a protected page. However, I'm running
into a problem. I want the user only to have to login once per
browser session. I have tried shared objects and normal session
putValue() and getValue() methods. Unfortunately, I can only get
two behaviors. (1) login every time you attempt to access a protected
page; (2) login once and never have to again until the server
is reset. Is there anyway to detect the browser closing and use
this event to manually invalidate the user's session?
A: There's no way to detect the browser closing to invalidate the
user's session. Instead there is an invalidation time out value
which indicates how long the session must be inactive before the
servlet engine invalidates it. The default value is 30 minutes.
Q: I would like to know whether it is possible to get following
info whenever a html page is viewed: 1) the name and version of
the browser where the request came from 2) the url from which
the current html page was referred to
A: This information can be retrieved by calling
req.getHeader( "User-Agent" ) and req.getHeader( "Referer" );