【问题标题】:capture parameters of a URL from servlet从 servlet 捕获 URL 的参数
【发布时间】:2014-01-22 10:27:04
【问题描述】:

我有两个网络应用程序。当用户单击第一个应用程序中的链接时,会出现第二个应用程序。我已将用户注册 ID 作为参数添加到链接的 URL。在第二个应用程序中,我编写了一个 servlet 来捕获用户注册 ID。

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

        try {
            int userId= Integer.parseInt(request.getParameter("userId"));

        } finally {            
            out.close();
        }
    }  

请告诉我是否应该在第二个应用程序中的任何位置提及链接的 URL。否则,servelet 从哪里获取 userId 参数。我对 java web 的东西很陌生。请帮帮我。

由于我的问题不清楚,我将在下面放另一个示例。 以下是我的服务。

@Path("/hello")
public class HelloWorldService {

@GET
@Path("/{email}")
public Response getUserDetails(@PathParam("email") String email){
    String output = "User info ..  "+email ;
    return Response.status(200).entity(output).build();
}

下面是我的servlet。

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

        try {
            String parameter= request.getParameter("email");

        } finally {            
            out.close();
        }
    }  

我的网址是 ...localhost:8080/RESTfulExample/rest/hello/hasi@gmail.com 我想将“hasi@gmail.com”获取到 servlet 中的参数变量。请帮我做这件事。

【问题讨论】:

  • 请包含您的导入。

标签: java web-services rest servlets web


【解决方案1】:

如果您的第二个应用程序需要 userid 参数。您可以在第一个应用程序的链接中传递它,在第二个应用程序中您将获得它。我认为你做得很好。

“请告诉我是否应该在第二个应用程序中的任何位置提及链接的 URL。” - 不确定你到底想要什么,但在第一个应用程序链接中,如果你传递参数,第二个应用程序将从请求中获取它,就像你做的那样。

编辑后

使用request.getRequestURL(); 这将为您提供请求的网址。然后你可以只做字符串操作来获取值。

【讨论】:

  • 非常感谢您的回答。我想我的问题不清楚。所以我会在下面放另一个小样本。请帮我解决问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 2013-02-06
相关资源
最近更新 更多