Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查jsp文件中的servlet会话属性值_Jsp_Servlets - Fatal编程技术网

检查jsp文件中的servlet会话属性值

检查jsp文件中的servlet会话属性值,jsp,servlets,Jsp,Servlets,我有一个无框架的java应用程序。它由用于查看的jsp文件和用于业务逻辑的servlet组成。我必须将用户会话设置为带有firstName参数的servlet。在jsp文件中,我需要检查firstName参数是否有值。如果设置了firstName参数,我需要在jsp文件中显示一些html。如果没有设置,我需要在jsp文件中显示不同的html Servlet.java: HttpSession session = request.getSession(); session.setAttribute

我有一个无框架的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);
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>
//在下面的标记之间,我需要使用以下规则放置一些HTML
//如果firstName存在:Hello${firstName}
//或者

最好的方法是什么

更新:

下面是我在JSTL上找到的一个很好的教程,以防有人需要它:


一些内容

其他内容


在servlet中,您可以编写如下内容

        HttpSession session = request.getSession(true);
        session.setAttribute("firstName", customer.getFristName())
        response.sendRedirect("index.jsp");
request.getSession(true)
返回一个新会话,如果它不存在,则返回任何会话,否则将返回当前会话。 在
index.jsp
页面中,您可以执行以下操作:

<%
if(session.getAttribute("firstName")==null) {
%>
<jsp:include page="firstPage.html"></jsp:include>
<%
} else {
%>
<jsp:include page="secondPage.html"></jsp:include>
<%
}%>


这里,如果
firstName
为空,那么
firstPage.html
将包含在页面中,否则
secondPage.html

我认为最好的方法是使用jstl标记。因为对于简单的jsp应用程序,最好将所有java代码添加到html中,但对于更重的应用程序,最好在html中使用最少的java代码。(将视图层与逻辑分离)(阅读本文了解更多信息)
出于您的期望,您可以很容易地使用下面的代码

<c:if test="${not empty firstName}">
    <%--If you want to print content from session--%>
    <p>${firstName}</p>

    <%--If you want to include html--%>
<%@include file="/your/path/to/include/file.jsp" %>

    <%--include only get wrong if you give the incorrect file path --%>
</c:if>
<c:if test="${empty firstName}">
    <p>Jaathi mcn Jaathi</p>
</c:if>

${firstName}

Jaathi mcn Jaathi


如果没有正确地包含jstl,将无法获得预期的输出。请参阅此事件

谢谢你,易卜拉欣!这个解决方案干净简单。它做的正是我想要它做的。我当时正在搞乱JSPEL,结果弄得一团糟凌乱的显然你做错了什么。使用JSTL/EL,只需
一些内容

其他内容

。我不知道那怎么会更混乱。@BalusC我当然知道!我在EL中寻找一个if-else构造,但无法让它工作,所以我放弃了这个想法。看看你的例子,它也能满足我的要求。我可能会使用这个,而不是将java代码放在JSP中。我读到在JSP中使用EL/JSTL比使用java更好。谢谢你的帮助!非常糟糕。。。!使用Scriptlet是最糟糕的主意。请参阅@Menuka Ishan的答案,使用jstl-way-to-go。谢谢Visruth!关于getSession(true)的精彩解释。
request.getSession()
在封面下做的完全相同。另见。事实上,
true
参数是完全多余的。
<c:if test="${not empty firstName}">
    <%--If you want to print content from session--%>
    <p>${firstName}</p>

    <%--If you want to include html--%>
<%@include file="/your/path/to/include/file.jsp" %>

    <%--include only get wrong if you give the incorrect file path --%>
</c:if>
<c:if test="${empty firstName}">
    <p>Jaathi mcn Jaathi</p>
</c:if>