Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp vs servlets vs. beans

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello.

i hope someone can help me out with this.
i am currently beginning to do web applications in java and i am using jsp with java beans.

i just want to know how that method differs with using servlets?
i mean, if you use servlets, does that replace the usage of java beans?
so now you program with jsp and servlets? and what do you get from it or what are the advantages?

thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets do not replace java beans.
You may use java beans(model) to pass information to your Jsp(view).
It's a little wide subject, so you should look for tutorials or books on the subject.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou is correct.

Search for tutorials using keywords "Model, View, Controller" or "MVC" and you will find plenty of information.
 
Karn Sadyapongse
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use servlet to control the process
use javaBean to get/set data
use jsp to show result

this is an easy example
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. i found this

Controller - extends HttpServlet, acts as the point of entry into the application, and delegates to various worker classes to fulfill a request. In particular, the Controller is a user of Model and View objects

Model - data-centric classes encapsulating problem domain objects. Each class corresponds roughly to the rows of a database table. Model objects can be constructed from a ResultSet of a database query, from user input, or from user request parameters.

View - implemented as Java Server Pages (or a similar tool), primarily concerned with presentation and formatting of Model objects which have been placed in scope by the Controller (or its delegate)

so basically, your jsps, are mainly for display purposes and should do no processing and computation, which should be done in servlets. database access and queries should be done ine java beans. is that so?
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so basically, your jsps, are mainly for display purposes and should do no processing and computation, which should be done in servlets.


correct


database access and queries should be done ine java beans. is that so?


Well, not really. The data retrieved from the db is modelled as a bean. For example, an employee table would have an employee bean. Each row in the db that is returned by a query is transformed to a bean whose fields are populated with column values from that row. A list of such bean objects are placed in some scope and used by the jsp for view rendering.

The db access itself occurs in a dao layer which roughly put, is a set of classes that that has methods to query the db for a particular set of data, and return the data as a list of bean objects as explained above.

The dao layer classes can be accessed directly from the servlet or from helper classes which then, in turn are invoked by the servlet.

Hope that helps.

cheers,
ram.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so basically, your jsps, are mainly for display purposes and should do no processing and computation, which should be done in servlets.


NOT Correct.

processing and computation are supposed to be done in MODEL. Servlets(CONTROLLER) should only be used to control(calling model components dispatch to the correct view)the web app.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a crude MVC diagram. By CRUD I mean create/update/use/delete.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

processing and computation are supposed to be done in MODEL. Servlets(CONTROLLER) should only be used to control(calling model components dispatch to the correct view)the web app.


I wouldnt be too quick to classify that as incorrect. It depends on what the user has in mind when he/she uses the word 'processing'.

Some people use request processing to mean accepting a request, calling handlers and final redirection to the view. This is certainly the work of servlets.

ram.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roger Chung-Wee:
Here's a crude MVC diagram. By CRUD I mean create/update/use/delete.



there's no arrow from the beans or model
i mean, after it goes there, does it pass the crud data to back to the servlets or the jsp?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm currently using just jsps and beans.

what happens then if i don't have servlets?
 
Ola Daniel
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shuini gustav

there's no arrow from the beans or model
i mean, after it goes there, does it pass the crud data to back to the servlets or the jsp?

The route should be:
Bean ----->>>>> Class ---->>>>>> Servlet ----->>>>> JSP

shuini gustav

i'm currently using just jsps and beans.

what happens then if i don't have servlets?



To effectively implement the MVC Model. You will need to use a Servlet as you controller.

However, you can successfully work with just JSPs and JavaBeans and end up putting all your java code in the JSP which would result in very lousy code that will not be easy to read and maintain.

So I donot recommend working with just the JSP....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic