如何从HttpServlet请求中获取动作以分派到多个页面

时间:2012-06-29 02:41:04

标签: jsp java-ee servlets dispatcher

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

这是我的代码:

account.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);
}
    }

3 个答案:

答案 0 :(得分:4)

我发现使用HttpServletRequest#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);
    }
}

答案 1 :(得分: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/*,则会给出如下值:

  • 对于网址/accounts/account,它会返回/account
  • 对于网址/accounts/edit,它会返回/edit

答案 2 :(得分:0)

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

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

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

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