【发布时间】:2019-11-22 07:13:04
【问题描述】:
我正在创建一个用于学习目的的小型 Web 应用程序。这里我使用 Java、Jquery、Ajax。我想在我的项目中添加会话。我没有在 JSP 中使用表单(请求不是通过表单提交)。我在这里更新了我的代码。谁能给我一个使用 Jquery Ajax 请求向我的应用程序添加会话的解决方案。
$("#submit").click(function(){
var username=document.getElementById("uname").value;
var password=document.getElementById("psw").value;
if (username=="" || username == null || password == ""|| password == null){
alert("Please input values");
document.getElementById("username").focus();
} else {
var method;
$.ajax({
type : 'post',
url : 'userservlet',
data : {
username : username,
password : password,
method : "login",
},
success: function (responseText) {
if (responseText.success){
}
else {
alert(responseText.error);
}
}
});
}
});
登录
我使用表单提交时的后端
public String loginuser(HttpServletRequest request, HttpServletResponse response, String username, String password) throws IOException {
HttpSession httpSession = request.getSession();
UserDAOImpl userdaoimpl = new UserDAOImpl();
String sts=null;
try {
sts = userdaoimpl.loginUser(username,password);
httpSession.setAttribute("username", username);
httpSession.setAttribute("password", password);
//httpSession.setMaxInactiveInterval(60);
} catch (Exception e) {
e.printStackTrace();
}
return sts;
}
【问题讨论】: