"&的Servlet QUOT; (服务器端)GWT中的初始化代码

编程入门 行业动态 更新时间:2024-10-27 22:31:32
本文介绍了"&的Servlet QUOT; (服务器端)GWT中的初始化代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在GWT应用程序的服务器端进行一次初始化?

我可能会想到很像 HttpServlet ,您可以在其中覆盖 init(),但旧习惯很久就会失去;)

我试图做的是:

  • 加载一堆属性

  • 建立与数据库的连接

  • >

解决方案

加载一堆属性? b

注册ServletContextListener以在服务器启动时加载Init参数。

加载属性并使其可见其他类是静态的。

我已经发布了一个示例代码在Servlet外部检索Init参数

建立与数据库的连接?

使用JNDI绑定 数据源。

使用Connection Utility类获取连接,并在完成连接后关闭连接。

这里是示例代码。

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(); 返回连接; } $ b $ public void close(Connection connection)throws SQLException { if(connection!= null&&!connection.isClosed()){ connection 。关(); } connection = null; 如果你使用 JBOSS在独立模式下。然后只需在 standalone.xml 中执行一些条目来创建数据源。只需根据数据库连接更新它的一些值,如 connection-url ,用户名和 password 。

在这种情况下,JNDI_LOOKUP_NAME将是 java:jboss / datasources / oracle

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(), but old habits are long to lose ;)

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.

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

<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>

更多推荐

&QUOT;&的Servlet QUOT; (服务器端)GWT中的初始化代码

本文发布于:2023-08-07 14:25:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   服务器端   代码   QUOT   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!