how to pass parameter
I have written a method for user authentication method in REST web service. After successful authentication, I want to pass the username. How can I pass it? Can I get value pass from login web service method in other web service method.
My code for login is:
@GET
@Produces("application/json")
public Response login(@Context HttpServletRequest req,@Context HttpServletResponse res,@QueryParam("loginname")String loginname,@QueryParam("password")String password) throws IOException, ServletException
{
userDAOImpl impl = new userDAOImpl();
Mongo mongo=impl.getConnection("127.0.0.1","27017");
DB db=impl.getDataBase(mongo,"userdb");
DBCollection coll=impl.getColl(db,"userdb");
userDTO dto = new userDTO();
dto.setUsername(loginname);
dto.setPassword(password);
if(impl.checkUser(coll, dto))
{
mongo.close();
re开发者_如何学编程turn Response.ok().build();
}
else
{
return Response.status(Response.Status.FORBIDDEN).build();
}
}
I can't tell if you are using some sort of web framework here or not, so I'll answer the question as if you aren't.
Servlets do allow you to add attributes to a request (which are gone after the request is processed), to a page (again, lost when the page is gone), or session (which lives as long as your browser/servlet maintain the session).
I'd suggest you start here with a simple example of how to deal with servlet attributes and paramters. And here's a more detailed explaination.
精彩评论