我有一个没有框架的 java 应用程序。它由用于 View 的 jsp 文件和用于业务逻辑的 servlet 组成。我必须将用户 session 设置为带有 firstName 参数的 servlet。在jsp文件中,我需要检查我的firstName参数是否有值。如果设置了firstName参数,我需要在jsp文件中显示一些html。如果没有设置,我需要在jsp文件中显示不同的html。
Servlet.java:
HttpSession session = request.getSession();
session.setAttribute("firstName", customer.getFristName());
String url = "/index.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
头文件.jsp:
// Between the <p> tags bellow I need to put some HTML with the following rules
// If firstName exist: Hello ${firstName} <a href="logout.jsp">Log out</a>
// Else: <a href="login.jsp">Login</a> or <a href="register.jsp">Register</a>
<p class="credentials" id="cr"></p>
什么是最好的方法来做到这一点?
更新:
这是我在 JSTL 上找到的一个很棒的教程,以防有人需要它:
http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm
最佳答案
<% if (session.getAttribute("firstName") == null) { %>
<p> some content </p>
<% } else {%>
<p> other content </p>
<% } %>
关于jsp - 检查jsp文件中的servlet session 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13638446/