加上上一篇拦截器文章代码综合一下
<body>
<div>
<input type="hidden" id="txtHiddenCtx" value="${pageContext.request.contextPath}" />
用户名:<input type="text" id="txtuserid"/><br>
密码:<input type="password" id="txtpassword"/>
<input type="button" value="登录" onclick="loginc();"/>
</div>
<script type="text/javascript">
var ctx = $("#txtHiddenCtx").val();
function loginc() {
var paramData = new Object();
paramData.userid = $("#txtuserid").val();
paramData.password = $("#txtpassword").val();
var jsonData = JSON.stringify(paramData);
$.ajax({
url : ctx + "/auth/loginc",
type : "POST",
async : true,
// contentType:"application/x-www-form-urlencoded",
contentType : "application/json;charset=utf-8",
dataType : "json",
data : jsonData,
beforeSend : function(XMLHttpRequest) {
},
success : function(data, textStatus) {
if(data.flag == 1){
window.location.href = ctx + "/";
}else{
alert(data.msg);
}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
// 通常情况下textStatus 和errorThown只有其中一个有值
alert("error: " + XMLHttpRequest.responseText);
},
complete : function(XMLHttpRequest, textStatus) {
}
});
}
</script></body>
</html>
@Controller
br/></body>
</html>
@Controller
@RequestMapping(value="/")
public String index() {
return "index";
}
}
@Controller@RequestMapping(value="/auth")
br/>@RequestMapping(value="/auth")
@RequestMapping(value="/loginc",method=RequestMethod.POST)
public void LoginPage(@RequestBody Map<String, Object> map,HttpServletRequest request,HttpServletResponse response) {
Map<String,Object> result = new HashMap<String,Object>();
String userid = String.valueOf(map.get("userid"));
String password = String.valueOf(map.get("password"));
if(StringUtils.isEmpty(userid)) {
result.put("flag", 0);
result.put("msg", "userid isEmpty");
MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}
if(StringUtils.isEmpty(password)) {
result.put("flag", 0);
result.put("msg", "password isEmpty");
MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}
if("590031i".equals(userid) && "123".equals(password)) {
result.put("flag", 1);
HttpSession session = request.getSession();
session.setMaxInactiveInterval(1800); //设置过期时间,单位是秒
session.setAttribute("userid", userid);
}else {
result.put("flag", 0);
result.put("msg", "id or password error.");
}
MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}
@RequestMapping(value="/loginout",method=RequestMethod.POST)
public void LoginOut(@RequestBody Map<String, Object> map,HttpServletRequest request,HttpServletResponse response) {
Map<String,Object> result = new HashMap<String,Object>();
HttpSession session = request.getSession();
session.invalidate();
result.put("flag", 1);
MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。