利用ajax实现登录:验证完用户信息后如何保存用户信息并实现跳转
发布时间:2020-12-15 21:55:27 所属栏目:百科 来源:网络整理
导读:代码如下: 前台: $.ajax({url : '../servlet/Login_Do',data : {name : $('#loginForm input[name=name]').val(),password : $('#loginForm input[name=password]').val()},dataType : 'json',success : function(data) {if (data.msg == null) {alert("用
|
代码如下: 前台: $.ajax({
url : '../servlet/Login_Do',data : {
name : $('#loginForm input[name=name]').val(),password : $('#loginForm input[name=password]').val()
},dataType : 'json',success : function(data) {
if (data.msg == null) {
alert("用户名密码错误");
} else {
loginDialog.dialog('close');
window.location.href ='Panel.jsp';
}
},error : function() {
alert("失败");
}
});
后台:(在验证信息的servlet中直接保存session,然后在跳转的新的页面可以直接session.getAttribute()) public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
name= new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") ;
System.out.println(name);
password = request.getParameter("password");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
String msg = "";
try {
json.put("msg",login());
out.print(json.toString());
HttpSession session = request.getSession();
session.setAttribute("name",login());
} catch (SQLException e) {
e.printStackTrace();
}
}
public String login() throws SQLException{
Dao user = new Dao(name,password);
Dao u = new Dao();
u = user.login();
if(u.getName()!=null){
return u.getName();
}
else
return null;
}
跳转后的页面接收参数部分:
<%
String a = String.valueOf(session.getAttribute("name"));
%>
<%=a %>您好,欢迎您的登录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
