简体   繁体   中英

How to use page directive value in jsp element?

I wants to use page directive value in jsp element (eg value 'i' to be used in textbox with name='hidden'), which will be used to count total number of unsuccessful attempt.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page session="true" %>
<html>
<head>
<title>Login Application </title>
</head>

<body>
<h2>Login Application</h2>
<s:actionerror />
        <%
        Integer i = (Integer)application.getAttribute("i");
        if (i == null) {
            i = new Integer(1);
        } else {
            i = new Integer(i.intValue() + 1);
        }
        application.setAttribute("i", i);

        session.setAttribute("attemp", i); 
        out.println("Attemp :::::"+ session.getAttribute("attemp"));
        %>
<s:form action="login.action" method="post">
    <s:textfield name="username" key="label.username" size="20" />
    <s:password name="password" key="label.password" size="20" />
    <s:submit method="doGet" key="label.login" align="center" />
    <s:textfield id="hidden" name="hidden" value="<%=i %>" />
</s:form>
</body>
</html>

Using Scriplet is outdated
You can avoid scriptlet accessing Application and Session in struts way, don't forget to use # while accessing Application , Session Or Request scope variable
Here I have tried to avoid Scriplet code, Also please confirm while accessing Application variable...
For a help refer https://struts.apache.org/docs/accessing-application-session-request-objects.html

<s:if test="%{#application.i == null}">
    <s:set name="i" value="1" scope="application" />
</s:if>
<s:else>
    <s:set name="i" value="%{#application.i + 1}" scope="application" />
</s:else>

<s:set name="attemp" value="%{#application.i}" scope="session" />

And Set Your hidden textfield as :

<s:textfield id="hidden" name="hidden" value="%{#application.i}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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