I created a very simple REST webservice in Java by using JAX-RS. And have a client make Ajax calls to that webservice to login and get the login information.
JAX-RS Code
@Path("/netsuite")
public class myRESTWebService{
@GET
@Path("/login/{userName}")
public String login(@PathParam("userName") String userName){
//here I have to save that userName in some session so that I can use it in below function
}
@GET
@Path("/getUserName")
public String getUserName(){
//here I have to return the above username
}
}
I know REST webservices are stateless, how can I make it stateful. I tried Google search whole day, didn't helped me much.
How can I make it stateful ?
You can access the HttpSession and store the username like this:
@GET
@Path("/login/{userName}")
public String login(@PathParam("userName") String userName, @Context HttpServletRequest servletRequest) {
HttpSession session = request.getSession();
session.setAttribute("userName", userName);
}
But I strongly recommend to rethink why you need state in a stateless application and take a look at Java EE Security Concepts.
Unrelated: Classes in Java always start with a capital Letter.