JSP 获取 html type=number 输入字段的值

JSP get value of a html type=number input field

所以,我必须做一种提供工具作为学习的功课JSP,基本原理是客户进入索引站点并输入他的数据,然后他被重定向到第二页向他展示了所有计算出的完整报价。

表单如下所示:

<form method="POST" action="offer.jsp">
    <table>
        <tr>
            <td>Name:</td><td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>Addresse:</td><td><input type="text" name="address"></td>
        </tr>
        <tr>
            <td>Boden in m²:</td><td><input type="number" name="area" min="1" value="10"></td>
        </tr>
        <tr>
            <td>Anzahl der R&auml;ume mit bis zu 15m²:</td><td><input type="number" name="rooms_15" min="0"</td>
        </tr>
        <tr>
            <td>Anzahl der R&auml;ume mit bis zu 25m²:</td><td><input type="number" name="rooms_25" min="0"></td>
        </tr>
        <tr>
            <td>Anzahl der R&auml;ume mit bis zu 40m²:</td><td><input type="number" name="rooms_40" min="0"></td>
        </tr>
        <tr>
            <td>Anzahl der WC-R&auml;ume:</td><td><input type="number" name="toiletrooms" min="0"></td>
        </tr>
        <tr>
            <td>Anzahl der WCs insgesamt:</td><td><input type="number" name="toilets" min="0"></td>
        </tr>
        <tr>
            <td>Anzahl der B&auml;der:</td><td><input type="number" name="bathrooms" min="0"></td>
        </tr>
        <tr>
            <td>Anzahl der K&uuml;chen:</td><td><input type="number" name="kitchens" min="0"></td>
        </tr>
    </table>
    <br>
    <input type="submit" value="Berechnen" onclick="<%
        offer.setName(request.getParameter("clientname"));
        offer.setAddress(request.getParameter("address"));
        System.out.print("\n\n\n"+request.getParameter("area")+"\n\n\n");       // S.O.P. for testing reasons
//                    offer.setArea(request.getParameter("area"));
       %>; location.href = 'offer.jsp'">
</form>

我只是为了测试而摆弄 属性 区域。计划是将所述输入的值设置为我的 JavaBean,但是 request.getParameter("area"); 只是保持 null,即使我使用 htmls value="10" 给它一个默认值。我已经发现输入需要以某种形式工作(duh),但仅此而已。问题不是我的 JavaBean 或类似的东西,因为正常的文本字段(名称和地址字段)工作得很好并且在下一页上正确显示。

希望有人能帮助我。

此致,
LordScrat

    **MVC Architecture with JSP and Servlet**

    **Model**

    public class StudentBean {

        private int rno;
        private String name;
        private float fee;

        public StudentBean() {

        }

        public int getRno() {
            return rno;
        }

        public void setRno(int rno) {
            this.rno = rno;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public float getFee() {
            return fee;
        }

        public void setFee(float fee) {
            this.fee = fee;
        }

        @Override
        public String toString() {
            return "StudentBean [rno=" + rno + ", name=" + name + ", fee=" + fee + "]";
        }

    }

    **View**

        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
            pageEncoding="ISO-8859-1"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        </head>
        <body>

            <form  method="get" action="StudentController">

                Rno: <input type="text" name="rno"/> <br/>
                Name: <input type="text" name="name"/> <br/>
                Fee: <input type="text" name="fee"/> <br/>
                <input type="submit" name="btnSubmit" value="Submit"/>
            </form>
        </body>
        </html>

    **Controller**

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


@WebServlet("/StudentController")
public class StudentController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        StudentBean studentBean = new StudentBean();
        studentBean.setRno(Integer.parseInt(request.getParameter("rno")));
        studentBean.setName(request.getParameter("name"));
        studentBean.setFee(Float.valueOf(request.getParameter("fee")));


        System.out.println(studentBean);

        request.getSession().setAttribute("studentObject", studentBean);
        request.getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
    }

}

**another page, where bean is being used.**

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="ISO-8859-1"%>
<%@ page import="com.loardscrat.controller.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>Hello</h1>
<%
    StudentBean studentBean = (StudentBean) request.getSession().getAttribute("studentObject");
    out.print(studentBean.getRno());
    out.print(studentBean.getName());
    out.print(studentBean.getFee());

%>
</body>
</html>