简体   繁体   English

Java Servlet/JSP - 无法访问 session 属性

[英]Java Servlet/JSP - can't access session attribiutes

while working on my homework first tasks were about servlets strictly and then when passing values between servlets the session.setAttribiute and session.getAttribiute worked OK. while working on my homework first tasks were about servlets strictly and then when passing values between servlets the session.setAttribiute and session.getAttribiute worked OK. But since using servlets and jsp I have problems with empty values.但是自从使用 servlets 和 jsp 后,我遇到了空值问题。 Here's the code in question.这是有问题的代码。
Servlet:小服务程序:

@WebServlet(name = "SubmitLoginServlet", value = "/submitLogin")
public class SubmitLogin extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            Connection con = DatabaseConnection.initDatabase();
            Statement statement = con.createStatement();
            String sql = "SELECT * FROM users";
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                String DBusername = resultSet.getString("name");
                String DBpassword = resultSet.getString("password");
                if(DBusername.equals(username) && DBpassword.equals(password)) {
                    request.getSession().setAttribute("username", "test");
//                  I USED "TEST" JUST FOR TESTING, STILL THE VALUE ON 'INDEX.JSP' IS EMPTY
                    request.getRequestDispatcher("/index.jsp").forward(request,response);
//                    response.sendRedirect("index.jsp");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// INDEX.JSP - pasted in this block because of bug with code formatting, sorry
// on top of the file I have <%@ page session = "false" %>

<div class="column is-one-fifth is-offset-two-fifths">
            <%
                HttpSession session = request.getSession(true);
                if(session.getAttribute("username") == "" || session.getAttribute("username") == null) {
            %>
            <form action="register.jsp">
                <button type="submit" class="button is-info is-fullwidth">Signup</button>
            </form>
            <form action="login.jsp" method="get">
                <button type="submit" class="button is-success is-fullwidth">Login</button>
            </form>

            <% } else { %>
            <h1 class="title"> Welcome <% request.getSession().getAttribute("username"); %></h1>
            <button class="button is-danger">Logout</button>
            <% } %>
        </div>

Then after login and redirecting to index.jsp i have Welcome with empty space and logout button.然后在登录并重定向到 index.jsp 后,我有欢迎空间和注销按钮。 Any help?有什么帮助吗?

Sorry guys, I'm just terrible at Java that's all.对不起,伙计们,我在 Java 上很糟糕,仅此而已。 I didn't know that when you want to output values in JSP you need to use <%= %> tags instead of <% %> that is used for logic.我不知道当您想要JSP中的 output 值时,您需要使用<%= %>标记而不是用于逻辑的<% %> > 标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM