我有html登录页面,当我的用户名和密码匹配时 我获取RequestDispatcher对象并前进至JSP页面并显示记录列表,我在JSP页面中具有按钮和文本字段,因此我需要通过ajax servlet添加更多记录,我得到空值
list-of-records.jsp
<script>
$(document).ready(function() {
$('#btn').click(function() {
$.get('login', function(responseText) {
$('#somediv').text(responseText);
location.reload();
});
});
});
$("#btn").click(function() {
var name = $('#name').val();
$.ajax({
url : 'login',
data : {
name : name
},
type : 'get',
dataType : "text",
success : function(data) {
},
error : function() {
alert('error');
}
});
});
</script>
<div style="width: 70%; margin: 0 auto;">
<input type="text" name="name">
<button id="btn" class="button">press here</button>
</div>
当我从get中获得价值时,我得到了空值
@WebServlet(“ / login”)
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
// here name is null !!
try {
DaoFactory.getDao().insertIntoCourses(name);
} catch (DaoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我从登录servlet进行操作,然后从同一个登录servlet获取requset
doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher dispatcher = req.getRequestDispatcher("/list-of-records.jsp");
dispatcher.forward(req, resp);