3

我正在使用页面控制器模式。如何通过检测请求操作然后根据结果进行调度,将同一个控制器用于两个不同的页面?

这是我的代码:

帐户.jsp

<form name="input" action="<%=request.getContextPath() %>/edit" method="get">
   <input type="submit" value="Modifier" />
</form>

帐户 Servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("received HTTP GET");

        String action = request.getParameter("action");

        if (action == null)
        {
            // the account page
            dispatch(request, response, "/account");    
        }
        else if (action == "/edit") {
            // the popup edit page
            dispatch(request, response, "/edit");
        }

        protected void dispatch(HttpServletRequest request,
            HttpServletResponse response, String page)
            throws javax.servlet.ServletException, java.io.IOException {
        RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher(page);
        dispatcher.forward(request, response);
}
    }
4

3 回答 3

4

我发现 usingHttpServletRequest#getServletPath()正是我需要的,所以我不需要解析任何东西!

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("received HTTP GET");

    String action = request.getServletPath();

    if (action.equals("/account"))
    {
        // the account page
        dispatch(request, response, "/content/account.jsp");    
    }
    else if (action.equals("/edit")) 
    {
        // the popup edit page
        dispatch(request, response, "/content/edit.jsp");
    }
}

protected void dispatch(HttpServletRequest request,
    HttpServletResponse response, String page)
        throws javax.servlet.ServletException, java.io.IOException {
    RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher(page);
    dispatcher.forward(request, response);
    }
}
于 2012-06-30T03:12:22.953 回答
1

你应该使用httpServletRequest.getPathInfo().

这就是文档所说的:

java.lang.String getPathInfo()

Returns any extra path information associated with the URL the client sent when it
made this request. The extra path information follows the servlet path but 
precedes the query string and will start with a "/" character.

This method returns null if there was no extra path information.

Same as the value of the CGI variable PATH_INFO.

Returns:
    a String, decoded by the web container, specifying extra path information that
    comes after the servlet path but before the query string in the request URL; 
    or null if the URL does not have any extra path information

在您的情况下,如果AccountServlet映射到/accounts/*它会给出如下值:

  • 对于 url/accounts/account它返回/account
  • 对于 url/accounts/edit它返回/edit
于 2012-06-29T07:57:46.437 回答
0

我想你可能想看看request.getRequestURI()然后获取该 URI 的最后一部分:

String uri = request.getRequestURI().getPath();
String action = uri.substring(0, uri.lastIndexOf('/'));
...

我没有检查过这段代码。小心边缘情况。

我不确定我是否理解你的问题。无论如何,希望这会有所帮助。

于 2012-06-29T03:15:07.237 回答