Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic questions in Servlets

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi everybody,

Q1) What is Session variable in Servlet? How to create session variables so that they are accessible even if Cookies are disabled by the browser?
Q2) How to use thread-safe Servlets?
 
madhu babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session is clientside info stored in server we create this
using HttpSession ses=request.getSession(true);
if u want to put any data in this
for ex:-
int count=10;
ses.putValue("count",new Integer(count)));
to get from session int i=((Integer)getValue("count")).intValue();
if u want make ThreadSafe implement u r servlet ThreadSafeModel.
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks madhu, But I still have doubts.
Q1)Are the servlets by default Multi-threaded?
Q2)In which package is the ThreadSafeModel?
Q3)What happens if I implement SingleThreadModel? Does it become thread unsafe then only or is the servlet thread unsafe even when it is multi-threaded?
Q4) Somewhere I read that we should never make the service(), doGet(), or doPost() methods synchronised to make servlet thread safe. Is it true? Why?
I am totally confused about this.
Bye.
 
prakash muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Ans1 ) Yes, by default servlets are multi-threaded
ans 2) ThreadSafeMOdel is a interface. Its comes under javax.servlet
Ans 3) If you want to access a object by a single thread, then you have to go for SingleThreadModel. So its calles threadsafe model
Ans 4) Yes, we should not make like that. If you want synchronized, you have to go for SingleThreadModel only.

I think, my answers will clear your doubt. Otherwise let me know, i'll send a detail
with regards
Prakash
(prakashem@rediffmail.com)
Originally posted by amit sanghai:

Thanks madhu, But I still have doubts.
Q1)Are the servlets by default Multi-threaded?
Q2)In which package is the ThreadSafeModel?
Q3)What happens if I implement SingleThreadModel? Does it become thread unsafe then only or is the servlet thread unsafe even when it is multi-threaded?
Q4) Somewhere I read that we should never make the service(), doGet(), or doPost() methods synchronised to make servlet thread safe. Is it true? Why?
I am totally confused about this.
Bye.

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi madhu,
Do you mean that servlets are by default not thread safe because they are multithreaded?

I thought that if servlet was made single-threaded then it becomes thread-unsafe because many client t\requests will try to access it which makes it thread-unsafe?
Why do we not make service(), doGet() and doPost() methods synchronised instead of making the servlet single-threaded which slows down the servlet??
I hope I am not asking too many questions all together!!!
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amit sanghai:

Hi madhu,
Do you mean that servlets are by default not thread safe because they are multithreaded?

Servlets will generally be executed simultaneously by multiple threads. For that reason, your coding will have to be thread safe.
Sometimes this is difficult to do. In those cases, you can tell the container to serialize requests by implementing the SingleThreadModel interface. This is an empty "flag" interface like Serializable, Remote, etc.

Why do we not make service(), doGet() and doPost() methods synchronised instead of making the servlet single-threaded which slows down the servlet??

It's the other way around: making these methods synchronized would greatly slow down the servlet because requests would have to wait for each other without the servlet container realising this.
If you implement SingleThreadModel, the container does not only serialize the requests for you. If the load is high, it may choose to instantiate multiple instances of your servlet to handle the traffic.
So by nicely telling the servlet container about the thread unsafeness of your code (by implementing SingleThreadModel) instead of handling it yourself (by synchronizing your methods) you give the container the opportunity to instantiate the servlet multiple times. The application will scale much better as a result.
Regards
- Peter
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Peter very much.
Your in-depth explanation has helped me a great deal to clear my confusion about making Servlets Thread-safe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic