JSP to Servlet: Dynamically Binding Form Data to a Bean??

J

John Davison

Pardon me if this seems trivial, but I'm new to JSP/Servlets and I
haven't found the answer to problem on the net.

I have an input form that is quite large. I want to dynamically bind my
form data to a bean, then pass this bean to a servlet. I've found one
method that works, but it seems like a hack.

The form is calling itself. If the hidden parameter "submitted" is set,
then the JSP forwards to the desired servlet.

<jsp:useBean id="id" class="com.acme.IdentityBean" scope="request">
<jsp:setProperty name="id" property="*" />
</jsp:useBean>

<c:if test="${param.submitted}">
<jsp:forward page="MyServlet" />
</c:if>

<html>
<body>
<form action="index.jsp" method="post">

<input type="hidden" name="submitted" value="true" />

Social Security #:
<input name="ssn" type="text" value="<c:eek:ut value="${id.ssn}" />"/>

First Name:
<input name="firstName" type="text" value="<c:eek:ut
value="${id.firstName}" />"/>

<!-- Many more input fields. They have been removed for brevity.
-->

<input type="submit" value="Submit"/>
<input type="reset" value="Clear"/>
</form>
</body>
</html>

I have to post to this JSP in order for my jsp:setProperty to do the
work of binding all the input fields to the IdentityBean. Is this the
ideal pattern?

Any advice is appreciated.

~J.
 
S

Sikri

The above example might work but may be 'kludgy'.
But a suggested answer would be to use struts as the framework.
It will:
1. Map your form to a bean.
2. Also provide a controller servlet
For more information, please follow this link:
http://struts.apache.org.

- VS
 
V

Viv

I haven't find any utility that map the data to a bean, so i end up
with my own utility. Reflection mechanism can work it for u, if u have
that much of time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top