英文:
Why does difference in name attribute of an HTML element affect variable value in a Servlet?
问题
我试图学习Servlets,遇到了一个问题。当我在HTML元素的name属性中使用破折号或下划线时,Servlet接收到的值为null。但是,如果我不使用下划线或破折号,Servlet会接收到输入的值。
第一个代码片段正常工作,结果可以在这里看到 第一个代码片段的输出,而第二个代码片段在按下“提交”按钮时不返回任何值,如此图片所示 第二个代码片段的输出。
为什么会发生这种情况?
编辑
我可能应该提到我还在使用一个AJAX函数:
function getNetSalary() {
$("#submitButton").click(function(){
$.ajax({
url:'someservlet',
data: {
labourUnionFee: $('#labour-union-fee').val()
},
type: 'POST',
cache: false,
success: function(data){
$('#net-salary-information-div').text(data);
},
error: function(){
alert('Please Fill all necessary fields');
}
});
});
}
而这是我的someservlet
中的代码:
String labourUnionFee = request.getParameter("labour-union-fee");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(labourUnionFee);
英文:
I'm trying to learn Servlets and I came across an issue. When I use dashes or underscores in name attribute of an HTML element the value that servlet receives is null. However, If I don't use underscores or dashes, the servlet receives the entered value.
<input type="text" name="labourUnionFee" id="labour-union-fee" value="0">
String labourUnionFee = request.getParameter("labourUnionFee");
works fine and the result can be seen from here output of the first snippet, whereas
<input type="text" name="labour-union-fee" id="labour-union-fee" value="0">
String labourUnionFee = request.getParameter("labour-union-fee");
does not return any value when 'submit' button is pressed as this picture demonstrates output of the second snippet
Why does this occur?
EDIT
I probably should have mentioned that I am also using an AJAX function:
function getNetSalary() {
$("#submitButton").click(function(){
$.ajax({
url:'someservlet',
data: {
labourUnionFee: $('#labour-union-fee').val()
},
type: 'POST',
cache: false,
success: function(data){
$('#net-salary-information-div').text(data);
},
error: function(){
alert('Please Fill all necessary fields');
}
}
);
}
);
And this is inside my 'someservlet':
String labourUnionFee = request.getParameter("labour-union-fee");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(labourUnionFee);
专注分享java语言的经验与见解,让所有开发者获益!
评论