Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:42:41+00:00 2026-06-18T15:42:41+00:00

JSF 2.1 Tomcat 7.0 Is this a wrong use of dependency injection? It works

  • 0

JSF 2.1
Tomcat 7.0

Is this a wrong use of dependency injection?

It works ok moving around the page. But “it works” is not the same as “it’s correct”.

I would use this pattern also for search purpose. I have a request and i like to use it to populate a table in the same page. Is this possible?

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <link rel="SHORTCUT ICON" href= "resources/img/onlyT.ico"></link>
        <title>title</title>
        <h:outputStylesheet library="css" name="compass.css"/>
    </h:head>
    <h:body>
        Login system
        <br />

        <h:form>
            User : <h:inputText value="#{user.username}" />
            Password : <h:inputSecret value="#{user.password}" />
            <h:commandButton action="#{loginBean.performLogin()}" value="Submit" />
            <h:commandButton value="reset" type="reset" />
            <h:commandButton value="ChangePsW" action="changePassword"></h:commandButton>
        </h:form>

        <h:message for="" style="color:red;margin:8px;"/>

    </h:body>
</html>

LoginBan.java

@ManagedBean
@RequestScoped
public class LoginBean {


    @ManagedProperty(value="#{user}")
    private UserBean userBean;

    //must povide the setter method
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public LoginBean() {}


    public String performLogin(){


        //Mi connetto al db
        if(userBean.getUsername().equalsIgnoreCase( "mario")){

        //effettuo i controlli per stabilire se esiste l'utente

        userBean.setRoles("-EE-E-E-E-E-E");
        return "/mainPortal/mainPortal";
        }
        return "/mainPortal/index";

    }
}

UserBean.java

@ManagedBean (name="user")
@SessionScoped 
public class UserBean implements Serializable  {

private String Username;
private String Password;
private String Roles;

/** Creates a new instance of UserBean */
public UserBean() {
}


/**
 * @return the Username
 */
public String getUsername() {
    return Username;
}


/**
 * @param Username the Username to set
 */
public void setUsername( String Username ) {
    this.Username = Username;
}


/**
 * @return the Password
 */
public String getPassword() {
    return Password;
}


/**
 * @return the Roles
 */
public String getRoles() {
    return Roles;
}


/**
 * @param Roles the Roles to set
 */
public void setRoles( String Roles ) {
    this.Roles = Roles;
}


/**
 * @param Password the Password to set
 */
public void setPassword( String Password ) {
    this.Password = Password;
}

change password also use the userBean and has his changePasswordBean.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:42:45+00:00Added an answer on June 18, 2026 at 3:42 pm

    JSF 2 allows you injecting managed beans in other managed beans. This is only limited by the scope of the bean you’re actually injecting, which has to be greater than the scope of the bean you actually are in. I mean, you can inject @SessionScoped bean in a @ViewScoped one, but not in the other way. As you follow that convention, I think you’re doing it well.

    Generally, your application should be composed by @ViewScoped or @RequestScoped beans to handle current user input/outputs and wider scoped beans for session or application means. So, when user logs in, it’s a good idea to maintain his data in a session context, however I suggest you not to maintain user’s password into session, at least if you’re not going to use it once the login has been succesfully done.

    Finally the question you make about search requests, I think you can implement a search input in your page and make your result table load dinamically depending on the search. Just use ajax to obtain it without having to reload the whole page. You can implement everything in a @ViewScoped bean, but remember not to return navigation results in your listener methods if you want to maintain the bean working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running a JSF 2.0 application on Tomcat 7. I want to use javascript
I try this example on Tomcat 7.0.23. The dialogs dialog and multiDialog are not
i want to use EL 2.2 for JSF 2 application and tomcat 7 is
I use JSF 2.0 and tomcat 7.0. To download file I use org.apache.commons.io.FileUtils.copyURLToFile(url, new
I'm working on a JSF 2.0 project using Mojarra, PrimeFaces and Tomcat 6.x, but
This is a rookie question, sorry. I developed a jsf-2 app (on Tomcat) which
i ve a jsf application which works fine on eclipse with tomcat 7. The
When i try to run my JSF application on Tomcat 7, it throws this
I'm running two webapps (based on Spring, JPA and JSF) on the same tomcat
I'm using Tomcat 7, MySql Workbench 5.2.27, JSF 2.0 and this exception comes from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.