How do I attach a JavaBean to a HTML Form submission
I found an example in the O'Reilly JavaServer Pages book that shows you how to pass a JavaBean from a JSP to a servlet.
<jsp:useBean id="userInfo" scope="request" class="com.ora.jsp.beans.userinfo.UserInfoBean">
<jsp:setProperty name="userInfo" property="*" />
</jsp:useBean>
<jsp:forward page="/myServlet" />
Then in the servlet, you do:
UserInfoBean userInfo = ( UserInfoBean )request.getAttribute("userInfo");
I want to do almost the exact same thing...but I don't want to forward the request. I want to pass the JavaBean along as part of an HTML form submission. Can anyone help me out with this? Thanks.
# 1
[nobr]Assuming that you have setup your environment correctly and you have a web application server like Tomcat running.
Since you didn't provide the code for the bean I created my own. You can change the code any way you want.
Say UserInfoBean is :
package test17;
public class UserInfoBean {
String firstName;
String lastName;
public UserInfoBean() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
And say you have a JSP page like this:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title></title></head>
<body>
<jsp:useBean id="userInfo" scope="request" class="test17.UserInfoBean">
<jsp:setProperty name="userInfo" property="*" />
</jsp:useBean>
<form action="index.jsp" method="POST">
<input type="text" name="firstName" value="${userInfo.firstName}"/>
<br/><br/>
<input type="text" name="firstName" value="${userInfo.lastName}"/>
<br/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
If you access the JSP page like this:
http://localhost:8080/p/test17/index.jsp?firstName=George&lastName=W
then it passes in the request parameter from the query string and populates the form.
Once the form is populated if you submit the form , data is transmitted to the next page , and you can access it via request.getParameter.
Is this the solution you are looking for?[/nobr]
# 2
Actually that's not what I'm trying to do. That example, if I understand it correctly, would supply data from a JavaBean back into the Servlet. But it doesn't pass the whole JavaBean. The JavaBean I'm using is somewhat complex (objects within objects). I'm trying to avoid passing "data" from the JavaBean because it will get pretty messy. I'm trying to attach the JavaBean as an "attribute" not a "parameter". When it gets to the Servlet, I'd extract it out with a "getAttribute" call.
# 3
Anyone have any other thoughts on this? Thanks.
# 4
I guess what you are trying to say is this: you want to generate some HTML and send it to the client's browser. When the client clicks on the button to request your servlet, you want to be able to access your JavaBean in that future request.
Well as you know HTML is text, so you can't include your JavaBean in the HTML. Not unless you want to convert it to hidden fields in the request, which you'd then have to put back together when your servlet starts processing the request.
Your best bet is to put the JavaBean into the user's session and have the servlet get it out of the session when it starts processing the next request.
# 5
You can use response.sendRedirect(nameof yourjavefile?a=1&b=2&c=3....) a,b,c,.. are parameters of your choice.
# 6
Gregory, I think it's would be better if you provided some code or initiated some code snippets that would help us understand what exactly you need and then we'll be able to provide you with more accurate solutions rather than simply guessing what you need.
# 7
OK, I'll work on a code snippet to submit. In the mean time, let me clarify the reason I'm trying to send the JavaBean in the HTML. I have a view (JSP) that is populated based on a large amount of data. I currently send that data to the JSP for display/modification via a JavaBean...standard stuff so far. In that view, there is the potential (depending on which submit button the user clicks) to require all the data to be sent to a servlet for "re-processing". Most of the data that was originally passed in is reused. I realize I could pass in all the data in the HTML as simple parameters, then re-build the JavaBean from that data. However, that would be painful and very ugly code. So what I'm REALLY trying to do is TWO "request scope" requests. One from the Servlet to the JSP, and one from the JSP to the Servlet. I could do this at the "session scope" of course. However, if the same Browser opens another window (or tab), they could easily trample all over the JavaBean used in the first window. I've tested this, and found this assumption to be accurate. I have come up with a possible solution that I'm coding up right now. When I create a new JavaBean in the Servlet, I name it based on a GUID (random number). Then, I attach this JavaBean to the session. Then, I attach the same exact same object to the request scope, but I don't use the GUID based name
,here I use an "unchanging name". When I get the the JSP, I populate the page based on the request scope reference to my object. When I submit that form, I have a hidden field that has the GUID name (which is a data member in the JavaBean). So back on the Servlet side again, I can easily determine which of the many JavaBeans in the session scope actually belong to this request. My one concern is that the session scope may become over populated if the user doesn't exit out of their page properly. So I'm going to add my own simple garbage collector that will kill very old JavaBeans (that match the class of course). Hope this is clear. Feel free to tell me I'm crazy!!!
