Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
22Activity
0 of .
Results for:
No results containing your search query
P. 1
Servlet Quest

Servlet Quest

Ratings:

4.67

(3)
|Views: 1,026|Likes:
Published by prabunivas

More info:

Published by: prabunivas on Sep 12, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

01/03/2013

pdf

text

original

 
1) Is it the "servlets" directory or the "servlet" directory?
 For Java Web Server:
 
on the file system, it's "servlets"
c:\JavaWebServer1.1\servlets\DateServlet.class
 
 
in a URL path, it's "servlet"
http://www.stinky.com/servlet/DateServlet
 
2) How do I support both GET and POST protocol from the same Servlet?
 The easy way is, just support POST, then have your doGet methodcall your doPost method:public void doGet(HttpServletRequest req, HttpServletResponseres)throws ServletException, IOException{doPost(req, res);}
3) How do I ensure that my servlet is thread-safe?
]This is actually a very complex issue. A few guidelines:1.
 
The
init()
method is guaranteed to be called once per servletinstance, when the servlet is loaded. You don't have to worryabout thread safety inside this method, since it is only calledby a single thread, and the web server will wait until thatthread exits before sending any more threads into your 
service()
method.
 
2.
 
Every new client request generates (or allocates) a newthread; that thread calls the
service()
method of your servlet(which may in turn call
doPost()
,
doGet()
and so forth).
 
3.
 
Under most circumstances, there is only one instance of your servlet, no matter how many client requests are inprocess. That means that at any given moment, there maybe many threads running inside the
service()
method of your solo instance, all sharing the same instance data andpotentially stepping on each other's toes. This means that
 
you should be careful to
synchronize
access to shared data(instance variables) using the
synchronized
keyword.(Note that the server will also allocate a new instance if youregister the servlet with a new name and, e.g., new initparameters.)
 
4.
 
Note that you need not (and should not) synchronize onlocal data or parameters. And especially you shouldn'tsynchronize the
service()
method! (Or 
doPost()
,
doGet()
 
et al 
.)
 
5.
 
A simple solution to synchronizing is to always synchronizeon the servlet instance itself using "
synchronized(this) { ... }
". However, this can lead to performancebottlenecks; you're usually better off synchronizing on thedata objects themselves.
 
6.
 
If you absolutely can't deal with synchronizing, you candeclare that your servlet "
implementsSingleThreadModel
". This empty interface tells theweb server to only send one client request at a time into your servlet. From the JavaDoc:
"If the target servlet isflagged with this interface, the servlet programmer isguaranteed that no two threads will execute concurrently theservice method of that servlet. This guarantee is ensured by maintaining a pool of servlet instances for each such servlet,and dispatching each service call to a free servlet. Inessence, if the servlet implements this interface, the servlet will be thread safe."
Note that this is not an idealsolution, since performance may suffer (depending on thesize of the instance pool), plus it's more difficult to share dataacross instances than within a single instance.See alsoWhat's a better approach for enabling thread-safeservlets and JSPs? SingleThreadModel Interface or Synchronization?7.
 
To share data across successive or concurrent requests,you can use either instance variables or class-staticvariables, or useSession Tracking.8.
 
The
destroy()
method is not necessarily as clean as the
init()
method. The server calls destroy
either 
after all service callshave been completed,
or 
after a certain number of secondshave passed, whichever comes first. This means that other threads might be running service requests at the same time
 
as your 
destroy()
method is called! So be sure to synchronize,and/or wait for the other requests to quit. Sun's ServletTutorial has an example of how to do this with referencecounting.
 
9.
 
destroy()
can not throw an exception, so if something badhappens, call
log()
with a helpful message (like theexception). See the "closing a JDBCconnection" example in Sun's Tutorial.
 
4) What is the difference between URL encoding, URL rewriting, HTMLescaping, and entity encoding?
 
URL Encoding
is a process of transforming user input to a CGIform so it is fit for travel across the network -- basically, strippingspaces and punctuation and replacing with escape characters. URLDecoding is the reverse process. To perform these operations, call
 java.net.URLEncoder.encode()
and
 java.net.URLDecoder.decode()
(the latter was (finally!) added to JDK 1.2, aka Java 2).
Example:
changing "
We're #1!
" into "
We%27re+%231%21
"
URL Rewriting
is a technique for saving state information on theuser's browser between page hits. It's sort of like cookies, only theinformation gets stored inside the URL, as an additional parameter.The HttpSession API, which is part of the Servlet API, sometimesuses URL Rewriting when cookies are unavailable.
Example:
changing <A HREF="nextpage.html"> into<A HREF="nextpage.html;$sessionid$=DSJFSDKFSLDFEEKOE">(or whatever the actual syntax is; I forget offhand)(Unfortunately, the method in the Servlet API for doing URLrewriting for session management is called encodeURL(). Sigh...)
 
There's also a feature of the Apache web server called URLRewriting; it is enabled by the
mod_rewrite
module. It rewrites URLson their way
in
to the server, allowing you to do things likeautomatically add a trailing slash to a directory name, or to map oldfile names to new file names. This has nothing to do with servlets.For more information, see the Apache FAQ(http://www.apache.org/docs/misc/FAQ.html#rewrite-more-config) .
5) How do I upload a file to my servlet or JSP?

Activity (22)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Ankit Jain liked this
ALi liked this
Rania Fahim liked this
rajaramansuresh liked this
rajaramansuresh liked this
shahidrana1122 liked this
gaslanyan liked this
k2sh liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->