• 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

Servlet Project Plan

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I'm attempting my first big project with Servlets. This project will have 2 parts to it. The 1st part, takes data from URLs and enters it into a DB based on a timer interval (as you can see in the run() method below). The 2nd part (specified in the doGet() method below) is going to serve the DB data to users requesting it from an html get request.

My plan is to have this working in such a way that multiple clients can access the database/doGet() method in a thread safe environment while the run() thread is doing its duty.

The users will be requesting the DB data through an html "Get" form. My question to you is that, is this thread safe code in such a way that I can have multiple users making Database requests? And based on an entire new program design.. would the following design work?



Any guidance would be extremely appreciated.

Edited by: ManRed on Apr 16, 2009 1:06 PM
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's up with the run() method?
 
Eric Belec
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:What's up with the run() method?



I went over the javadocs on threading and thought this was a good idea.. I may be wrong haha. My reason(s) are below so please feel free to disagree if what I'm saying is a bad idea/ doesn't make sense.

The run() methods task is to create a thread and inside this thread I have a DB job running on a sleep interval. This job runs at all times the servlet is running.

The doGet() method will respond to client requests where I will show them entries made into the DB. Both processes run concurently and the doGET methods use the data from the run() job.

Please let me know if anything is confusing. Thanks for your reply!
 
Eric Belec
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I now have figured out your point... I was under the impression that my servlet would be able to perform a timed automated job which runs at all times and independent of clients Get Requests.

I guess a servlet only runs when a user requests it right? As such, I can't have a task running at all times?

Does this mean in the code above I'm unintentionally creating a separate job process with each client doGet() request?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still not sure what it is you expect your background process to be doing, but it should not be part of a servlet. A servlet should be only for responding to requests.
 
Eric Belec
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I'm still not sure what it is you expect your background process to be doing



Well, I didn't want to complicate matters with that detail but if it helps, this background process grabs data from a dynamic URL (example, RSS feed) and places it into a DB.

Bear Bibeault wrote: but it should not be part of a servlet. A servlet should be only for responding to requests.



I was under the impression I could do looped jobs in a servlet by creating another thread (while still responding to client requests). So, you're saying that I shouldn't be using a servlet to do my jobs. AHHH

What would be the best way to have the jobs running on the Database while still being able to reply to client doGet() requests within my servlet?? Do you think I should create a Java console application which does the jobs for me and then use the Servlet for displaying the updated DB to users?
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When ever a servlet receives request, it creates a thread for that particular request.
And if you create a thread to run your db process, from the request thread, then the request might complete and the response will be sent before even the database operation is completed.

So i think it is not a good idea to create a new thread from a servlet.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try an answer your other doubts question first and then get back to the original problem

Eric Belec wrote:I think I now have figured out your point... I was under the impression that my servlet would be able to perform a timed automated job which runs at all times and independent of clients Get Requests.
I guess a servlet only runs when a user requests it right? As such, I can't have a task running at all times?
Does this mean in the code above I'm unintentionally creating a separate job process with each client doGet() request?



Yes a servlet runs only when a client make a request. However the servlets init() method runs just once - when the class is loaded. So the code the way you have written it creates just one thread - not one thread per client. It should just keeps loading your data into the database in the background.


Coming back to your original problem - What happens when you try to run your code? Frankly i don't like seeing threads created with a servlet - if you really must look out for some application listener - something that would listen when the application is loaded - I have not used listeners for long - so out of touch here - or maybe you can run the thread code as a separate process outside the web application.

Why do you need a thread safe application? Thread safety is important - but from my understanding of your requirements you do nothing that needs thread safety. The fetching and inserting URL data into the database is completely independent of any user request. Right? Or am I missing something?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why you are worrying about Thread management , your code is in a servlet and , servlet is deployed in a servlet container , and all Thread management is servlet container task.

You only concentrate on your business logic and left all Thread management or request handling to servlet container
 
Eric Belec
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help guys!!! That clears that up! In the end, I created a separate Java application that does the automated tasks for me.

Now doing the insanely hard 2nd part (the Servlet) where I respond to clients via doGET() to clients wanting to see the Database. Thing is, I'm looking to create 4 pages to which users can access where they must first be logged in. This includes (take a deep breath 1st):

