facebook

JSF with a Database driven bean [Closed]

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #219795 Reply

    bradm
    Member

    Two JSF questions: while I can get simple examples to work (where the managed bean doesn’t connect to a database, I have to restart Tomcat to see the results of every minor change I make. Is this normal?

    When I use a managed bean that gets its goods from a database, however, I get this error:

    Can’t instantiate class: ‘com.jsfdemo.bean.DataBean’.. class com.jsfdemo.bean.DataBean : java.lang.NullPointerException

    Now I extrapolated the same bean code and ran it as part of a console application and all was well… Here’s the bean code: the exception seems to be thrown on the rs = stmt.executeQuery() line:

    public class DataBean
    {
    Connection con;
    PreparedStatement pstmt;
    Statement stmt;
    ResultSet rs;
    String returnString = null;

    public DataBean() throws SQLException
    {
    login();
    }

    public Connection getConnection()
    {
    try
    {
    Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”);
    con = DriverManager.getConnection(“jdbc:microsoft:sqlserver://myserver:1433;DatabaseName=mydb;user=;password=”);
    }
    catch (SQLException ex)
    {
    System.out.println(ex.getMessage());
    }
    catch (java.lang.ClassNotFoundException e)
    {
    System.err.print(“ClassNotFoundException: “);
    System.err.print(e.getMessage());
    }
    System.out.println(“Connected”);
    return con;
    }

    public String getReturnString()
    {
    return returnString;//this is set by calling login() in the ctor
    }

    public void login() throws SQLException
    {
    try
    {
    if (con == null)
    {
    con = getConnection();
    }
    stmt = con.createStatement();
    rs = stmt.executeQuery(“SELECT Building_Name, Building_Number FROM Building_Numbers ORDER BY Building_Name”);
    ArrayList al = new ArrayList();//where we’ll store what we get from the database
    while (rs.next())
    {
    al.add(rs.getString(1) + “|” + rs.getString(2));
    }
    rs.close();
    returnString = al.toString();
    }
    catch (SQLException ex)
    {
    rs.close();
    returnString = “HOSED AGAIN”;
    }
    }
    }

    #219801 Reply

    Riyad Kalla
    Member

    I have to restart Tomcat to see the results of every minor change I make. Is this normal?

    No, make sure you are using an exploded deployment, JDK 1.4+ to Run Eclipse and Tomcat, atleast Tomcat 5.0 and have your webcontext or Tomcat setup to reload changes in your webapplication. Please see tomcat docs on how to do this.

    As far as your NPE, I would suggest setting breakpoints in your bean and walking through it, see whats happening.

    #219803 Reply

    bradm
    Member

    Debugging the bean gives me no errors: it only bails when I try to use it as part of a JSF application while testing on Tomcat…

    #219804 Reply

    bradm
    Member

    I just redeployed to Tomcat and all is well, but changes still aren’t registered until the server is restarted. Curious, because autodeploy is set to true…

    Here is the Tomcat doc on this:

    “If the host “autoDeploy” property is true, the host will attempt to deploy and update web applications dynamically, as needed. The host will need to have background processing enabled for automatic reloading to work, which is the default.”

    Any ideas?

    #219805 Reply

    Riyad Kalla
    Member

    Autodeploy is the wrong setting, that just handles if new WAR files are deployed, you need to use the Tomcat administrator to edit your web context for that web proejct and set it reloadable to true.

    #219806 Reply

    bradm
    Member

    Thanks. The administration site has been turned off by default in Tomcat 5 and you have to download it. I have, and I’ve placed the files where they belong, yet after restarting the server I’m still told it doesnt’ exist. After 30 minutes of searching about I confess I can’t find a single mention anywhere of manually installing the administrative web site… Can you help?

    #219807 Reply

    Riyad Kalla
    Member

    brad,
    The Tomcat Administrator is part of the Tomcat download. You have to ‘enable’ it by adding user(s) who belong to the ‘admin’ and/or ‘manager’ roles in your tomcat-users.xml file in your /conf dir, here is what a default one looks like:

    
    <?xml version='1.0' encoding='utf-8'?>
    <tomcat-users>
      <role rolename="tomcat"/>
      <role rolename="role1"/>
      <user username="tomcat" password="tomcat" roles="tomcat"/>
      <user username="role1" password="tomcat" roles="role1"/>
      <user username="both" password="tomcat" roles="tomcat,role1"/>
    </tomcat-users>
    

    You need to add a user, usually named “admin” with a password of your choice, that has the roles “admin” and “manager”, so something like this:

    
    <?xml version='1.0' encoding='utf-8'?>
    <tomcat-users>
      <role rolename="tomcat"/>
      <role rolename="role1"/>
      <user username="tomcat" password="tomcat" roles="tomcat"/>
      <user username="role1" password="tomcat" roles="role1"/>
      <user username="both" password="tomcat" roles="tomcat,role1"/>
      <user username="admin" password="admin" roles="admin,manager"/>
    </tomcat-users>
    

    There are two administrative apps that ship with Tomcat, 1 is the Tomcat Manager, used to start/stop/restart/remove/add web applications, the other is the Tomcat Administrator… used to configure tomcat and all running web applications. Each requires a user with the respective roles (admin and manager) so we made 1 user above that belongs to both. Now start Tomcat and navigate to http://localhost:8080, see the top two links at the left? Manager and Administrator? Now you can login to these both using admin/admin for the login and password. Play around with them and get a feel for them. Note that the manager is a good thing to keep open at all times so that if your web application comes out of sync with MyEclipse, instead of restarting Tomcat completely, just click “Restart” next to your webapp and Tomcat will just restart that 1 project, its a hell of a lot faster.

    Also play around with the Administrator, it takes some getting used to. You are looking for settings like “reloadable” and “reload changes”, things along those lines. They might already be set to true, there is an implicit 15-second timeout or so that you need to wait for Tomcat to reload changed files.

    Also make sure in ME you are using an exploded deployment, packaged deployment (WAR file) will not support hot-syncing changes to the server.

    #219810 Reply

    bradm
    Member

    Thanks Riyad. I feel better: those are exactly the steps I had taken with the tomcat-users.xml file.

    Unfortunately clicking on the Tomcat Administration link at the left still only leads to the same message: “Tomcat’s administration web application is no longer installed by default. Download and install the “admin” package to use it. ” This is version 5.5.4… Anybody else ever experienced this? Stopped and restarted the server; tried different browsers… stumped.

    #219812 Reply

    Riyad Kalla
    Member

    Ahhh, I’m using Tomcat 5.0.28, not 5.5. Maybe give 5.0.28 a try unless you need a JDK 5.0 app server?

    #219814 Reply

    bradm
    Member

    Thanks again Riyad!

    Yes: a JDK 5 app server is in the mix here… Naturally I’ll try 5.0.28 and see…

    #219821 Reply

    bradm
    Member

    I’ve completely removed any vestige of Tomcat 5.5 from my system and rebooted; installed Tomcat 5.0.29; started a fresh MyEclipse project; and tried the example code again. Now the message I get is “org.apache.jasper.JasperException: Unable to compile class for JSP”.

    I am just baffled here. I’m clearly running roughshod over some fundamental piece of the MyEclipse/JSF equation, but I just don’t see how. Any help would be appreciated.

    #219822 Reply

    Riyad Kalla
    Member

    You need to run Tomcat 5.0.x with a JDK not a JRE. Tomcat 5.5 includes its own compiler, so it can be run with a JRE.

    #219824 Reply

    bradm
    Member

    Thanks Riyad. My Tomcat installation is pointed at the 1.5 sdk: should I be using an earlier version?

    #219825 Reply

    bradm
    Member

    You’re certainly on the right track though: NO jsp will work at the moment…

    #219829 Reply

    Riyad Kalla
    Member

    JDK 5.0 should be fine, I’m not sure why its not working… try and load a JSP page, and when that fails open cataling.log and see what the entire exact stack trace is for the error.

Viewing 15 posts - 1 through 15 (of 17 total)
Reply To: JSF with a Database driven bean [Closed]

You must be logged in to post in the forum log in