可以更改请求调度程序中的URL

时间:2012-07-11 12:33:58

标签: jsp servlets requestdispatcher

是否可以在发送请求时更改网址。

这是我的代码

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浏览器中看到它时,它会给出servlet的url = http://localhost:8888/url-mapping。 但我想要url = http://localhost:8888/index.jsp。怎么可能。

2 个答案:

答案 0 :(得分: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);

 }

答案 1 :(得分:-1)

我得到了答案

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

完美的工作