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"
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With