使用JSP </key,value>设置HTML5会话存储<key,value>

时间:2015-02-03 20:06:25

标签: javascript html5 jsp

我有一个名为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();                    
                }
                %>

另一个文件是header.jspf文件,它通过&lt;%@ include file =&#34; header.jspf&#34;在其他页面中使用%GT;

<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>

当我试图获得&#34;第一个&#34;来自headerStorage的值只是返回null,这里有什么问题?这是同一个项目,文件位于Apache服务器上运行的同一文件夹中。 谢谢:)。

1 个答案:

答案 0 :(得分:0)

我没有设法从jsp页面设置存储,而不是将用户表单发送到jsp页面并尝试在那里设置存储我刚刚发送了一个JSON并使用JSP页面的响应来设置存储在登录html页面,它工作得很好。