• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Can a jsp talk to a servlet?

 
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
 
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
 
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.
 
mister krabs
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.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!