-2

Is it possible to change the url in case of request dispatch .

This is my code

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

 }

So when see it in web browser so it give the url=http://localhost:8888/url-mapping of servlet. but i want that url=http://localhost:8888/index.jsp. how it can be possible.

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50

2 Answers2

1

I got the answer

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

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

    res.sendRedirect("index.jsp");

}

And in index.jsp

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

Its perfectly work

0

You should be doing HttpServletResponse.sendRedirect() instead of RequestDisaptcher.forward(). Any parameters you want to send can be sent as Query Parameters.

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

 }
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • thankyou again sir but i want to set the request attribute. Sir there is any way plz help me. – Aashutosh Shrivastava Jul 11 '12 at 12:40
  • Send Query parameter and access it as ServletRequest.getParameter("description") on redirected servlet. What is the issue? – Ramesh PVK Jul 11 '12 at 12:42
  • sir it is not working in jsp page List list= (List) ServletRequest.getParameter("description"); it give the error – Aashutosh Shrivastava Jul 11 '12 at 12:48
  • i already post the servlet & jsp code <% List list= (List) ServletRequest.getParameter("description"); if (!data.isEmpty()) { for (int i = 0; i < data.size(); i++) { HomePageServicesDescription description = data.get(i); %>
    <%=description.getTitle()%>

    <%=description.getDescription()%>

    <%} } else {%>
    – Aashutosh Shrivastava Jul 11 '12 at 12:51
  • ServletRequest is class, you should be giving the object variable which is request. So it should be request.getParameter("description") – Ramesh PVK Jul 11 '12 at 12:57
  • sry sir it is not working it give the exception and the url is http://localhost:8888/index.jsp?description=[Title%20:%20Portal%20/%20eCommerce,%20Description%20:%20%3Cspan%20style=%22color:%20rgb(102,%20102,%20102);%20text-align:%20justify;%20%22%3EOur%20development%20team%20build%20solutions%20that%20work%20across%20user%20communities%20and%20devices(different%20types)%20right%20out%20of%20the%20box. %3C/span%3E%3Cbr%] like this – Aashutosh Shrivastava Jul 11 '12 at 13:03
  • what is the exception you are getting – Ramesh PVK Jul 11 '12 at 13:08