Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 26th, 2004, 06:42 AM
Registered User
 
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to naveen
Default init(),encodeurl()


hi,
please find me the answers for:
1) in servlets i am confused with multithread and
   singlethreadmodel regarding the invocation of init() method.

    multithreaded servlets and singlethread serlvet in both cases
  does the servletcontainer invoke the init() method every time an
  object is created or only once for all the instances..?

    can anyone help me out of it.
=================
2) functionality of encodeURl(),encodeRedirectURL()

regards
naveen



 
Old October 8th, 2004, 10:25 AM
Authorized User
 
Join Date: Nov 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to sonicDace Send a message via MSN to sonicDace Send a message via Yahoo to sonicDace
Default

Hey, Naveen

Yes, that is quite a confusion... Here's how it goes:

init() is inovoked by the container (where your web app is running). The init method of the context is invoked when the web app starts, or when the first request for a resource on your web app is made (depending on how the people who developed the container felt like implementing it). As for a servlet init, it is invoked when the servlet is instantiated, which could be when the context is started up, or when the first request for your servlet comes through (you can configure this in the deployment descriptor).

SingleThreaded model has been deprecated in the latest Servlet spec, but if you're not using the lates spec, and you need to implement some thread safety mechanism, I recommend some reading as implementing the SingleThreaded model is basically the same as synchronizing the Service method. Instance variables, Session and Context attributes are still not thread safe.

the container creates 1 instance of your servlet, when it does that, it will call the init method then and only then. And for every request, depending on container implementation, a thread object will be created which at some poing will call the Service method on that 1 instance of your servlet. When you implement singlethreadedmodel, all you are doing is saying "If another thread wants to run my service method while I'm running it for someone else, it'll just have to wait til I'm done"

2) the session ID is typically implemented as a unique ID in a cookie called jsessionID. When a user has cookies disabled in his browser, then he can't make use of whatever functionality was implemented using sessions UNLESS the web app developer took the previsions of URL rewriting.

URL rewriting simply appends the jsessionID as part of a request attribute if and only if the requesting user's browser has cookies enabled. So now a request for

http://www.mysite.com/myapp/mypage.jsp

would look like this:

http://www.mysite.com/myapp/mypage.j...ssionid=123456

the way you us it is calling the encode methods on your URL, which in jsp would be done like this:

<a href="<%=response.encodeURL("http://www.mysite.com/myapp/mypage.jsp") %>"/>

To score a couple of brownie points with WROX, they have a very good book that covers this topic and much more ;)

Hope this helps

thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
Applet does not init Tomcat 5.5 dl Apache Tomcat 0 April 27th, 2005 08:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.