0

Edit: This is not a duplicate of this question because I'm trying to send data between doGet and doPost of the same servlet. The other question asks about how to send data between different servlets but I'm already doing that by appending the info to the URL (see very first code snippet below), which I can't do within the same servlet. Plus, the linked question is from 2011 and uses Tomcat 5.5, while I'm using 8.5.

I'm working on a small Java 8/Tomcat 8.5 "app" with two servlets and a filter: If the servlet path is "/hello", the filter kicks in and does this if there's no active session:

response.sendRedirect(request.getContextPath() + "/login?p="+request.getServletPath());

This loads the "/login" servlet (LoginServlet.java) and its html file (LoginHTML.html):

<body>
    <form method="POST" action="login">
        <label for="username">Username:</label><br>
        <input type="text" id="username" name="username"><br>
        <label for="password">Password:</label><br>
        <input type="password" id="password" name="password"><br><br>
        <input type="hidden" id="redirect" value="">
        <input type="submit" value="Login">
    </form>
</body>

Clicking the "Login" button should then load the page that the user tried to access before (via LoginServlet.doPost), in my case "/hello" (but it could also be "/hello/bla" or "/hello/thisisatest" - even though those don't exist yet).

LoginServlet.doGet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String redi = request.getParameter("p");
    RequestDispatcher dispatcher = request.getRequestDispatcher("/html/LoginHTML.html");
    dispatcher.forward(request, response);
}

redi contains the path that was accessed before but I'm now looking for a way to somehow pass this to doPost, so it can go back to the previous page.

As you can see in the above html code, I already added the hidden attribute redirect to the form but how do I set its value to the value in redi at runtime (probably in doGet), so I can access it in doPost with request.getParameter("redirect")? Is there another way to do this?

Neph
  • 1,823
  • 2
  • 31
  • 69
  • @BalusC This is not a duplicate of [this](https://stackoverflow.com/q/5364354/2016165) question because I'm trying to send data between `doGet` and `doPost` of the **same** servlet. The other question asks about how to send data between different servlets but I'm already doing that by appending the info to the URL (see very first code snippet below), which I can't do within the same servlet. – Neph Jul 20 '20 at 11:10
  • @BalusC Thanks for opening the question again. The JST part of the accepted answer works but you have to use a jsp file. I'd rather keep the html version I also mentioned in my question, so without using any extra libraries. – Neph Jul 20 '20 at 13:25
  • @BalusC I meant, I'd rather not use a jsp file because that requires the JST library. Is it possible to add stuff like the hidden attribute to an html page at runtime? I tried to simply write to a `PrintWriter` after the `forward` in `LoginServlet.doGet` but that didn't add anything. Or is there maybe a completely different way to pass the extra info from `doGet` to `doPost` (that doesn't require an extra library)? – Neph Jul 20 '20 at 13:46

1 Answers1

1

This is not the best way, but you can try it. You can use JavaServer Pages and JSTL or other similar things instead HTML.

In doGet() method use request.setAttribute("redi", redi). In JSP use ${redi} in your hidden field value. In doPost() method use request.getParameter("redirect").

maegami
  • 26
  • 4
  • Thanks, I tried it but unfortunately `request.getParameter("redirect")` returns `null` in `doPost`. `This is not the best way, but you can try it.` - What is "the best way" to do it then? Is it possible to do this with an html file (instead of the jsp)? – Neph Jul 20 '20 at 11:28
  • I managed to fix it: `name="redirect"` was missing. The full line is: ``. I'd prefer not to use jsp though, is there a way to do it with just a basic html file? – Neph Jul 20 '20 at 12:54
  • You can use Thymeleaf if you want to use HTML files, but it's not very convenient. – maegami Jul 21 '20 at 13:35
  • Servlets are commonly used with JSP. There are 3 ways to pass values ​​between requests in a servlet application, there are cookies, hidden field (your option) and application context (implemented using `ServletContextListener`) – maegami Jul 21 '20 at 13:42
  • Another way is to use a session, in my opinion, this way is better. You can use it by `request.getSession()`. `HttpSession` class has methods `setAttribute()` and `getAttribute()`. After using, you can delete it by `removeAttribute()`. – maegami Jul 21 '20 at 13:52