我正在servlet中成功創建會話,並且我可以將會話/會話屬性設置爲jsp,但不能設置爲端點類。我想獲得端點類中的會話信息。請幫我解決一下這個。如何處理Google App Engine中的會話?
我正在使用Eclipse的Maven和我啓用了AppEngine上-web.xml中
會話我讀了article這個也只是如何使會話我din't不懂。
這個servlet是檢查是否存在已經是一個會話
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public FirstServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service(request, response);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session=request.getSession(false);
if (session != null) {
String name = session.getAttribute("name");
// do something
} else {
// do something
}
}
}
如果會話沒有使用這個servlet
public class SeccondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SeccondServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service(request, response);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.getRequestDispatcher("login.html").include(request, response);
String name = request.getParameter("name");
HttpSession session=request.getSession();
session.setAttribute("name", name);
// do something
}
}
這是我的終點API類(谷歌雲端點有創建會話)
@Api(
name = "myapi",
version = "v1",
clientIds = "given client ids")
public class MyApi{
@ApiMethod(name = "name", path = "name", httpMethod = "post")
public List<String> getUser(HttpServletRequest servletReq) {
HttpSession session = servletReq.getSession(false);
List<String> name= new ArrayList<String>();
if(session == null) {
userName.add("no Name");
} else {
name.add((String)session.getAttribute("name"));
}
return name;
}
即使創建了我,我仍然得到「no Name」的結果會話,我可以得到的會話屬性,這裏的「名」
u能成爲更加清楚一點什麼ü意思終點?使用谷歌雲端點或普通的舊servlet或任何Mvc框架如春天等 – 2015-02-24 07:18:43
是的,我使用谷歌雲端點。並且我想要在Google雲端點類中獲得會話屬性。 – user4430114 2015-02-24 07:21:53