我正在尝试使用$ .post方法从html文件提交表单数据。但是,在服务器端,servlet变为null。没有字段为空且为空。谁能告诉我这是什么问题?谢谢。顺便说一下,我在这里只显示了一个字段,但实际上我在表单中有几个字段。这是我的代码:
html
<form id="reservationsForm" name="reservationsForm" method="post" action="Reservations"
class="well form-horizontal" align="left" style="width:865px;border:2px solid #000000;margin:auto;">
<br>
<div >
<div align="left" class="form-group">
<label class="col-md-2 control-label" style="font-family:sans-serif;color:black"><B>Pickup Date:</B></label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<input placeholder="Please select a date" class="form-control" type="text" id="datepicker" style="width:
238px;">
</div>
</div>
</div>
<br>
<td><label align="center" style="font-family:sans-serif;color:black"># of Stops</label></td>
<select class="form-control selectpicker" id="stops" name="stops" size="1" tabindex="14" width="150px">
<option value="0">No Stop</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<div align="center">
<input onclick="send()" type="submit" name="submit" value="Send" style="font-weight:600" class="btn btn-primary">
</div>
</form>
JS
var send = function() {
var pickUpDate = document.getElementById('datepicker').value;
var totalStops = document.getElementById('stops').value;
var inputData = {"pickUpDate":pickUpDate,
"totalStops":totalStops }
$.post('http://localhost:8080/WelcomeLimoUsa/Reservations', inputData, myData, "json");
function myData(data) {
}
};
的servlet
@WebServlet("/Reservations")
public class Reservations extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
if( request != null) {
System.out.println("PickUp Date is: " +
request.getParameter("pickUpDate"));
System.out.println("total Stops is: " + request.getParameter("totalStops"));
String pickUpDate = request.getParameter("pickUpDate");
Integer totalStops = Integer.parseInt(request.getParameter("totalStops"));
}else{
System.out.println("#############Request is null#########");
}
}
catch (Exception e){
e.printStackTrace();
System.out.println("Exception occured");
}
}
}
服务器端错误
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at com.welcomelimo.controller.Reservations.doPost(Reservations.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at com.welcomelimo.controller.Reservations.doPost(Reservations.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Exception occured
PickUp Date is: null
答案 0 :(得分:0)
尝试在客户端进行调试。在Javascript中:
git reflog
EDIT1:确认预期值后 使用以下命令检查所有servlet的请求参数:
var totalStops = document.getElementById('stops').value;
// look if totalStops contains the expected value
console.log(totalStops);
var inputData = {"pickUpDate":pickUpDate,
"totalStops":totalStops }
EDIT2:看到服务器没有收到参数后。
正如我建议你可以显示你在servlet中收到的所有参数。我尝试使用您的代码并获取:Enumeration<String> names = request.getParameterNames();
while(names.hasMoreElements()) {
String name = names.nextElement();
response.getWriter().append(name + "=" + request.getParameter(name) + "<br>");
}
所以表单已提交,但没有ajax ajax代码没有停止默认的提交事件。
删除stops=2<br>submit=Send<br>
并让该行只是:
onclick="send()"
您可以通过以下方式将表单提交与javascript绑定:
<input type="submit" name="submit" value="Send" style="font-weight:600" class="btn btn-primary">
要阻止默认提交事件,请在send()函数末尾添加// when the html document is ready / parsed from browser
$( document ).ready(function() {
// Bind an event handler to the "submit" JavaScript event
$("#reservationsForm").submit(send)
});
- Javasript中的函数:
return false;
您可以在jQuery.submit()
的文档中看到...我们可以通过调用.preventDefault()来取消提交操作 事件对象或从我们的处理程序返回false。