Hi, my question is about forwarding from a servlet to a JSP. Here is my servlet:
So if the session has expired the servlet redirects to login.jsp. The problem is that codeA is also executed. I want if the session has expired, the servlet to redirect to the JSP and codeA to be ignored. CodeA must be executed only when the session is valid. I know I can do it this way:
However putting my long code in an else clause makes it more difficult to read. That's why I am asking if there is another way - a command that forwards to a JSP and discards the remaining code (like <jsp:forward page="/..."/> in JSPs).
This is not a question about servlets, and it's not a question about forwarding to a JSP. The code in a servlet is Java code and it follows all of the rules of Java code. Nothing special happens in your Java code because it is in a servlet.
In other words you already know the answer to this. An alternative to
Or you could put your "long code" into another method and call it from this one... there's lots of Java options you could use. Just don't get distracted by the fact that your Java code happens to be in a servlet.