• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error on threads

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


I am new to threads concept.I tried to get the output checking current thread and display relevent page.But i am not able to get successful output.Please help to solve this problem.




The above program checking one condition at a time .I need to if calleridstatus zero means the corresponding output will be displayed

otherwise if calleridstatus one means the corresponding output will be displayed.How to solve this problem.




Regards,
Suganya.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its not good practice to implementing runnable in Servlet.

Have a separate class implements runnable and have your logic there and call it from the servlet.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suganya,



The above program checking one condition at a time .I need to if calleridstatus zero means the corresponding output will be displayed

otherwise if calleridstatus one means the corresponding output will be displayed.How to solve this problem.



I am not exactly sure of your problem. How exactly is the application behaving? How is that different than what you expect?


Unfortunately, there is a lot of problems with your code which will eventually cause problems. The rest of this post is less about fixing the problem you describe, and more about suggesting improvements.




You should NEVER store instance variables in a Servlet. Every request to a servlet uses the same Servlet instance, which means every user who reaches your site will be sharing the same Statement, ResultSets, Connection, and User. Then they will also be sharing the same reference to the request and response. This is very bad. If two users attempt to use the Servlet at the same time these variables will begin to be traded between users, their states will get messed up, and the application will break (possible very badly).

So NEVER store instance varables. When possible, pass variables from one method to another through parameters and return values.



So run() doesn't do anything. Like the previous poster said the Servlet should not be a Runnable. Since the run() method doesn't do anything it is best to just remove it, and remove the 'implements Runnable'.


You create a new Thread, then check its name. Since you have not set the name of the Thread yet, the Thread name will NEVER be "crmp1thread". It would only become that after you call th.setName("crmp1thread"). So this whole if construct is rather pointless. It should just be:



Danger! Danger! Danger!

You are calling while(true) inside a Servlet's doGet() method. This leaves the possibility that a request to the Servlet may never end. After just a few users it would use up all the allowed incoming connections, and requests to your application will be blocked and time out. You should not do while(true) in a request service method (or request thread). The request should be controlled, as short as possible, and have a specific end point. If you need to run a long-lasting task then you should move the task to a background thread to free up incoming requests.


Just to be clear. In this while loop, you are running the same query over and over again and only looking at the first entry returned from the query each time. Is that the behavior you want?

To improve your code you should look at using PreparedStatements.

It is a little safer to call:

since it prevents the possibility of a null pointer exception... just a style preference, but if you don't use it you should check for calleridstatus == null.


You are commenting out your connection closing. Where as I agree that these places are not the proper place for the closing, I would suggest that you do not forget to close them entirely - it is another way of eating resources and calling future problems. You want to close the connection in a finally block, probably after the catch(...) block you have below.



It is generally not a good idea to catch the general Exception. Rather catch specific exceptions. Also, since you are running in a Servlet environment, printStackTrace may not be the best way of reporting the exception (if your lucky the exception gets buried in some log deep in your server's path somewhere). You are better off using a logging tool, and wrapping the exception in a ServletException and re-throwing it, so the Servlet engine's exception handling tool can report it properly.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic