2
I created a simple JSP page, which does nothing but call a Servlet to validate a login. Here is the code of the page index.jsp
:
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Foo</title>
</head>
<body>
<form action="validalogin" method="post">
<input type="text" placeholder="login" name="user"/>
<input type="password" placeholder="senha" name="pass"/>
<input type="submit" value="Entrar"/>
</form>
</body>
</html>
When I run this code, a JSESSIONID is automatically generated, as in the following image. The problem is that this happens even before calling Servlet, i.e., JSP is creating a session:
The form will be sent to Servlet (where the session will be created/validated), but as the page is creating one automatically, when the request arrives in Servlet it gives the session as valid because it has already been created previously.
I cleaned up the cookies browser and updated the page (F5), the result was the same: A new JSESSIONID was generated.
I did some research and found something related to the subject in that reply in Stackoverflow, where the author cites that:
Every call to JSP page implicitly creates new Session if there is no Session yet. This can be turned off by
session='false'
page Directive, in which case Session variable is not available on JSP page at all.
After that, I updated the directive from my page to the following:
<%@page contentType="text/html" pageEncoding="UTF-8" session="false" %>
And when I cleaned up cookies and updated the page, again a JSESSIONID was generated.
I don’t know if it’s something related to the server, but I’m using Apache Tomcat. I even looked in the configuration file server.xml
for something related to automatic session creation and found nothing.
How do I modify this behavior? I gathered all the details I found relevant to the question, if any are missing can charge in the comments.
Maybe this link can help you...
 http://stackoverflow.com/questions/2255814/can-i-turn-off-the-httpsession-in-web-xml
– user22557
@Guilhermeiazzetta This will "kill" any session. I just wouldn’t want you to be logged in when accessing a JSP page. This session will be created after the user has been validated, who will create Servlet (and not the page, as is being done). But thank you.
– Renan Gomes