File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes pass bean from jsp:setProperty to servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "pass bean from jsp:setProperty to servlet?" Watch "pass bean from jsp:setProperty to servlet?" New topic
Author

pass bean from jsp:setProperty to servlet?

Will Lee
Ranch Hand

Joined: Mar 16, 2005
Posts: 58
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:


SCJP 1.4, SCWCD, SCDJWS
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
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

Joined: Mar 16, 2005
Posts: 58
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

Joined: Jun 23, 2005
Posts: 266
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.
Alexandre Cervieri
Greenhorn

Joined: Aug 02, 2005
Posts: 27
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: pass bean from jsp:setProperty to servlet?