File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes include jsp in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "include jsp in servlet" Watch "include jsp in servlet" New topic

include jsp in servlet

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I understand that in servlets there is nothing like this for auto-setting a bean's properties:

<jsp:setProperty name="myBean" property="*" />

However, I thought I saw something online about getting the RequestDispatcher when in a servlet doPost() method and including a JSP (that contains the auto-populating jsp:setproperty tag) like this:

Is this a good idea? I'm guessing I wouldn't want to do a lot of output in that JSP (just have it set the bean's properties), partially because it seems like it could get messy. Or do I just check the response after that method to see if the JSP outputted anything?
Tim Hannaford

Joined: Jun 16, 2004
Posts: 6
I am not sure if I understand the situation. Why would you want a servlet to call a JSP to set a bean? Its easier to set a bean with a servlet and forward it to a JSP.
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
What you say is true. I'm talking about how if you POST from a JSP then you can use the jsp:setProperty to auto-set all of a bean's properties before you even reach the servlet or whatever it is you're POSTing to (it's handy). However, a servlet doesn't have that bit of shorthand, so you have to do it all manually. I seem to remember seeing someone use RequestDispatcher.include in a servlet to include the output of a JSP, and I was wondering if that would be a way to use the quick setProperty from within a servlet.

But I guess I'm showing how tired I've been the last couple of days, because there's no reason why if I'm posting from a JSP to a servlet that I can't still use the jsp:setProperty in the JSP from which I'm POSTing! :roll:
Larry Cryderm

Joined: Mar 23, 2004
Posts: 9
Check out the servlet code the jsp creates (in the *.java file). Emulate that code in your servlet to set the values. Remember, jsp's turn into servlets and those "short cuts" in jsp code are expanded in the servlet that is executed.

I shoulda been a rock star.
chandana sapparapu
Ranch Hand

Joined: Sep 28, 2002
Posts: 63
This may not be a relevant answer, but can you post your form date to a jsp first?
That is, you have form.jsp, and you want to post it to a servlet.
My solution is have a setter.jsp,

form.jsp -> set using setter.jsp -> post it to servlet.

will that work for your problem?
I agree. Here's the link:
subject: include jsp in servlet
It's not a secret anymore!