-2

在请求调度的情况下是否可以更改 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);

 }

因此,当在 Web 浏览器中看到它时,它会给出http://localhost:8888/url-mappingservlet 的 url=。但我想要那个 url= http://localhost:8888/index.jsp。怎么可能。

4

2 回答 2

0

我得到了答案

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

它完美地工作

于 2012-07-12T05:54:18.333 回答
0

你应该做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);

 }
于 2012-07-11T12:38:25.677 回答