“Servlet” (server-side) initialization code in GWT

后端 未结 2 938
清歌不尽
清歌不尽 2020-11-28 16:04

How can I have a single time initialization on a server side of GWT app?

I may be thinking to much like HttpServlet where you can override init()

2条回答
  •  情话喂你
    2020-11-28 16:56

    load a bunch of properties?

    Register ServletContextListener to load Init parameters at server start-up.

    Load properties and make it visible to other classes statically.

    I have already posted a sample code Retrieve Init parameters outside servlet


    establish a connection to the database?

    Use JNDI to bind the data source.

    Use Connection Utility class to get the connection as well as close the connection as soon as its done.

    Here is the sample code.

    import java.sql.Connection;
    import java.sql.SQLException;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    import com.woodmac.datavisualizer.shared.DVConstants;
    
    public class ConnectionUtil {
    
        private DataSource dataSource;
    
        private static ConnectionUtil instance = new ConnectionUtil();
    
        private ConnectionUtil() {
            try {
                Context initContext = new InitialContext();
                dataSource = (DataSource) initContext.lookup(JNDI_LOOKUP_NAME);
    
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
    
        public static ConnectionUtil getInstance() {
            return instance;
        }
    
        public Connection getConnection() throws SQLException {
            Connection connection = dataSource.getConnection();
            return connection;
        }
    
        public void close(Connection connection) throws SQLException {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
            connection = null;
        }
    }
    

    If you are using JBOSS in standalone mode. Then just do some entries in standalone.xml to create a data source. Just update some of its value as per your database connection such as connection-url, user-name and password.

    In this case JNDI_LOOKUP_NAME will be java:jboss/datasources/oracle

    
        jdbc:oracle:thin:@ipaddress:1521/sid
        oracle
        select * from dual
        
            20
            50
            true
        
        
            username
            password
        
        
            select * from dual
            true
        
        
            30000
            1
            60
            1
        
    
    

提交回复
热议问题