• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

probably a stupid question, but: how to pass data from jsp to servlet

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I've been working for a long time with JSF, and now that I have to work with JSP/Servlets, I can't figure out how to perform basic things I was easily doing before.

Basically, I have a jsp that displays a form; the user types in data, and when submitting the form, a servlet is invoked to separate business logic from presentation.

In my jsp, I have included the bean:
<jsp:useBean id="expert" scope="session"
class="....Expert" />

I have tried adding:
<jsp:setProperty name="expert" property="*"/>

right below the bean declaration, without success.

I have declared the input field:
<input id="firstname" type="text" class="form-control"
placeholder="First name" value="${expert.firstName}" />

When clicking on 'submit', I would expect the servlet to find a session variable named 'expert' (which it does) with the value entered by the user in the fields.

Unfortunately, this does not work and fields in the servlet are always null.

I'm probably missing something here, but I'm running out of idea, so would appreciate some help... Am I trying to do something impossible?

Thanks

/jnc
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jean

Welcome to Java ranch !!
First of all , no question is stupid question .. every question is equally important ..
every question or query has only aim - to give a chance to Person to increase his knowledge ...
feel free to ask any query/question related to Java in this forum ..

related to your question, have you set your object in HTTPSession in your servlet class?

Thanks
Abhay
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are submitting to a servlet (as you should be -- a JSP should never be the target of a submission) you don't need all of that useBean crap in the JSP. Get rid of it.

In the servlet, you'll fetch the submitted data through the request.getParameter() family of methods on the request instance.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhay Agarwal wrote:related to your question, have you set your object in HTTPSession in your servlet class?


There is no need for any session object at all. Let's not make this any more complicated than it needs to be.
 
Jean Noel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:If you are submitting to a servlet (as you should be -- a JSP should never be the target of a submission) you don't need all of that useBean crap in the JSP. Get rid of it.

In the servlet, you'll fetch the submitted data through the request.getParameter() family of methods on the request instance.



Well, I guess that if the value in the jsp need to be initialized, this has to happen in the bean, hasn't it?

I understand I can do a getParameter for each field, but it would be much more convenient if the bean members would get all populated at once... Would save me a lot of line of codes...

But thanks a lot for your help

Jean-Noël
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jean Noel wrote:Well, I guess that if the value in the jsp need to be initialized, this has to happen in the bean, hasn't it?


Not like you are doing. The useBean action and its ilk like setProperty are pretty much obsolete goop.

If you want to initialize the values in the JSP, its page controller should create a scoped variable (bean) and put it in request scope prior to forwarding to the JSP. Then (without the need for useBean goop) the EL can be used to provide the value of the value attribute.

For example, if the page controller creates a scoped variable named myValues with a bean that contain the various properties, it could be used as such:


Easy as that. No useBean, no setProperty, no goop.

If the value can contain quotes, it'd be best to wrap it using <c:out>.

I understand I can do a getParameter for each field, but it would be much more convenient if the bean members would get all populated at once... Would save me a lot of line of codes...



There are tools for that. For example: BeanUtils.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic