如何传递参数

发布于 2024-10-31 15:43:57 字数 869 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2024-11-07 15:43:57

我不知道你是否在这里使用某种网络框架,所以我会回答这个问题,就好像你没有使用一样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文