Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

October 27, 2009

More on Servlets

Context parameters can not be anything other tham String. If we want to set init parameter to be sth else, we need to add a listener which would convert the String value to the desired value.
coz we can not put the code in Servlet as the servlet would have got the parameter value as String only by then.
This is done by implementing "ServletContextListener" interface which has two methods - contextInitialized(ServletContextEvent) and contextDestroyed(ServletContextEvent)

Difference between Attribute and Parameter

Attribute :
1. Type : Application / context , Request , Session
2. Method to set : setAttribute(String name , Object value)
3. Return type : Object
4. Method to get : getAttribute(String name)

Parameter :
1. Type : Application /context , Request , Servlet init parameters
2. Method : These can be set only in Deployment descriptor (web.xml)
3 . Return type : String
4. Method to get : getInitParameter(String name)

Context scope isnt thread safe. We can make them thread safe by using : synchronized(getServletContext())

Session persists across multiple requests from same client.
Its also not thread safe. We can makr it thread safe as we did to Context : synchronized(session)

Only requesr attributes and local variables are thread safe. Instance variables arnt.
we generally do not declare non final instance variables in Servlets.

We also have a "SingleThreadModel" interface, which can be used to make servlet thread safe. But this interface should better not be used as it effects performance a lot.

RequestDispatcher has two methods : forward(request , response) and include(request , response)
include() sends the request to do sth and it then comes back to the sender.
Difference b/w RequestDispatcher from context and from request

In the one from context, you can not specify relative path. u must specify absolute path.

No comments: