我用java的主要servlet來創建會話變量。但我是 無法從java main2 servlet訪問該會話變量。HttpSession不工作?
前端是Angular2和後端是用java
Angular2部分
getsess(){
console.log("button press")
var url = "http://localhost:8000/demoApp/main";
var header=new Headers();
header.append('Content-Type','application/x-www-form-urlencoded');
let body=new URLSearchParams();
body.append('username',this.pass);
this._http.post(url,body,{headers:header}).subscribe(
res => console.log(res)
);
}
view(){
let body=new URLSearchParams();
body.append('username',this.pass);
this._http.post("http://localhost:8000/demoApp/main2",body).subscribe(
res => console.log(res)
)
}
inval(){
this._http.get("http://localhost:8000/demoApp/main3").subscribe(
res => console.log(res)
)
爪哇主要的servlet部分
// TODO Auto-generated method stub
String s=request.getParameter("username");
System.out.println(s);
HttpSession hs=request.getSession(true);
hs.setAttribute("s", s);
System.out.println("session created !! "+ hs.getAttribute("s"));
爪哇MAIN2 servlet的部分
HttpSession hs=request.getSession();
System.out.println("another page"+hs.getAttribute("s"));
輸出
你怎麼在這裏做會話管理? –
我感覺jsessionid cookie沒有設置。你可以嘗試直接從瀏覽器打到網址嗎? – Sanj