Reputation: 127
Could you help me to come up with solution. There are JSP-page which sends form parameters to servlet. Usually I parse parameters by HttpServletRequest.getParameter() which works fine for forms with tiny parameter numbers. Now I'm developing application which has a lot of JSPs with number of parameters and the standard way of form processing is inconvenient. I think that possible solution might be by using -action. I don't understand whether it works for me. I browsed a lot of materials but find nothing about it. I mean that there is any information regarding possibility to get form parameters in jsp by , automatically create instance of the entity class, map all the parameters to entity-properties and send the entity-instance to the servlet. Please take a look at the code:
index.jsp
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="NewFormServlet" enctype="application/x-www-form-urlencoded">
<jsp:useBean id="client-bean" class="model.entity.Client" scope="request"/>
<h3>Please enter client information</h3><br>
Client first name<input type="text" name="first-name"/><br>
<jsp:setProperty name="client-bean" property="firstName" value="${requestScope.first-name}"/>
Client last name<input type="text" name="last-name"/><br>
<jsp:setProperty name="client-bean" property="lastName" value="${requestScope.last-name}"/>
Client address<input type="text" name="address" size="100"/><br>
<jsp:setProperty name="client-bean" property="address" value="${requestScope.address}"/>
Client city<input type="text" name="city"/><br>
<jsp:setProperty name="client-bean" property="city" param="${requestScope.city}"/>
Client postal code<input type="text" name="postal-code"><br>
<jsp:setProperty name="client-bean" property="postalCode" value="${requestScope.postal-code}"/>
<input type="hidden" name="jsp-identifier" value="client-form">
<input type="submit" value="Submit">
</form>
</body>
</html>
What is incorrect in this code? Thank you in advance.
Upvotes: 0
Views: 1216
Reputation: 149075
You should first think about what occurs on server and what occurs in browser, as well as what is transmitted via HTTP. A form submission uses many phases :
HttpServletRequest
reflecting current HTTP request, and a HttpServletResponse
to prepare what will be sent back to browser after processingAll that means that anything you can do to request attributes in the JSP part will be lost at the time of processing of the submitted form by the servlet. You can only rely on session attributes, and on input form fields that will be accessible as request parameters.
So with your current JSP, the Servlet will find nothing in request attributes (it is a different HttpServletRequest) and will only be able to use parameters with names firstName
, lastName
, address
, city
, etc.
I can understand it is not the expected answer, but HTTP protocol is like that ...
EDIT per comment :
You can put the attribute in session, and then the servlet will use the same session as the JSP. But read again what I wrote above and think when things happen :
Client
bean that you put in session scope, and use its value to initialize the form fields. Stop for the server partClient
bean has null valuesI'm sorry but there's not enough magic for the server to automatically find in its attributes (either request or session) what comes from form submission : it only exists in request parameters, and it is the servlet job to process them and eventually put them in attributes.
Upvotes: 2
Reputation: 38320
Edit: It appears that jsp:useBean is an old school way to collect up a group of parameter values for easier display on a page. It does not add an attribute when the request is posted.
Based on that, I see little value in the jsp:useBean tag, since you can use el expressions to access attributes that you set in a servlet.
This does not help you get the posted parameter values into a bean in the servlet.
You can write a method on the bean to extract the parameter values from the request (visitor pattern). For example:
class bean
{
private String value;
public void loadFromHttpServletRequest(final HttpServletRequest request)
{
value = request.getParameter("value");
}
}
Consider using a package like spring-mvc.
Upvotes: 1