File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes probably a stupid question, but: how to pass data from jsp to 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 "probably a stupid question, but: how to pass data from jsp to servlet" Watch "probably a stupid question, but: how to pass data from jsp to servlet" New topic
Author

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

Jean Noel
Greenhorn

Joined: Nov 30, 2005
Posts: 3
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
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1220
    
    1

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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

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

Joined: Nov 30, 2005
Posts: 3
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: probably a stupid question, but: how to pass data from jsp to servlet