Servlet获取隐藏表单字段的参数和空值

时间:2014-01-04 00:04:50

标签: java servlets servlet-3.0

我有类似下面的表单字段

<form action="test" name="formtest" method="post">
<input type="hidden" name="testid" value ="12345">
</form>

现在在servlet中我做了类似

的事情
int testid = Integer.parseInt(request.getParameter("testid"));

我得到Null指针异常,不知怎的,我相信即使这些形式有价值,它也不会被拿到servlet。

我没有想法,需要第二眼看看我错过了什么。我的表单已启用,并且值由jquery动态填充。但是我测试使用静态值来删除任何jquery问题提供的值。但仍然没有快乐。

因为我试图解析空值,所以我得到了异常。

java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:415)
    at java.lang.Integer.parseInt(Integer.java:497)
    at test.jobs.viewaddtojobs.processRequest(viewaddtojobs.java:41)
    at test.jobs.viewaddtojobs.doPost(viewaddtojobs.java:149)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at listener.trimresponse.doFilter(trimresponse.java:45)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

jsp表格

<form id="id-frm-gg" class="cl-frm-gg" action="JobsAdd" method="post" name="frmgg" enctype="multipart/form-data">
                <label class="cl-ilab">Your Job will be posted in Section:&nbsp;&nbsp;<span id="sp-jobs-category" class="cl-jobs"><%= request.getAttribute("selectedjob")%></span></label><span id="id-error-sc" class="cl-info"></span><br><br`enter code here`>
                <input type="hidden" name="subcategoryid" value="12001">
                <input type="hidden" name="subcategory" value="<%= request.getAttribute("selectedjob")%>">
                <label id="id-status-info" class="cl-ilab">This Job Requirement is posted from :&nbsp;<span id="id-error-auto-status" class="cl-info"></span></label>
                <p id="id-status-check" class="cl-ch">
                    <label class="cl-ilab">
                        <input type="radio" name="status" value="business-owner" id="status_0">
                        Direct Business Owner</label>  
                    <label class="cl-ilab">
                        <input type="radio" name="status" value="recruiter" id="status_1">
                        Recruiter</label>
                    <br>
                </p>
                <br>
                <label id="id-job-status-info" class="cl-ilab">This Job is :&nbsp;<span id="id-error-job-status" class="cl-info"></span></label>
                <p id="id-job-status" class="cl-ch">
                    <label class="cl-ilab">
                        <input type="radio" name="jobstatus" value="contract" id="jobstatus_0">
                        Contract</label>  
                    <label class="cl-ilab">
                        <input type="radio" name="jobstatus" value="part-time" id="jobstatus_1">
                        Part-time</label>
                    <label class="cl-ilab">
                        <input type="radio" name="jobstatus" value="internship" id="jobstatus_2">
                        Internship</label>
                    <label class="cl-ilab">
                        <input type="radio" name="jobstatus" value="non-profit" id="jobstatus_3">
                        Non Profit</label>
                    <label class="cl-ilab">
                        <input type="radio" name="jobstatus" value="remote" id="jobstatus_4">
                        Remote</label>
                    <label class="cl-ilab">
                        <input type="radio" name="jobstatus" value="fulltime" id="jobstatus_5">
                        Full-time </label>
                    <br>
                </p>
                <br>
                <label class="cl-ilab">Designation:</label>
                <input name="txtdesignation" type="text" class="cl-txt" id="id-txtdesignation" tabindex="4" title="Enter the designation details for job offered" size="55" maxlength="100"><span id="id-sinfo" class="cl-info"></span>&nbsp;&nbsp;&nbsp;&nbsp;<label class="cl-ilab">Job Keywords:</label>
                <input name="txtkeywords" type="text" class="cl-txt" id="id-txt" tabindex="5" title="Please enter job keywords" size="50" maxlength="100"><span id="id-nsinfo" class="cl-info"></span>
                <br><br>
                <label class="cl-ilab">Experiences(in Months):</label>
                <input name="txtexperience" type="text" class="cl-txtexperience" id="id-txtexperience" tabindex="5" title="Enter the experience required for this Job" size="5" maxlength="10">&nbsp;<span id="id-error-experience" class="cl-info"></span>
                &nbsp;
                <label class="cl-ilab">Job Location:</label>
                <input name="txtlocation" type="text" class="cl-txt" id="id-txtlocation" tabindex="4" title="Please enter the job location details for job offered" size="55" maxlength="100"><span id="id-job-location-info" class="cl-info"></span>&nbsp;&nbsp;&nbsp;&nbsp;
                <label class="cl-ilab">
                    <input type="radio" name="telecommute" value="telecommute" id="id-telecommute" title="Please check if telecommute is ok for this job">
                    Telecommute Ok ?</label>
                <br><br>
                <label class="ilab">Contact Email:</label>
                <input name="txtemail" type="text" class="cl-txtemail" id="id-txtemail" tabindex="7" title="Please enter your valid email id to contact you with regard to this Job" size="40">&nbsp;<span id="id-emailinfo" class="cl-info"></span>
                &nbsp;&nbsp;
                <label class="ilab">Contact Phone:</label>
                <input name="txtphone" type="text" class="cl-txtphone" id="id-txtphone" tabindex="8" title="Please enter your valid Phone number to talk about this Job" size="10" maxlength="15">
                &nbsp;<span id="id-phoneinfo" class="cl-info"></span>
                &nbsp;&nbsp;
                <label class="cl-slab">Industry Type: </label> 
                <select name="txtindustrytype" style="width:160px;" class="cl-select">  
                    <option value="Select">Select Country</option>
                    <option value="USA" selected>United States</option>
                </select>&nbsp;<span id="id-industrytypeinfo" class="cl-info"></span>
                <br><br>
                <label class="cl-ilab">Desired Education:</label>
                <input name="txteducation" type="text" class="cl-txt" id="id-txteducation" tabindex="4" title="Please enter the desired education details for Job Fitment" size="55" maxlength="100"><span id="id-education-info" class="cl-info"></span>&nbsp;&nbsp;&nbsp;&nbsp;
                <br><br>
                <label class="ilab">Title:</label>
                <input name="txttitle" type="text" class="cl-txttitle" id="id-txttitle" tabindex="8" maxlength="100" title="Please enter the title of this Job requirement in 100 words" size="150">
                &nbsp;<span id="id-titleinfo" class="cl-info"></span>
                <p id="id-title-max" class="cl-title-max">Max characters accepted in Title field is 100.Characters Left:<span id="id-c" class="charsLeft"><b>100</b></span></p>
                <label class="ilab">Job Description:&nbsp;<span id="id-desinfo" class="cl-info"></span></label><br>
                <textarea name="txtdescription" cols="100" rows="20" class="cl-desc" id="id-desc" title="Please enter the jobdescription"></textarea>
                <br>
                <label class="ilab">Desired Job Skills:&nbsp;<span id="id-jobskillsinfo" class="cl-info"></span></label><br>
                <textarea name="txtjobskills" cols="100" rows="20" class="cl-desc" id="id-jobskills" title="Please enter the desired skillsets of your job requirement"></textarea>
                <br>
                <label class="ilab">Job Benefits Offered:&nbsp;<span id="id-jobbenefitinfo" class="cl-info"></span></label><br>
                <textarea name="txtjobbenefits" cols="100" rows="20" class="cl-desc" id="id-jobbenefits" title="Please enter the Job benefits if any offered"></textarea>
                <br>
                <p id="id-tc" class="cl-tc-info">By Posting in this Site,We assume that you agree to our <a href="www.google.com" title="Terms and Conditions">Terms and Conditions</a>.</p><br>
                <input name="Submit" type="submit" class="cl-frm" id="id-submit" tabindex="10" title="Submit" value="Submit">
                <input name="Cancel" type="reset" class="cl-frm" id="id-cancel" tabindex="12" title="Cancel" value="Cancel">
                <br><br>
            </form>

的servlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        java.sql.Timestamp sqlDateTime = new java.sql.Timestamp(new java.util.Date().getTime());
        PrintWriter out = response.getWriter();
        bnaddtojobsFormbn jobsForm = new bnaddtojobsFormbn();
        try {
          int subcategoryid = Integer.parseInt(request.getParameter("subcategoryid"));
          System.out.println("subcategoryid = " + subcategoryid);
          jobsForm.setSubcategoryid(subcategoryid);
          String subcategory = request.getParameter("subcategory");
          jobsForm.setSubcategory(subcategory);
           String status = request.getParameter("status");
           jobsForm.setPosterinfo(status);
           String jobstatus = request.getParameter("jobstatus");
           jobsForm.setJobtype(jobstatus);
           String txtdesignation = request.getParameter("txtdesignation");
           jobsForm.setDesignation(txtdesignation);
           String txtkeywords = request.getParameter("txtkeywords");
           jobsForm.setKeywords(txtkeywords);
           String txtexperience = request.getParameter("experience");
           jobsForm.setExperience(txtexperience);
           String txtlocation = request.getParameter("txtlocation");
           jobsForm.setLocation(txtlocation);
           String telecommute = request.getParameter("telecommute");
           jobsForm.setTelecommute(telecommute);
           String txtemail = request.getParameter("txtemail");
           jobsForm.setEmail(txtemail);
           String txtphone = request.getParameter("txtphone");
           jobsForm.setContact(jobstatus);
           String txtindustrytype = request.getParameter("txtindustrytype");

0 个答案:

没有答案