saga
saga

Reputation: 25

Keep same URL params after doPost()

I have a jsp page that list information depending on url. For example, url can be : "http://localhost:8080/group?ID=27" And the jsp will list all 'persons' of this specific 'group'.

In this Jsp page, (/WEB-INF/views/group.jsp), I have a form to add a new "person". I just want that, after submitting the form, and adding the user in the DB, I come back to the first url : "http://localhost:8080/group?ID=27", to see the list again, with the new user.

In my doPost() method, I have this:

public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

    InfoUser infoUser = new InfoUser();
    infoUser.setName(request.getParameter("name"));
    infoUser.setGroup_id(Integer.parseInt(request.getParameter("group_id")));
    infoUser.setDescription(request.getParameter("description"));
    infoUserDao.add(infoUser);

    this.getServletContext().getRequestDispatcher("/WEB-INF/views/group.jsp").forward(request,response);

}

But of course, after the doPost() method, I go back to group.jsp, which has this url : "http://localhost:8080/group"

Is there anyway to go back to "http://localhost:8080/group?ID=27" after posting ? Thanks for your help

Upvotes: 0

Views: 50

Answers (2)

saga
saga

Reputation: 25

For your information, this is the kind of code I used to solve the problem :

public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
    InfoPoint infoPoint = new InfoPoint();
    infoPoint.setName(request.getParameter("name"));
    infoPoint.setLength_id(Integer.parseInt(request.getParameter("length_id")));

    infoPoint.setDescription(request.getParameter("description"));
    infoPointDao.ajouter(infoPoint);

    int ID = infoPoint.getLength_id();
    request.setAttribute("infoPoints", infoPointDao.lister(ID));

    response.sendRedirect("/points?ID="+ID);
}

Hope it will help someone someday ;)

Upvotes: 0

Kraylog
Kraylog

Reputation: 7563

You can either redirect the user to the correct address (which requires another request to be made):

response.sendRedirect("/group?ID=27");

Or, you can update the URL in the browser after the page loads, using JavaScript:

window.history.pushState("object or string", "Title", "/group?ID=27");

Upvotes: 1

Related Questions