无法在Servlet中将Int转换为字符串

问题描述 投票:0回答:1

im试图制作一种用于插入数据的方法,但是解析值时出错。此错误似乎正在处理中,在我的insert方法中将int转换为字符串,并显示此错误:

type Exception report

message null

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NumberFormatException: null
    java.lang.Integer.parseInt(Integer.java:542)
    java.lang.Integer.parseInt(Integer.java:615)
    servlet.JobServlet.insert(JobServlet.java:137)
    servlet.JobServlet.doGet(JobServlet.java:85)
    servlet.JobServlet.doPost(JobServlet.java:123)
    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)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.

帮助我修复它。

这是我在servlet上的插入方法:

private void insert(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        String id = request.getParameter("idJob");
        String title = request.getParameter("titleJob");
        int min = Integer.parseInt(request.getParameter("minSalary"));
        int max = Integer.parseInt(request.getParameter("maxSalary"));
        dao.save(new Job(id, title, min, max));
        response.sendRedirect("jobServlet?action=list");
    }

和这是我的jsp代码:

    <div class="card-body card-block">
                                    <form action="jobServlet?action=insert" method="post" class="form-vertical">
                                        <div class="row form-group">
                                            <div class="col-12 col-md-9">
                                                <input type="text" id="idJob" name="idJob" placeholder="Enter Job Id..." class="form-control">
                                            </div>
                                            <div class="col-12 col-md-9">
                                                <input type="text" id="titleJob" name="titleJob" placeholder="Enter Job Title..." class="form-control">
                                            </div>
                                            <div class="col-12 col-md-9">
                                                <input type="text" id="minSalary" name="minSalary" placeholder="Enter Minimal Salary..." class="form-control">
                                            </div>
                                            <div class="col-12 col-md-9">
                                                <input type="text" id="maxSalary" name="maxSalary" placeholder="Enter Maximal Salary..." class="form-control">
                                            </div>
                                        </div>
                                        <input type="submit" name="submit" value="Save" class="btn btn-primary btn-sm"/>

我尝试了这个,但还是没用:

private void insert(HttpServletRequest request, HttpServletResponse response)
                throws SQLException, IOException {
            String id = request.getParameter("idJob");
            String title = request.getParameter("titleJob");
            String min = request.getParameter("minSalary");
            String max = request.getParameter("maxSalary");
            dao.save(new Job(id, title, Integer.parseInt(min),Integer.parseInt(max)));
            response.sendRedirect("jobServlet?action=list");
        }
java html jsp servlets
1个回答
0
投票

检查您的请求(如果使用浏览器,可以轻松完成),并查看是否传递了maxSalary和minSalary。其中之一为空。

此外,如果您使用的是html5,则无需使用文本作为输入类型。只需使用数字,您将获得一个整数。

此外,无论您在哪里使用Integer.parseInt(),都会捕获数字格式异常。

© www.soinside.com 2019 - 2024. All rights reserved.