在servlet中运行java代码之前加载资源 [英] Loading resources before running java code in servlet

查看:22
本文介绍了在servlet中运行java代码之前加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tomcat 中有一个 servlet.后端的java代码执行需要很长时间.有没有办法与后端的代码并行加载静态资源(css、images、javascript)?现在,只有在代码运行完毕后才会加载它们.

I have a servlet in tomcat. It takes a really long time for the java code in the backend to execute. Is there a way to load static resources (css,images,javascript) in parallel with the code in the backend? Right now, they are only loaded once the code finishes running.

推荐答案

您可以使用 Ajax 风格的解决方案,您可以在没有数据的情况下绘制页面,使用占位符来检索数据,甚至可以使用加载"微调图形.

You could use an Ajax-style solution where you paint your page without data, with a placeholder for retrieving the data, maybe even with a "loading" spinner graphic.

Ajax 调用的工作方式是,当页面加载时,一些 Javascript 将触发,通过 XmlHttpRequest 向 Tomcat 发起 Ajax 请求,从而开始计算.当tomcat请求完成时,浏览器会通知浏览器.然后网页中会有一些 javascript 将接收响应并替换占位符.如果服务器返回一个 HTML 片段,它就像在 javascript placeholder-div.innerHtml = your-response-text 中执行一样简单.

The way that an Ajax call works, when the page is loaded, some Javascript will fire that will launch an Ajax request to Tomcat via XmlHttpRequest that will start the calculation. The browser will notify the browser when the tomcat request is completed. Then there will be some javascript in the webpage that will take the response and replace the placeholder. If the server returns an HTML fragment, it's as simple as executing in javascript placeholder-div.innerHtml = your-response-text.

这里有一个 Ajax 基础教程 和一个 基于 Java 的示例,具有与 Java Servlet 后端通信的 Web 前端.

Here's a basic tutorial on Ajax and a Java-based example that has the web front-end communicating with a Java Servlet back-end.

这篇关于在servlet中运行java代码之前加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