JS页面获取session(通过ajax)
JS文件:
<script type="text/javascript">
function getUser(){
alert(window.location.host);
$.ajax({
// url:"http://localhost:8080/SecondDemo/user/getSession",
url:"http://"+window.location.host+"/SecondDemo/user/getSession",
type:"GET",
dataType:"text",
success:function(data){
if(data=="new")
window.location.href="pages/login.html";
else
{
alert(data);
window.location.href="pages/user.html";
}
},
error : function(e) {
alert("出错");
alert(e.responseText);
},
})
}
</script>
后台的java controller:
@RequestMapping(value ="/getSession",method = RequestMethod.GET)
public void getSession(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException
{
if(session.isNew())
{
response.getWriter().write("new");
System.out.println("出错");
}
else
{
String phone_id=(String) session.getAttribute("phone_id");
System.out.println(phone_id);
response.getWriter().write(phone_id);
}
}