日期:2014-05-16 浏览次数:20718 次
代码如下:
前台:
$.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 %>您好,欢迎您的登录