Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What to use where?

 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean I should create a connection in a class and create its object in a servlet?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic