aime
aime

Reputation: 127

How to process form by <jsp:setBean> and send it to servlet?

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

Answers (2)

Serge Ballesta
Serge Ballesta

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 :

  1. on server : the JSP is executed using servlet context, session, and request attributes, with still full access at the previous request (parameters, ...) => all that generates a HTML page (with eventually css or javascript linked or included)
  2. on browser : the browser gets and parses the HTML page, optionnaly gets linked resources (images, etc.), and display the form to the user
  3. on browser : the user fills the input fields of the form and clicks the input button
  4. on browser : the browser collates data form input fields, generate an new HTTP request (usually a POST one) and sends it to server
  5. on server : the servlet container pre-processes the request (until that is is only a stream of bytes conforming to HTTP protocol) and calls the appropriate servlet method with a new HttpServletRequest reflecting current HTTP request, and a HttpServletResponse to prepare what will be sent back to browser after processing

All 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 :

  • on server, when executing the JSP, you create an empty Client bean that you put in session scope, and use its value to initialize the form fields. Stop for the server part
  • on client, user fills the input fields - the server knows nothing on that - and submit the form through a new request
  • on server, the servlet has the values in request parameters, but the session still contains the previous values and so the Client bean has null values

I'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

DwB
DwB

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

Related Questions