【问题标题】:can be change the url in request dispatcher可以更改请求调度程序中的 url
【发布时间】:2012-07-11 12:33:57
【问题描述】:

请求派发时是否可以更改url。

这是我的代码

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{

 List<HomePageServicesDescription> data= HomePageServicesDescriptionDB.showHomePageServicesDescription();
 req.setAttribute("description", data);

 req.getRequestDispatcher("index.jsp").forward(req,res);

 }

所以当在网络浏览器中看到它时,它会给出 url=http://localhost:8888/url-mapping 的 servlet。 但我想要那个 url=http://localhost:8888/index.jsp。怎么可能。

【问题讨论】:

    标签: jsp servlets requestdispatcher


    【解决方案1】:

    您应该使用HttpServletResponse.sendRedirect() 而不是RequestDisaptcher.forward()。您想要发送的任何参数都可以作为查询参数发送。

    public void doGet(HttpServletRequest req, HttpServletResponse res) 
            throws IOException, ServletException
    {
    
       List<HomePageServicesDescription> data =
                 HomePageServicesDescriptionDB.showHomePageServicesDescription();
        req.setAttribute("description", data);
    
        res.sendRedirect("index.jsp?description="+data);
    
     }
    

    【讨论】:

    • 再次感谢先生,但我想设置请求属性。先生有什么办法请帮助我。
    • 发送查询参数并在重定向的 servlet 上作为 ServletRequest.getParameter("description") 访问它。有什么问题?
    • 先生,它在 jsp 页面中不起作用 List list= (List) ServletRequest.getParameter("description");它给出了错误
    • 我已经发布了 servlet 和 jsp 代码 list= (List) ServletRequest.getParameter("description"); if (!data.isEmpty()) { for (int i = 0; i
    • ServletRequest 是类,你应该给出请求的对象变量。所以应该是 request.getParameter("description")
    【解决方案2】:

    我得到了答案

    public void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws IOException, ServletException
    {
    
        List<HomePageServicesDescription> data = HomePageServicesDescriptionDB.showHomePageServicesDescription();
        req.getSession().setAttribute("description", data);
    
        res.sendRedirect("index.jsp");
    
    }
    

    在 index.jsp 中

    List<HomePageServicesDescription> data= (List<HomePageServicesDescription>) session.getAttribute("description");
    

    完美的工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      相关资源
      最近更新 更多