表单不会将任何值发布到servlet

时间:2015-11-30 16:11:34

标签: java forms servlets

我的表单没有向我的servlet发布任何值。 我已经为id尝试了其他值。这也不起作用。

它不断从表单中获取带有HTTP 500错误的空值

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
login.StoreVM.doPost(StoreVM.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是HTML代码:

                  <li>
                    <ul>
                        <li class="title"><h3>Lite</h3><h4>€100<small>/month</small></h4></li>
                        <li class="divider"></li>
                        <li>2 Core CPU</li>
                        <li>4GB RAM</li>
                        <li>50GB Disk Space</li>
                        <li>No Backup</li>
                        <li>8 AM to 5 PM Live Support</li>
                        <li><form method="post" action="storeVM">
                        <input type="text" id="vmstore" value="hello"/> 
                        <button type="submit" class="grey">Sign up now</button>
                        </form>
                 </ul>
             </li>

这是java servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    HttpSession session = request.getSession(false);

    //Make some variables and shorter things.
    int button = Integer.parseInt(request.getParameter("vmstore"));
    PrintWriter out = response.getWriter();;

    //Check if session is set.
    if(session == null || session.getAttribute("username") == null) {
        out.println("Please login first before ordering");
        RequestDispatcher rs = request.getRequestDispatcher("index.html");
        rs.include(request, response);
    } else {
        //Parse string to int.
        out.println(button);
        if(button == 1) {
            out.println("yeah");
        } else if (button == 2) {
            out.println("yeah2");
        } else if (button == 3) {
            out.println("yeah3");
        } else {
            out.println("Something went wrong. Please contact us");
            RequestDispatcher rs = request.getRequestDispatcher("index.html");
            rs.include(request, response);
    }

我不知道为什么它不起作用,因为所有其他形式都有效。

1 个答案:

答案 0 :(得分:1)

答案是将<input>更改为name =“vmstore”。

request.getParameter不接受id但只接受name。