pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>welcom to lvmeihe's world</title>
</head>
<body>
<!-- action的值可以有controller处理,也可以交给servlet处理 -->
<form action="index.action">
<span>账号</span><input type="text" name="userAccount" required="required">
<span>密码</span><input type="password" name="passWord" required="required">
<input type="submit" value="登录">
</form>
<h1>方法2</h1>
<span>账号</span><input type="text" name="userAccount" id="id1" required="required">
<span>密码</span><input type="password" name="passWord" id="id2" required="required">
<input type="button" id="enterIndex">
<script type="text/javascript">
$("#enterIndex").click(function(){
$.ajax({
type: 'post',
usl: 'index.action',
data:{'账号':'value','密码':'value'},
dataType:"JSON",
success: function(){window.location= 'index.jsp';
}
});
方法2:$.get(url,data,function(){从controller中返回的数据});
方法3:$.post(url,data,function(){从controller中返回的数据});
});</script>
</body>
</html>
@ResponseBody
@RequestMapping("login")
public String doLogin(HttpServletRequest request, HttpServletResponse response){
String userAccount = request.getParameter("userAccount");
String password = request.getParameter("password");
//根据从页面获取的账号和密码 和数据库中的对比 查看是否匹配
//匹配成功 允许登录并进入主页
//匹配不成功重新进入登录页/或者注册页(点击密码找回按钮)
if(" ".equals(" ")){
request.setAttribute("error", "登录失败");
}
return "0";
}
方法2
@RequestMapping("index")
public String tests(Map<String,Object> params){
String account = (String) params.get("userAccount");
String password = (String) params.get("password");
//从数据库中查找数据 然后和account password进行比较
return "index";
}
是否登录过?
@RequestMapping("xxx")
public String doIndex(Map<String,Object> params,HttpServletRequest request, HttpServletResponse response){
Set<String> keys = params.keySet();
for (String string : keys) {
System.out.println(string);
}
Cookie[] cookies = request.getCookies();
//判断是否登录过了
if(cookies != null && cookies.length > 0){
for(Cookie cookie : cookies){
String name = cookie.getName();
if(name.equals("login")){
//有login。说明已经登录过了,可以跳转到index.jsp页面
return "index";
}
}
}
//没登录过,就在此返回到登录页面,进行登录
return "login";
}