简体   繁体   English

GWT中的“ Servlet”(服务器端)初始化代码

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

How can I have a single time initialization on a server side of GWT app? 如何在GWT应用程序的服务器端进行一次一次性初始化?

I may be thinking to much like HttpServlet where you can override init() , but old habits are long to lose ;) 我可能在想像HttpServlet一样,可以在其中重写init() ,但是旧的习惯很容易失去;)

What I am trying to do is: 我正在尝试做的是:

  • load a bunch of properties 加载一堆属性

  • establish a connection to the database 建立与数据库的连接

load a bunch of properties? 加载一堆属性?

Register ServletContextListener to load Init parameters at server start-up. 注册ServletContextListener以在服务器启动时加载Init参数。

Load properties and make it visible to other classes statically. 加载属性并使它对其他类静态可见。

I have already posted a sample code Retrieve Init parameters outside servlet 我已经发布了一个示例代码, 在servlet外部检索Init参数。


establish a connection to the database? 建立与数据库的连接?

Use JNDI to bind the data source. 使用JNDI绑定数据源。

Use Connection Utility class to get the connection as well as close the connection as soon as its done. 使用Connection Utility类获取连接以及在完成连接后立即将其关闭。

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 . 如果您JBOSS in standalone mode使用JBOSS in standalone mode Then just do some entries in standalone.xml to create a data source. 然后只需在standalone.xml进行一些输入即可创建数据源。 Just update some of its value as per your database connection such as connection-url , user-name and password . 只需根据您的数据库连接更新其某些值,例如connection-urluser-namepassword

In this case JNDI_LOOKUP_NAME will be java:jboss/datasources/oracle 在这种情况下,JNDI_LOOKUP_NAME将为java:jboss/datasources/oracle

<datasource jta="true" jndi-name="java:jboss/datasources/oracle" pool-name="OracleDS" enabled="true" use-java-context="true" use-ccm="true">
    <connection-url>jdbc:oracle:thin:@ipaddress:1521/sid</connection-url>
    <driver>oracle</driver>
    <new-connection-sql>select * from dual</new-connection-sql>
    <pool>
        <min-pool-size>20</min-pool-size>
        <max-pool-size>50</max-pool-size>
        <prefill>true</prefill>
    </pool>
    <security>
        <user-name>username</user-name>
        <password>password</password>
    </security>
    <validation>
        <check-valid-connection-sql>select * from dual</check-valid-connection-sql>
        <background-validation>true</background-validation>
    </validation>
    <timeout>
        <blocking-timeout-millis>30000</blocking-timeout-millis>
        <idle-timeout-minutes>1</idle-timeout-minutes>
        <use-try-lock>60</use-try-lock>
        <allocation-retry>1</allocation-retry>
    </timeout>
</datasource>

You can add like mentioned in a comment a ServletContextListener . 您可以添加注释中提到的ServletContextListener

public class ServerConfig implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
    // Do stuff on startup.
    }

    public void contextDestroyed(ServletContextEvent event) {
    // Do stuff on shutdown.
    }
}

Now put the new class on the server side, you also ave to register the Listener in your web.xml file : 现在,将新类放在服务器端,您还需要在Web.xml文件中注册Listener:

<listener>
<listener-class>path.to.class.ServerConfig</listener-class> 
</listener>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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