0

I Have a file called authenticate.jsp :

<%@page import=" java.sql.*, java.util.*,java.io.*" %>

        <%
                Connection con = null;
                Statement stmt = null;
                ResultSet fireman = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    String connectionString = "jdbc:mysql://localhost:3306/firedept?"
                    + "user=xxx&password=xxx"; 
                    con = DriverManager.getConnection(connectionString);
                    stmt = con.createStatement();
                    fireman = stmt.executeQuery("SELECT username,password,first,last,user_type FROM fireman");

                    String user = request.getParameter("user");
                    String pass = request.getParameter("pass");
                    while (fireman.next()) 
                    {
                        if (user.equals(fireman.getString("username")) && pass.equals(fireman.getString("password")))
                        { 
                            String first = fireman.getString("first");
                            String last = fireman.getString("last");
                            String user_type = fireman.getString("user_type");
                            session.setAttribute("fname", fireman.getString("first"));
                          %>

                            <script type="text/javascript"> 

                            sessionStorage.setItem("first", "<%=first%>" ) 
                            sessionStorage.setItem("last", "<%=last%>" )
                            sessionStorage.setItem("user_type", "<%=user_type%>" )

                            </script>

                          <%

                          response.sendRedirect("home.jsp"); // remember to change to jsp
                          break;
                        }
                    }

                    response.sendRedirect("login.jsp"); // remember to change to jsp
                }
                catch (Exception ex) {
                    out.write(ex.getMessage());
                }
                finally {
                    stmt.close();
                    con.close();                    
                }
                %>

And another file which is a header.jspf file and its used in other pages via <%@include file="header.jspf" %>

<div class="logged" id="login_tag">You are logged in : </div>
<script>
document.getElementById("login_tag").innerHTML = "You are logged in as : "+sessionStorage.getItem("first");
</script>

When i try to get the "first" value from the sessionStorage in the header it just returns null, what is the problem here ? This is the same project and the files are in the same folder running on Apache server. Thanks :).

blade
  • 69
  • 8
  • 1
    Can you show use the final HTML that is generated? You are using `script` tags, so hopefully everything that is executing in your page is executing in the correct order. If you want to ensure it is, then you may want to run all the javascript after the browser finishes loading the content using something like this: http://stackoverflow.com/questions/11936816/call-a-function-after-complete-page-load – Michael Freake Feb 03 '15 at 23:32
  • Ok i have checked using Chromes debugger and the sessionStorage is empty when i try to set a value in the Authentication JSP file, however when setting the sessionStorage in a different JSP file it works just fine. i will note that the Authenticate.jsp file is not seen by the user, it just processes the form info and redirects the user to another page. – blade Feb 04 '15 at 01:51
  • It is a VERY BAD PRACTICE to have business logic, especially DB connection details in a JSP page. – belgoros Jul 25 '17 at 13:29

1 Answers1

0

Well i did not manage to set the storage from the jsp page, instead of sending the users form to the jsp page and trying to set the storage there i just sent a JSON and used the response from the JSP page to set the storage in the login html page and it works just fine.

blade
  • 69
  • 8