I am successfully creating sessions in servlet and I can get sessions/ session attribute to jsp but not in endpoints class. I want to get the sessions info in endpoints classes. Please help me with this.
I am using maven in eclipse and I enabled sessions in appengine-web.xml
I read an article about this also except how to enable session I din't understand anything.
This servlet is to check whether there is an already a session
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
}
}
}
If session is not there create session using this 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
}
}
This is my endpoints api class (Google Cloud Endpoints)
@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;
}
I am still getting "no Name" as result even though I created session and i can get the session attribute, here "name"