aspose file tools*
The moose likes JSP and the fly likes How to use a jsp:useBean inside a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to use a jsp:useBean inside a Servlet" Watch "How to use a jsp:useBean inside a Servlet" New topic
Author

How to use a jsp:useBean inside a Servlet

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212

I am submitting data from a JSP to a Servlet .



How can i use this jsp:useBean inside my servlet so that i can set data automatically and transfer this User Object to the DAO Layer

Thanks in advance.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
you can't, but you can stuff the object into a session or request attribute.
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Can't i use hrad coded HTML in my servlet and pass this Object ??
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
you can't use template text in a servlet, although you can pass HTML through the HttpServletResponse.OutputStream object. Where does HTML come into play here? Here's a quick example of how you would pass an object from a jsp page to a servlet using the request object.




Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

PavanPL KalyanK wrote:
I am submitting data from a JSP to a Servlet

This is not done with a request dispatcher.

So, are you submitting or dispatching? (The latter would be very weird.)

If submitting, the JSP and servlet are executing under different requests. Back up a minute and explain why you are trying to do this. It's rasing all sorts of red flags.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Bear , i did not get what you said but this is what i am trying to explain .

The User data on to Form (From JSP) is submitted to a servlet only know

Let me explain :




Inside my servlet



There are many setter fields so instead of doing all these , i want to use jsp:useBean facility inside the servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Ah, now I understand. There is no analogous operation build into the Servlet specification. You can either set each, or you could use bean reflection to write your own that does the same thing as the useBean action. Or you might be able to find a 3rd party implementation.

Personally, in such situations I don't use a bean at all; I use a Map wrapped in a class that provides the getters. I don't need setters at all as the name/values pairs are already captured in the Map instance.
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Thanks , Could you please tell me or point out to a link "you could use bean reflection to write your own that does the same thing as the useBean action"

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

The bean classes in java.bean as well as the general reflection abilities built into Java let you find the setters for a particular property name.

It can be a bit messy, so before you go that route, I'd suggest looking into my Map wrapper scheme.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Map Wrapper .http://java.sun.com/docs/books/tutorial/collections/implementations/wrapper.html


If you want something you never had do something which you had never done
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Please check your private messages for an important administrative matter.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
What's wrong with the code you have in your servlet? The useBean action tag is for JSP ONLY.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

He wants to automatically populate the bean with the request parameters without having to enumerate each property.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Obviously you would use the setProperty action in JSP, but how is that possible in a servlet without using setter methods?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Ryan Beckett wrote:Obviously you would use the setProperty action in JSP, but how is that possible in a servlet without using setter methods?

That is exactly the conversation we've been having.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use a jsp:useBean inside a Servlet