在 JSP 页面上共享数据库连接和已加载的脚本、样式

发布于 2024-09-29 07:30:26 字数 565 浏览 4 评论 0原文

我目前正在尝试用 JSP 编写现有的桌面应用程序。

为了能够保持可持续性、视觉效果和功能性,我决定使用 JQuery。

一个月前开始编码后,我们现在意识到这些页面在每个页面中都会大量使用库(JQuery 和 JQueryUI 库、额外的插件和 CSS 文件)。另外我们每个进程都需要连接oracle数据库。

我想要的是一个主页(假设称为 Application.jsp)。这将

  1. 连接到数据库,保持连接活动(连接成功,并且只要我继续使用它,它就活动)
  2. 使用ajax调用子页面。子页面编码为原始 html。 (尝试使用Jquery加载()和get(),并成功)
  3. 在加载的子页面上应用JQuery函数和CSS(无法使其工作)
  4. 通过使用活动数据库连接使用getJSON()获取查询结果。 (也无法让这个工作,可能是因为 JQuery 在我加载的页面上不起作用,或者数据库连接可能无法共享)

如果,我可以以这种方式工作,这将节省我在每个页面中加载数百 kB 的库单个子页面调用。另外,我不需要为每个进程维护数据库连接。

任何帮助将不胜感激。谢谢...

I am currently trying to code our existing desktop application in JSP.

To be able to maintain sustainability, have visual effects and get functionality, I have decided to use JQuery.

After starting to code a month ago, we now realized that these pages are bringing a heavy usage of libraries (JQuery and JQueryUI libraries, extra plugins and CSS files) called in every single page. Also we need oracle database connection in every process.

What I want to have is a main page (lets say called Application.jsp). Which will

  1. Connect to the DB, keep the connection alive (the connection is successful and its alive as long as i keep using it)
  2. Calling the sub-pages with ajax. Sub-page is coded as raw html. (Tried load() and get() with Jquery, and successful)
  3. Applying the JQuery functions and CSS on the loaded sub-page (couldn't have it working)
  4. Getting the query result using getJSON() by using the alive DB connection. (Also couldn't have this working, might be because JQuery did not work on my loaded page or maybe database connection cannot be shared)

If, I can have this working in this way, that will save me loading hundreds of kB's libraries in every single sub-page call. Also i wont need to maintain a Database connection for every process.

Any help would be appreciated. Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2024-10-06 07:30:26

不要让自己的联系永远开放。当数据库保持打开时间过长并且您的应用程序将崩溃时,数据库将关闭它们。常见的做法是使用连接池。像样的 servletcontainers 提供了类似 JNDI 数据源的功能。连接池实现本身会担心以正确的方式获取和关闭实际连接。

目前尚不清楚您使用的是哪个 servletcontainer,因此这里只是一些特定于 Tomcat 的文档作为示例:

完成此操作后,更改 JDBC 代码中的连接管理器,将 DriverManager#getConnection() 替换为 DataSource#getConnection() 并保留JDBC 代码的剩余部分是可靠的(在尽可能短的范围内获取并关闭连接,始终关闭finally块中的所有资源)。如果您对此也不确定,您可以在此 基本 JDBC DAO 教程

Don't keep connections forever open yourself. The DB will close them when it's being kept open for too long and your application will break. The common practice is to use a connection pool. Decent servletcontainers offers facilities for this in flavor of a JNDI datasource. The connection pool implementation will itself worry about acquiring and closing the actual connection the right way.

It's unclear which servletcontainer you're using, so here are just some Tomcat-specific documents as example:

Once done that, change the connection manager in your JDBC code to replace DriverManager#getConnection() by DataSource#getConnection() and keep the remnant of your JDBC code solid (acquire and close the connection in shortest possible scope, always close all resources in finally block). If you're uncertain about that as well, you can find some insights in this basic JDBC DAO tutorial.

海螺姑娘 2024-10-06 07:30:26

在连接管理方面,绝对采用BalusC的推荐。查找应用程序服务器或容器的资源实现并在应用程序服务器中设置数据库连接。

将 JQuery 函数和事件处理程序应用到新的 AJAX 加载内容时,您将需要使用 jquery 选择器并重新应用之前应用于整个文档的任何内容,但您只能针对新加载的内容运行它,而不是整个文档树。

On the connection management, definitely go with BalusC's recommendation. Lookup your App Server or Container's resource implementation and setup the database connections in the app server.

On the application of JQuery functions and event handlers to new AJAX loaded content, you will need to use a jquery selector and reapply anything previously applied to the whole document, but you can run it only against the newly loaded contents, not the entire document tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文