0

I'm using in my project the embebed jetty and came up with a few problems. I have these two pages:

  • índex.jsp
  • result.jsp

And these two servlets:

  • upload
  • search

In the índex.jsp there is a form to upload a file and the upload process in handle by the upload servlet but then I should return a message to the índex.jsp to tell if the upload was done.

request.setAttribute("message", "Upload Sucedded!");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
rd.forward(request, response);

This will forward the message to the índex page but the url will be /upload and I would like to be the índex. So there is some other way of struct my files and maybe make my welcome file some servlet instead of the índex.jsp?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332

2 Answers2

1

This has nothing to do with Jetty. That's default behavior of forwarding.

Ugly solution:

If you want to rewrite your URL, use a redirect instead and pass the parameter by session. After displaying the message, remove it from session.

Better solution:

Change the name of your upload servlet by IndexServlet. This servlet will handle GET and POST requests for your index.jsp page. In the end of the processing, you will forward to your JSP page. By doing this, you can directly post the form to your current page:

<form action="index.jsp" method="POST" enctype="multipart/form-data">
    <!-- your fields ... -->
</form>

Then your servlet:

@WebServlet("index.jsp")
public class IndexServlet extends HttpServlet {
    //using ... to avoid parameters and exceptions to be thrown
    @Override
    public void doGet(...) throws ... {
        //this method should only forward to your view
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
        rd.forward(request, response);
    }

    //using ... to avoid parameters and exceptions to be thrown
    @Override
    public void doPost(...) throws ... {
        //current implementation...
        //in the end, forward to the same view
        request.setAttribute("message", "Upload Sucedded!");
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
        rd.forward(request, response);
    }
}

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • This my website can't find the css files and etc... In jetty i have made my default servlet the indexServlet instead of the webpage. – esfomeado12 Aug 12 '14 at 08:22
0

The simplest solution would be to change upload servlet to use redirect and use request parameter instead of attribute like this:

// upload servlet
response.sendRedirect("index.jsp?message=YourMessage");

Then in index.jsp use request.getParameter("message") or EL to display your message.

Gas
  • 17,601
  • 4
  • 46
  • 93