13

I am just starting with Servlets/JSP/JSTL and I have something like this:

<html>
<body>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:directive.page contentType="text/html; charset=UTF-8" />

<c:choose>
  <c:when test='${!empty login}'>
    zalogowany
  </c:when>
<c:otherwise>
   <c:if test='${showWarning == "yes"}'>
        <b>Wrong user/password</b>
    </c:if>
    <form action="Hai" method="post">
    login<br/>
     <input type="text" name="login"/><br/>
     password<br/>
     <input type="password" name="password"/>
     <input type="submit"/>
     </form>
  </c:otherwise>
</c:choose>
</body>
</html>

and in my doPost method

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
{
    HttpSession session=request.getSession();
    try
    {
        logUser(request);
    }
    catch(EmptyFieldException e)
    {
        session.setAttribute("showWarning", "yes");
    } catch (WrongUserException e) 
    {
        session.setAttribute("showWarning", "yes");
    }
    RequestDispatcher d=request.getRequestDispatcher("/index.jsp");
    System.out.println("z");
    d.forward(request, response);
}

but something is not working, because I wanted something like this:

  1. if user had active session and was logged to system "zalogowany" should show
  2. otherwise logging form

the problem is whatever I do, those forwards don't put me to index.jsp, which is in root folder of my Project, I still have in my address bar Projekt/Hai.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Andna
  • 6,539
  • 13
  • 71
  • 120

1 Answers1

27

If that is really your only problem

the problem is whatever I do, those forwards don't put me to index.jsp, which is in root folder of my Project, I still have in my address bar Projekt/Hai.

then I have to disappoint you: that's fully by specification. A forward basically tells the server to use the given JSP to present the results. It does not tell the client to send a new HTTP request on the given JSP. If you expect a change in the address bar of the client, then you have to tell the client to send a new HTTP request. You can do that by sending a redirect instead of a forward.

So, instead of

RequestDispatcher d=request.getRequestDispatcher("/index.jsp");
System.out.println("z");
d.forward(request, response);

do

response.sendRedirect(request.getContextPath() + "/index.jsp");

An alternative is to get rid of the /index.jsp URL altogether and use /Hai URL all the time. You can achieve this by hiding the JSP away in /WEB-INF folder (so that the enduser can never open it directly and is forced to use the servlet's URL for this) and implement the doGet() of the servlet as well to display the JSP:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
}

This way, you can just open http://localhost:8080/Project/Hai and see the output of the JSP page and the form will just submit to the very same URL, so the URL in browser address bar will basically not change. I would maybe only change the /Hai to something more sensible, such as /login.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ok then, I got it wrong then, if I use forward from servlet and use .jsp from WEB-INF then url will still point to my servlet, but if that is the case, how can I check if some user comes to the page for the first time so I can only show him login page? Because at first I was thinking that I just render .jsp page and use tags to determine if he comes for the first time. – Andna Feb 10 '12 at 02:03
  • 3
    For that a filter should be used: http://stackoverflow.com/tags/servlet-filters/info In a nutshell: when a login is successful, put the `User` in the session. In the filter you just check its presence and then continue or redirect the request accordingly. – BalusC Feb 10 '12 at 02:10
  • 1
    I use `getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);` to redirect from servlet to jsp. But this does not work in server. I used `response.sendRedirect(request.getContextPath() + "/message.jsp");` but then jsp does not show the message which I send from servlet. how to solve it ? – kamal Jul 08 '13 at 06:49
  • 1
    Another solution is to return the forwarded url back to the client (e.g., as a custom header on the response) and use some javascript to update the window location history - window.history.pushstate or replacestate. This doesn't work in older browsers (use a 302 redirect for these). – JeeBee Nov 07 '14 at 11:34
  • But make sure that you are not using web socket, if you are using web socket, new session is going to open. – Bassel Kh Aug 07 '18 at 23:43