I am developing a website with JavaServer Pages and I am facing the following problem: The page works correctly where I am developing (use NETBEANS for it). At the moment of 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 run 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, errors (the classic " Java Null Pointer Exception
") come out as a consequence.
How am I manipulating the session variables? Well, here are some pieces of code:
This here is a servlet to login. I am saving two data, the name of the user and the type of user. In addition to that, I set a time of life (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 recover 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 the 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 well, but from nowhere that happens.
Note: In the XML file I have also put the time of life 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 can not explain why (The server is an Ubuntu Server). What is Netbeans supposed to do to keep them alive?
I'm sure it's configuration issues. On the server to keep the sessions alive on each page, I have to use encodeURL
... but this is too annoying. 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