Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jsp or Servlet

 
Mandy Smith
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure whether to write a jsp page or a servlet.
I have lot of html contents to be included and some dynanmic contents fetched from database(like name, phone,email etc). I am not comfortable to do the database connection in a jsp page, where as if i go for a servlet i need to append lot of html contents to my buffer. So could anyone please suggest me what to go for.
Appreciate your suggestion!
Thanks.
 
Mahesh Rana
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say,
have your servlet hook on to the database
and generate the XML.
And let an XSL take care of the presentation.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend create a data bean that has all the information in it, place it in the request object, and forward to a JSP that would use the bean to fill in the dynamic content. Most of the page would be static HTML and you could do away with XML/XSL.
This is precisely the reason to use Servlets and JSPs.
[This message has been edited by Tony Alicea (edited October 08, 2001).]
 
Mandy Smith
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony, i think, its a good idea to create a data bean. I have never done this before. Could you please assist me.
Did you mean to fetch the data from the database and store all the required info in this bean, so that i have access to all info using get methods. I need to fetch user info from the session which i am using in my sql query. In my Data Bean i don't have access to session object. This jsp page only displays data and i don't require to do any processing part. This jsp page not only displays the data from one table but from several tables. So do you think i need to create separate data beans pertaining to particular table.
Also i am not clear with how to place the data bean in the request object and forwarding it to a jsp.
Could you please elaborate on this.
Appreciate your help!
Thank you.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You almost answered yourself the question
I'd define a JavaBean class (w/get, set etc...) that has all the fields needed by the JSP to display the user info.
The servlet would do the DB call and set all the required fields (properties) of the bean.
The servlet would place the bean in the request by calling
req.setAttribute("myBean", bean)
In the JSP you have two choices but I'd use the
<jsp:useBean id="myBean" CLASS="..." SCOPE="request" .../>
and then populate the fields like
Name: <%= myBean.getName() %>
Others??
 
Mandy Smith
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Tony! I had general idea like the way you have mentioned. Thanks for your detailed explanation.
Appreciate your help.
 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can Anyone recommend a good jsp/servlet book?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic