Subject: init(),encodeurl()
Posted By: naveen Post Date: 9/26/2004 6:42:25 AM

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



Reply By: sonicDace Reply Date: 10/8/2004 10:25:43 AM
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.jsp?jsessionid=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

Go to topic 19986

Return to index page 750
Return to index page 749
Return to index page 748
Return to index page 747
Return to index page 746
Return to index page 745
Return to index page 744
Return to index page 743
Return to index page 742
Return to index page 741