- Registration Page // check link below
- Login Page // also in link
- Signup Page // ohh and guess what.. no way, its in the link too
- Main Page // my custom page (shows info to client from the DB)

Please don't forget that I'm just doing this for my sake and if the login page looks like crap that's because it actually is crap and well this is just for learning purposes. Either way, I'd still like to create this servlet the proper way. So... after some reading I found the famous "Model-View-Controller" (MVC) paradigm but found it looks really complicated.

I've found a tutorial here, Servlet Controller Tutorial

Has anyone used this type of design before? Can I also have sessions without using this Front Controller Servlet which is redirecting all the request to the appropriate pages? Which approach would you guys recommend where I can have the possibility of loging in clients and accessing a Database?

Did I just go way over my head here or is this all realistic?
 
Saifuddin Merchant
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like what you say in your post ->

I'd still like to create this servlet the proper way.

...

Everyone's who has developed a web app in java is sooner or later (well sooner really) going hear about MVC. You'll really find loads about MVC just a simple google would get you tons of stuff. I sure they got a tutorial in the FAQ section right at the Ranch. (Sorry I dont have the link - maybe someone will post soon)

Ok now for your other query

Can I also have sessions ...


Sure you can. As long as you have a web application inside of web server you can have session (does not matter if don't you use MVC, controller, Servlets...) you still get sessions.

As long as you have a web application you can log clients and accessing Database. Personally I think programming is so much of a personal style at least while learning that you just need to try a little get hang of things and then move to better design...Rather than design perfect first specially while starting...

You know it would be better if you start creating some part of you application (maybe login)...show what you are doing and we could take it from there on...

BTW - if you still not read Head First JSP and Servlets I'll recommend the first 6 chapters!!
 
Saifuddin Merchant
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Belec wrote:I think I now have figured out your point... I was under the impression that my servlet would be able to perform a timed automated job which runs at all times and independent of clients Get Requests.



By the way did someone say you were right out here? Your thread would have run independent of the clients get request!


 
Eric Belec
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Mercs wrote:

Eric Belec wrote:I think I now have figured out your point... I was under the impression that my servlet would be able to perform a timed automated job which runs at all times and independent of clients Get Requests.



By the way did someone say you were right out here? Your thread would have run independent of the clients get request!



No.. ohh well I've already created a separate application for the job anyways.. good to know for the future design thanks!

Thanks for your help Sam, I just find it strange that in the tutorial (from my last post), that the login page is actually in .jsp and that its form action is to post to the subscribe page. This alone is very confusing. In my design I would just create a static html page for login and submit it to the controller just as the subscribe.html page does (in the tutorial I posted above). Any thoughts on this would be welcome.

Anyways, thanks for your help, I'm going to now start the login page as you said I should. I'll post updates as I go along to let you know how poorly I'm doing haha.
 
Saifuddin Merchant
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Belec wrote:
No.. ohh well I've already created a separate application for the job anyways.. good to know for the future design thanks!


Well what I meant was you were technically right - but its still not a good idea to put side effect code in your servlet ... as Bear pointed out your servlet is one thing only to serve the client request (either get or post)... also its not a good idea to put your own threads in a servlet...so no from a future point I'll defiantly say what you did is the right way to go - create a separate standalone application

Eric Belec wrote:
I just find it strange that in the tutorial (from my last post), that the login page is actually in .jsp and that its form action is to post to the subscribe page. This alone is very confusing. In my design I would just create a static html page for login and submit it to the controller just as the subscribe.html page does (in the tutorial I posted above). Any thoughts on this would be welcome.


Yep your right. In fact that's the way it should be done and is done at most times!! But there is a good reason to create even all your static pages as JSP instead of HTML (you know have static content but just rename it as a JSP). Why? Well lets say tomorrow you decide you want to display the server date on each page (dynamic stuff - you need jsp). Easy stuff just rename the HTML page to JSP. Ooh Oh...You just realized you got the .html link in a 100 pages which you will now have to change to .jsp.

Eric Belec wrote:
Thanks for your help Sam,

yw of course! (yw==your welcome)
 
reply
    Bookmark Topic Watch Topic
  • New Topic