• 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

pass bean from jsp:setProperty to servlet?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A practical question:
HFSJ p357: going str8 from req. to jsp, you can use
<jsp:useBean id="bean1" class= scope="request"(or "session")>
<jsp:setProperty name="bean1" property="*" />
</jsp:useBean>

How can I (or if it's possible) to pass this automatically populated bean1 to servlet? For example,
the jsp contains:
<form action="myServlet.do">
<input type="text".....>

<input type="submit">
</form>

<jsp:useBean>...............................</jsp:useBean>


In the mapping "myServlet" class, is there a way to fetch this bean1? :roll:
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have the scope for the bean as "session". Then in your servlet you can get the bean from the session object associated with the request.
 
Will Lee
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did but failed
Here's part of my jsp code:


<form action="getEmployee">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

<jsp:useBean id="employee" class="test.Employee" scope="session">
<jsp:setProperty name="employee" property="*" />
</jsp:useBean>



And part of my servlet (maped to getEmployee)


protected void doGet(req, resp) throws ... {
HttpSession session = req.getSession();
Employee emp = (Employee) session.getAttribute("employee");
if(emp != null)
System.out.println(emp.getName());
else
System.out.println("get nothing");

RequestDispatcher rd = req.getRequestDispatcher("/showInput.jsp");
rd.forward(req, resp);
}



Employee is the bean w/ property "name".

First time to run it, console print out "get nothing" since no bean in the scope, but later on, there's bean with name = null, i.e., it didn't load data into the bean.

What's wrong w/ my code?

 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the bean is not null and the property "name" is null, it means that the request has no parameter with the name "name". Just try setting a "name" parameter in the request object before you forward it to the jsp.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Will,

What you are seeing as a result of your code is correct. You will not have the result you want with you current JSP. Let me explain: You have a single JSP with the following code, right?

<form action="getEmployee">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

<jsp:useBean id="employee" class="test.Employee" scope="session">
<jsp:setProperty name="employee" property="*" />
</jsp:useBean>

When the user calls this single JSP what happens is: a html form with a input text will be shown in the browser, in the server a bean called employee will be created if it doesn't exists in the session; only if the bean is being created all its properties will be set according to the current request parameters.

Then, when in the browser you click the submit button you will be calling the servlet directly and not the JSP, so, since the JSP will be called once your parameters (only the 'name' parameter in fact) will never be set in the bean.

To achieve the result you want you should remove the jsp:setProperty from the body of the jsp:useBean and submit the html form to the same JSP before going to the servlet.

I hope this help. I'm sure that there are better ways to do that besides the one I exemplified to you.

Regards.
reply
    Bookmark Topic Watch Topic
  • New Topic