0

I have a few values at JSP file, and I there is a problem with pushing radio button to the Servlet and outputting it.

The input file is in this jsp:

<label for="phone">Phone: </label>
    <input type="text" name="phone" id="phone" value="${phone}"><br/><br/>

    <input type="radio" name="sex" id="sex" value="${male}">Male
    <input type="radio" name="sex" id="sex" value="${female}"/>Female<br/><br/>

And Servler is here: public static RequestNewEmployee fromRequestParameters(HttpServletRequest request) { return new RequestNewEmployee( request.getParameter("firstName"), request.getParameter("lastName"), request.getParameter("email"), request.getParameter("phone"), request.getParameter("sex")); } public void setAsRequestAttributes(HttpServletRequest request) { request.setAttribute("firstName", firstName); request.setAttribute("lastName", lastName); request.setAttribute("email", email); request.setAttribute("phone", phone); request.setAttribute("sex", sex); }

However, I don't receive any praramentr at sex field( throught all others work properly). What might be an issue?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vlad Demyan
  • 363
  • 1
  • 4
  • 16

1 Answers1

-1

Theory
${} is called EL expression. Inside you can have variables. From the oracle tutorial:

The web container evaluates a variable that appears in an expression by looking up its value according to the behavior of PageContext.findAttribute(String). For example, when evaluating the expression ${product}, the container will look for product in the page, request, session, and application scopes and will return its value. If product is not found, null is returned. A variable that matches one of the implicit objects described in Implicit Objects will return that implicit object instead of the variable's value.


Now lets talk about your problem.

  1. You can't have two html tags with identical ids.
  2. You receive empty parameter because, obviously, it's empty(undefined). Where did you define male and female variables?

I think this is what you're trying to achieve:

form.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/formServlet">
        <input type="radio" name="sex" id="sexChoice1" value="male">Male
        <input type="radio" name="sex" id="sexChoice2" value="female"/>Female<br/><br/>
        <input type="submit" value="submit">
    </form>
</body>
</html>

FormServlet

public class FormServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println(req.getParameter("sex"));
    }
}

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>FormServlet</servlet-name>
        <servlet-class>com.artmal.controller.FormServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>FormServlet</servlet-name>
        <url-pattern>/formServlet</url-pattern>
    </servlet-mapping>
</web-app>

Console output after selecting 'male' option and submitting:

male

Artem Malchenko
  • 2,320
  • 1
  • 18
  • 39