Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a jsp talk to a servlet?

 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little unclear as to the concept of using jsp's and servlets. I was asked to develop something that would allow users to update/search a database. So the basic idea is that users will go to a web page, and from there be able to access the database.
The information that I've read has talked about JSPs and servlets, but independently of each other. Can I use a JSP to create the web page, that calls the servlet to do the db connection? Is this how it works?
Annette
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, if you want.
JSPs are best for pages with lots of static HTML and small amounts of variable content. Servlets are best for pages with lots of variable content and small amounts of static HTML.
Depending on how complex the "results" page is you may prefer to use JSP, a servlet which uses a template system such as WebMacro, or a servlet with hard-coded HTML.
 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically the JSP will just need to pass variables back to the servlet to specify the search criteria. The users will be able to navigate to different pages depending on what they want to see, and then from there enter a word by which they would like to search the database. So this would be the best way to go about doing it?
Annette
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I have been doing it is to have each dynamic request initially handled by a servlet. The servlet can
  • verify the user is logged on
  • retrieve request parameters
  • perform necessary actions such as database updates or queries
  • save results (including database result sets or other objects) into the response attributes
  • redirect to the proper .jsp page

  • If the application logic is trivial (a simple database query) I do it right in the servlet. Otherwise, I have application specific classes that handle the database interactions and business logic.
    This allows separating the flow of control, the business logic, and the presentation from each other.
    Any feedback on this approach would be appreciated.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to do everything through a jsp you can. But don't have the jsp call a servlet. Instead have it call a java bean to do the database access.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic