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

时间:2018-03-13 23:19:53

标签: java html jsp javabeans jsp-tags

所以,我必须提供一种提供工具作为学习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,但即使我使用htmls request.getParameter("area");给它一个默认值,null仍然只是value="10"。我已经发现输入需要处于一种工作形式(duh),但那就是它。问题不在于我的JavaBean或类似的东西,因为普通的文本字段(名称和地址字段)工作得很好并且在下一页上正确显示。

希望有人可以帮助我。

您诚挚的,
LordScrat

1 个答案:

答案 0 :(得分:1)

    **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>