简体   繁体   English

Servlet属性在JSP中不可用

[英]Servlet attributes not available in JSP

I have a strange problem. 我有一个奇怪的问题。 My servlet values only show up when the isAuthenticated is true here, not when it's false. 我的servlet值仅在isAuthenticated为true时显示,而不为false时才显示。 This is using Shiro, if that matters. 这很重要,正在使用Shiro。 I have looked at the console of hte browser and it's not redirecting or anything, but my values only show on the JSP when the if below is true. 我已经看过hte浏览器的控制台,它没有重定向或任何东西,但是当if if满足时,我的值仅显示在JSP上。

public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        if(SecurityUtils.getSubject() != null && SecurityUtils.getSubject().isAuthenticated()) {
            servletRequest.setAttribute("loginStatus",
                    "true");
        } else {
            servletRequest.setAttribute("loginStatus",
                    "false");
        }
        servletRequest.setAttribute("testValue", "Test Value");
        servletRequest.getRequestDispatcher("/index.jsp").forward(servletRequest, servletResponse);

Here is the JSP 这是JSP

<html>
    <body>
    <p>
        This is the index page, authenticated is <b>${loginStatus}</b>
    </p>
    <p>
        The test value is ${testValue}
    </p>
    <ul>
        <li><a href='app-login'>Login Page</a></li>
        <li><a href='test'>Test link (protected)</a></li>
        <li><a href='logout'>Logout</a></li>
    </ul>
</html>
        }

Any ideas? 有任何想法吗?

Turns out index.jsp was a poor name for that page. 原来index.jsp对于该页面来说是一个不好的名字。 Wildfly was just rendering the index.jsp page when my url was '/', instead of letting the servlet at that URL render it. Wildfly只是在我的URL为“ /”时呈现index.jsp页面,而不是让该URL上的servlet呈现它。

I changed the jsp to index-view.jsp and it works as expected. 我将jsp更改为index-view.jsp,它可以按预期工作。

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

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