Session variables only work in the development environment (JSP)

0

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

asked by TwoDent 11.07.2018 в 02:16
source

2 answers

1

Do you have several WARs on 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

link

    
answered by 21.07.2018 / 11:39
source
0

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 sendRedirect() you make a new request and this is very used when dealing with POST requests and the attributes are not visible, but when you forward you do not make a new request but, the same request will be traveling for each one of servlets that you indicate and always keeping the same URL in the browser and is done with:

request.getRequestDispatcher("ruta del jsp que quieres ir").forward(request, response);

That will always keep the same session or until you eliminate it.

I hope I helped something.

    
answered by 22.07.2018 в 00:39