LordScrat
LordScrat

Reputation: 180

JSP get value of a html type=number input field

So, I have to make a kind of offering tool as a homework to learn JSP, and the fundamental is that the client goes on the index site and inputs his data, and then he gets redirected to a second page that shows him the full offer with everything calculated.

The form looks like this:

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

I'm only fiddling around with the area property for testings sake. The plan is to set the value of said input to my JavaBean, but the request.getParameter("area"); simply remains null even if I'm giving it a default value using htmls value="10". I've already found out that the input needs to be in a form to work (duh), but thats about it. The problem is not my JavaBean or anything like this, because the normal textfield (Name and Address fields) work perfectly fine and are shown correctly on the next page.

Hope someone can help me.

Yours sincerely,
LordScrat

Upvotes: 0

Views: 6174

Answers (1)

Umesh Kumar Sharma
Umesh Kumar Sharma

Reputation: 306

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

Upvotes: 1

Related Questions