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

How to use a jsp:useBean inside a Servlet

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't, but you can stuff the object into a session or request attribute.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't i use hrad coded HTML in my servlet and pass this Object ??
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map Wrapper .http://java.sun.com/docs/books/tutorial/collections/implementations/wrapper.html
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check your private messages for an important administrative matter.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He wants to automatically populate the bean with the request parameters without having to enumerate each property.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic