File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes What to use where? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "What to use where?" Watch "What to use where?" New topic
Author

What to use where?

Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Hello friends,
I am confused.
Servlets and JSP both do the same thing then in actual web development do we use Servlets.
Where are listeners and filters used, even we can work out without them?
Kindly guide.
Thanks in advance


Poornima Sharma
SCJP 6.0
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Originally posted by Poornima Sharma:
Servlets and JSP both do the same thing ...

They do not. Or at least should not.

Servlets are generally used for the controller portion of the application. They direct the processing of the web application and should not be used for generating output.

JSPs should be used to define the view portion of the application. They should not be used for processing or anything other than constructing the display.

Where are listeners and filters used, even we can work out without them?


Filters are used when you need to do something before (or after) multiple requests that is independent of the actions. Making sure that the user is authenticated for example.

Listeners are used to trigger based upon certain events. For example, when a web app starts up, or when a session expires.
[ November 20, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
So to get a database connection what should I use a servlet/listener/jsp.
I cant get the best.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

None of the above. DB access should occur in the model layer, not in the presentation layer.

The servlets, as controllers, generally use the services of the model layer, but don't perform database access themselves.
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
You mean I should create a connection in a class and create its object in a servlet?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837


Poornima Sharma
----
So to get a database connection what should I use a servlet/listener/jsp.
I cant get the best


You can use a listener (class which implements ServletContextListener) to create a DataSource and make it available to other parts of your web application (i.e: storing in the ServletContext) at the startup.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

What is MVC? Model View Controller.
Assuming that you are new to servlets and jsp you should first understand the MVC architecture.
[ November 20, 2008: Message edited by: Sudipto Shekhar ]

Regards, Sud.
SCJP 5 ScjpFAQ JLS
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by Poornima Sharma:
So to get a database connection what should I use a servlet/listener/jsp.
I cant get the best.


The role of each one of them is different.Maybe this will make the differences clear.

originally posted by Sudipto Shekhar
you should first understand the MVC module.

Its not a module an appropriate term is a design pattern or an architecture.


SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What to use where?