4

I have: A managed bean called "LoginBean". A JSF page called "login.xhtml"

In this jsf page, i have a login form.

Inside the managebean i have a loginCheck function.

public void loginCheck(){
 if(logincorrect){
  //set user session 
 }else{
  //set lockout count session ++
 }
}

What i want to do in my jsf page is that when the lock out count session == 2 (means users failed to login correctly 2 times, i need a recaptcha tag to be displayed.

<td>
    <%
         if(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("numberOfLogins") == 2){
         <p:captcha label="Captcha" requiredMessage="Oops, are you human?"/>
       }
     %>

Apparently, the <% tag does not work. Appreciate any help from java/jsf experts.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Slay
  • 1,285
  • 4
  • 20
  • 44
  • 4
    Another terrible idea. Scriptlet code in JSPs is 1998 vintage stuff. You shouldn't be doing this. Learn JSTL and don't put scriptlets in your pages. – duffymo Sep 08 '12 at 13:03
  • Who are we to judge? I thought SO was to be factual base? Why do the questions have to be so factual but answers and comments are opinionated? lol. – cbmeeks Dec 10 '13 at 15:53

2 Answers2

16

Scriptlets (those PHP-like <% %> things) are part of JSP which is deprecated since JSF 2.0 in favor of its successor Facelets (XHTML). Facelets doesn't support any alternative to scriptlets anymore. Using scriptlets in JSP has in almost all cases lead to badly designed and poorly maintainable codebase. Forget about them. Java code belongs in fullworthy Java classes. Just prepare the model (some Javabean class) in the controller (a JSF backing bean class) and use taglibs and EL (Expression Language, those #{} things) to access the model in the view.

Your concrete use case,

<%
     if(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("numberOfLogins") == 2){
     <p:captcha label="Captcha" requiredMessage="Oops, are you human?"/>
   }
 %>

can be solved in fullworthy JSF/EL as follows:

<p:captcha label="Captcha" requiredMessage="Oops, are you human?" rendered="#{numberOfLogins == 2}" />

That numberOfLogins can by the way much better be made a property of a JSF @SessionScoped @ManagedBean than some attribute being manually placed in the session map.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • can we use scriptlet in jsf 1 ? – Mahmoud Saleh May 15 '13 at 08:35
  • 1
    *Scriptlets* are not specific to JSF. They are specific to the JSP view technology. So, if you're using JSP, then you can use *scriptlets*. However, they are considered bad practice and officially discouraged. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202 – BalusC May 15 '13 at 10:32
9

This is not how JSF works, at least not with XHTML as the presentation layer instead of JSP. (<% is part of JSP, which you're no longer using here.) The proper way to do this is with managed beans. Alternatively, you could use Expression Language (EL) here.

I'd review the "JavaServer Faces Technology" chapter of Oracle's Java EE tutorial for some additional assistance.

ziesemer
  • 27,712
  • 8
  • 86
  • 94