如何传递参数
我在 REST Web 服务中编写了一种用户身份验证方法。身份验证成功后,我想传递用户名。我怎样才能通过呢?我可以在其他 Web 服务方法中从登录 Web 服务方法获取值传递吗?
我的登录代码是:
@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();
return Response.ok().build();
}
else
{
return Response.status(Response.Status.FORBIDDEN).build();
}
}
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();
return Response.ok().build();
}
else
{
return Response.status(Response.Status.FORBIDDEN).build();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你是否在这里使用某种网络框架,所以我会回答这个问题,就好像你没有使用一样。
Servlet 确实允许您向请求(处理请求后消失)、页面(同样,当页面消失时丢失)或会话(只要您的浏览器/Servlet 维护会话,该属性就存在)添加属性)。
我建议您从一个 如何处理 servlet 属性和的简单示例开始参数。 这里有更详细的解释。
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.