gpt4 book ai didi

jsp - 重定向到 jsp。为什么 url 没有结尾/response.jsp?

转载 作者:行者123 更新时间:2023-12-04 19:36:57 25 4
gpt4 key购买 nike

我有 servlet 通过以下方式重定向到 jsp:

request.setAttribute("responseVar",responseVar); request.getRequestDispatcher("response.jsp").include(request, response);

为什么在浏览器中 url 看起来像 localhost:8080/servletname/servletname 而不是 localhost:8080/servletname/response.jsp

我该如何解决?

最佳答案

你需要的是response.sendRedirect("response.jsp");这也会更改 URL,但它不会记住请求中设置的任何属性。这是一个单独的请求。


什么 HttpServletResponse#sendRedirect()状态:

Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.


编辑

如果您还需要访问属性,请尝试任一选项:

  1. 发送值作为查询参数,但对查询字符串长度有限制

    小服务程序

    response.sendRedirect("response.jsp?responseVar="+responseVar);

    JSP

    ${param.responseVar}
  2. 设置为 session 属性

    小服务程序

    request.getSession().setAttribute("responseVar",responseVar);

    JSP

    ${sessionScope.responseVar}
  3. web.xml中修改Servlet本身的url-pattern如下所示,可以使用includeforward 也是如此。

    网络.xml

    <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.x.y.z.MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/response.jsp</url-pattern>
    </servlet-mapping>

关于jsp - 重定向到 jsp。为什么 url 没有结尾/response.jsp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24159470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com