[英]JSP form values passed to a servlet
I have a form in JSP in the following manner : 我在JSP中有以下形式的表单:
<form id="provision-field" method="post" action="${pageContext.request.contextPath}/myServlet">
<fieldset>
<ol class="fields">
<li>
<label for="field1">field1</label>
<input type="text" id="field1" "
value="<%= field1 %>"
/>
<span class="description">
<span class="optional">Optional</span>
</span>
</li>
</ol>
</fieldset>
<div class="actions">
<button type="submit" name="Submit">
Submit form
</button>
<a href="" class="close-dialog">Cancel</a>
</div>
</form>
I have a js snippet on click of the submit button does the following 单击提交按钮时,我有一个js代码段执行以下操作
var field = document.getElementById("field1").value;
$.ajax({
url: '${pageContext.request.contextPath}/myServlet'
type: 'POST',
data: field,
dataType: "html",
success: function(html) {
alert("Success");
},
error: function(error){
alert("ERROR");
}
});
When I just use the form element (ie take out the js code) , I can reach my servlet but none of my form parameters are passed . 当我只使用form元素(即取出js代码)时,可以到达servlet,但是没有传递任何form参数。 when I try using the js code , the ajax request does not work . 当我尝试使用js代码时,ajax请求不起作用。 could someone point to me how this should be correctly done . 有人可以向我指出应该如何正确做到。
The servlet code is : Servlet代码为:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("Inside the post function");
logger.info(request.getParameter("data");
}
var field = document.getElementById("field1").value;
$.ajax({
url: '${pageContext.request.contextPath}/myServlet'
type: 'POST',
data: {
data :field
},
dataType: "html",
success: function(html) {
alert("Success");
},
error: function(error){
alert("ERROR");
}
});
Inside servelt following code in doPost method : Assuming that you have primary knowledge of HttpServlet... 在doPost方法中的servelt下面的代码内:假设您具有HttpServlet的基本知识...
request.getParameter("data");
I am sharing small Ajax with Servlet tutorial , which may help you for further problem... Download Link- AJAX Servlet Tutorial 我正在与Servlet教程共享小型Ajax,这可能会帮助您解决进一步的问题... 下载链接-AJAX Servlet教程
data: { field1:field1Value }
send like this data: { field1:field1Value }
发送如下
and then access request.getParameter("field1");
然后访问request.getParameter("field1");
in servlet 在servlet中
由于表单提交方法是post method="post"
,因此您需要确保在doPost(request, response)
方法中获取请求值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.