我有一个没有框架的java应用程序.它由用于视图的jsp文件和用于业务逻辑的servlet组成.我必须设置用户会话是带有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);
Run Code Online (Sandbox Code Playgroud)
header.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>
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?
更新:
这是我在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>
<% } %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40168 次 |
| 最近记录: |