Java – How to get dropdown value using jsp:useBean and jsp:setProperty

javajsp

I have a rather simple form in JSP that looks like this:

<form action="response.jsp" method="POST">
                        <label>First Name:</label><input type="text" name="firstName" /><br>
                        <label>Last Name:</label><input type="text" name="lastName" /><br>
                        <label>Email:</label><input type="text" name="email" /><br>
                        <label>Re-enter Email:</label><input type="text" name="emailRe" /><br>
                        <label>Address:</label><input type="text" name="address" /><br>
                        <label>Address 2:</label><input type="text" name="address2" /><br>
                        <label>City:</label><input type="text" name="city" /><br>
                        <label>Country:</label>
                        <select name="country">
                            <option value="0">--Country--</option>
                            <option value="1">United States</option>
                            <option value="2">Canada</option>
                            <option value="3">Mexico</option>
                        </select><br>
                        <label>Phone:</label><input type="text" name="phone" /><br>
                        <label>Alt Phone:</label><input type="text" name="phoneAlt" /><br>
                        <input type="submit" value="submit" />
                    </form>

But when I try and access the value of the select box in my Java class I get null. Ive tried reading it in as a String and an Array of strings neither though seems to be grabbing the right value.

The response.jsp looks like this:

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%!
%>
<jsp:useBean id="formHandler" class="validation.RegHandler" scope="request">
    <jsp:setProperty name="formHandler" property="*" />
</jsp:useBean>
<%
            if (formHandler.validate()) {
%>
<jsp:forward page="success.jsp"/>
<%
            }
            else
            {
%>
<jsp:forward page="retryReg.jsp"/>
<%            }
%>

I already have Java script validation in place but I wanted to make sure I covered validation and checking for non-JS users.

The RegHandler just uses the name field to refer to the value in the form.

Any Idea how I could access the select box's value?

Best Solution

Works at my machine.

package mypackage;

public class Bean {
    private String country;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

with

<%@page pageEncoding="UTF-8"%>

<jsp:useBean id="bean" class="mypackage.Bean" scope="request">
    <jsp:setProperty name="bean" property="*" />
</jsp:useBean>

<!doctype html>
<html lang="en">
    <head><title>SO question 2814402</title></head>
    <body>
        <form>
            <select name="country">
                <option value="1">one</option>
                <option value="2">two</option>
                <option value="3">three</option>
            </select>
            <input type="submit">
            <p>Selected country: ${bean.country}
        </form>
    </body>
</html>

Maybe the setter is bogus? Did you check the server logs?

By the way, have you considered servlets?