簡體   English   中英

從servlet捕獲URL的參數

[英]capture parameters of a URL from servlet

我有兩個Web應用程序。 當用戶單擊第一個應用程序中的鏈接時,將顯示第二個應用程序。 我已包含一個用戶注冊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中的參數變量。 請幫我做到這一點。

如果您的第二個應用程序需要userid參數。 您可以在第一個應用程序的鏈接中傳遞它,而在第二個應用程序中將獲得它。 我認為您做得正確。

“任何人都請告訴我,我是否應該在第二個應用程序中的任何位置提及鏈接的URL。”-不確定在這里到底想要什么,但是在第一個應用程序鏈接中,如果您傳遞參數,則第二個應用程序將從該參數中獲取它。請求,就像您執行請求的方式一樣。

編輯后

使用request.getRequestURL(); 這將為您提供所需的網址。 然后,您只需執行字符串操作即可獲取值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM