4

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"

user4430114
  • 69
  • 1
  • 5
  • Can u be little more clear what do u mean by endpoints? U r using google cloud endpoints or plain old servlets or any Mvc frameworks like Spring, etc – Ramesh Lingappa Feb 24 '15 at 07:18
  • Yes i am using google cloud endpoints. and I want to get session attributes in google cloud endpoints class. – user4430114 Feb 24 '15 at 07:21

1 Answers1

10

Assuming you know about HttpSessions (if not it's simply cookie exchanged between sever and client in order to deal with the logged-in user).

So all user related or any other session-related information is stored in the server end and a session ID representing the information will be sent as cookie to the client, and the client will sent it back on every HTTP request.

AppEngine uses Datastore to store the session information and memcache for faster access to that information.

You can access the session data using a standard HttpSession object injected in every HTTP request.

The code to access this HttpSession varies in frameworks you use. If you want, I can specify code snippets to help to understand.

UPDATE:

If you are using servlets, then accessing session information will look like the following:

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String userID = "Pankaj";
private final String password = "journaldev";

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) {
  HttpSession session=request.getSession();
  // access any value
  User user = (User)session.getAttribute("loggedInUser");
}

And for Google Cloud endpoints, use the following:

@ApiMethod
public Response getUser( HttpServletRequest servletReq) {
    HttpSession session = servletReq.getSession();
    session.getAttribute("loggedInUser");
}
Mr_Spock
  • 3,815
  • 6
  • 25
  • 33
Ramesh Lingappa
  • 2,448
  • 20
  • 33
  • Thank you @Ramesh it is very helpful but i am getting error on Response asking me to import which i need to import among these org.jsoup.helper.HttpConnection.Response or com.google.appengine.repackaged.com.google.protos.gdata.proto2api.Core.Response; – user4430114 Feb 24 '15 at 13:53
  • Response is my custom class for returning for http request, you can use any object there like Map since we need to return json as response – Ramesh Lingappa Feb 24 '15 at 13:55
  • Eventhough after creating session with username I couldn't get the session in google cloud endpoints it is null please consider question again I updated with my code so that u can suggest me – user4430114 Feb 24 '15 at 16:12
  • @user4430114 Stack Overflow is not supposed to be your debugging service. If your initial question has been answered by Ramesh, upvote it and accept it, then ask another one :). – Patrice Feb 25 '15 at 22:19