I am developing a web page with JavaServer Pages and I am facing the following problem: The page works correctly where I am developing (I use NETBEANS for it). When giving " Run Project `" the page receives the session variables very well.
When I give " Clean and build " to generate the file .WAR
to upload it to my hosting, and I execute the page from the domain (from the hosting), at times I lose the value contained in the session variable, and as a result of this, as a consequence they leave errors (the classic " Java Null Pointer Exception
").
How am I manipulating the session variables? Ok, here are some snippets of code:
This right here is a servlet to login. I am saving two data, the user name and the user type. In addition to that, I establish a lifetime (1 hour):
Login log = new Login();
log.setEmail(request.getParameter("username"));
log.setPassword(request.getParameter("password"));
try {
if (log.iniciarSesion()) {
HttpSession session = request.getSession(false); <---- Llamo la sesion
session.setAttribute("username", log.getEmail()); <---- guardo usuario
session.setAttribute("tipo", log.getTipoUser().charAt(0)); <---- guardo tipo
MessageController.setError(false);
session.setMaxInactiveInterval(3600); <---- ajusto tiempo de vida (3600 segundos)
switch (log.getTipoUser().charAt(0)) {
case 'T':
response.sendRedirect("ListaObrasServlet?page=1");
break;
case 'S':
response.sendRedirect("ListaTitularServlet?page=1");
break;
default:
response.sendRedirect("ObrasServlet?page=1");
break;
}
} else {
MessageController.setError(true);
response.sendRedirect("index.jsp");
}
} catch (SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
Now an example of how I retrieve the values:
<nav class="gtco-nav" role="navigation">
<div class="gtco-container">
<div class="row">
<div class="col-sm-4 col-xs-12">
<div id="gtco-logo"><a href="index.jsp">VUDA </a></div>
</div>
<div class="col-xs-8 text-right menu-1">
<ul>
<% if(session.getAttribute("tipo").toString().contains("U")){ %>
ETC ETC ETC ETC
<% }else if(session.getAttribute("tipo").toString().contains("T")){%>
ETC ETC ETC ETC
<% }else if(session.getAttribute("tipo").toString().contains("S")){ %>
ETC ETC ETC ETC
<%}%>
(mas y mas html)
<% if(session.getAttribute("tipo").toString().contains("U") ){%>
ETC ETC ETC ETC
<% } %>
<li><a href="#">Contactenos</a></li>
<li class="btn-cta" <% if(session.getAttribute("username")!=null ) out.print("style='display:none;'");%>><a id="btn2" data-toggle="modal" data-target="#loginModal"><span>Usuario</span></a></li>
<li class="btn-cta" <% if(session.getAttribute("username")!=null ) out.print("style='display:inline;'");else out.print("style='display:none;'");%>><a href="logoutServlet"><span>Cerrar Sesion</span></a></li>
</ul>
</div>
</div>
</div>
</nav>
Well, as I was saying, I repeat the problem: everything works fine when I run from netbeans , but when I upload the page to the hosting, I lose the value contained in the session when I change the page . There are moments when everything works fine, but out of nowhere that happens.
Note: In the XML file I also have the life time of the sessions with this:
<session-config>
<session-timeout>
60
</session-timeout>
</session-config>
What could it be?
UPDATE: I discovered that the session actually dies as soon as you change the page in the hosting, and I don't understand why (The server is an Ubuntu Server). What is Netbeans supposed to do to keep them alive?
I'm pretty sure it's a configuration issue. On the server, in order to keep the sessions alive on each page, I have to use encodeURL
... but this is too annoying for me. How do I make the GlassFish server keep the session alive? What I use in my Stack Web is:
Operating System: Ubuntu Server 18.04 LTS
Web Server: GlassFish Server 4.1.1
Database: MySQL
Programming language: Java Server Pages
Do you have several WAR's in the application server?
I have had problems in a Websphere having several applications because of the session cookie (JSESSIONID), since they all used the same name.
It worked for me to change the name of the session cookie to some applications so that the name was unique and could not be repeated.
Maybe you could try it on Glashfish to see if it works
https://docs.oracle.com/cd/E18930_01/html/821-2417/beash.html
I hope to help you with this brief idea that I have.
I believe that you lose the session variables by the number of times you make the requests. I understand that with you
sendRedirect()
make a new request and this is very used when it comes to POST requests and the attributes are not visible, but when you forward you do not make a new request but rather the same request will travel through each of the servlets that you indicate and always keeping the same URL in the browser and is done with:With that, the same session will always be maintained or until you delete it.
I hope I have helped in something.