【问题标题】:How to get the action from the HttpServlet request to dispatch to multiple pages如何从 HttpServlet 请求中获取操作以分派到多个页面
【发布时间】:2012-06-29 02:41:04
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

  • doExecutedispatch 的代码是什么?
  • 请忽略doExecute,我已经添加了dispatch方法。
  • “请求操作”是什么意思?
  • @Ramesh,假设您有一个用于http://localhost:8080/myproject/edithttp://localhost:8080/myproject/account 的控制器。这是两个不同的动作。

标签: jsp jakarta-ee servlets dispatcher


【解决方案1】:

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

【讨论】:

  • 这不是您所问问题的答案。您想知道通用控制器中的路径。 getServletPath() 仅在具有 servlet 映射时才返回。
  • 这正是上述问题的解决方案。当然,您必须将映射添加到 web.xml 文件,因为如果不这样做,您会得到 404,这就是拥有页面控制器的全部意义,否则我会选择前端控制器。
【解决方案2】:

您应该使用 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

【讨论】:

  • 有趣。我得试试这个。到目前为止,我正在获取 URI request.getRequestURI().toString()
【解决方案3】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 2019-03-08
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多