简体   繁体   English

使用Java Servlet扩展现有的Webapp

[英]Expanding an existing webapp with Java Servlets

I have an existing and working web app. 我有一个现有且可以正常使用的网络应用。 This web app is a combination of HTML, CSS, Javascript, various Javascript libraries (Jquery etc.), images, and fonts. 该Web应用程序是HTML,CSS,Javascript,各种Javascript库(Jquery等),图像和字体的组合。

Using Tomcat and a Java servlet, I want to be able to deploy this web app. 我希望使用Tomcat和Java Servlet来部署此Web应用程序。 I also need to be able to serve up different web pages based on the URL (that's where the servlets come in, I think). 我还需要能够根据URL提供不同的网页(我认为这就是servlet的所在)。

I found this SO post which shows how to deploy a web app using Tomcat through Tomcat's webapps directory. 我找到了这篇SO帖子该帖子显示了如何通过Tomcat的webapps目录使用Tomcat部署Web应用程序。 This works perfectly, except that it only displays the one index.html file, and there is no way to change the displayed HTML based upon the user entered URL. 除了只显示一个index.html文件,而且无法根据用户输入的URL更改显示的HTML之外,这非常理想。

I also found this other SO post which describes how to return static HTML from a Java servlet. 我还找到了另一篇SO文章 ,它描述了如何从Java servlet返回静态HTML。 The problem with this, is that it only returns the HTML file, and none of the supporting files (Javascript, CSS, images etc.) which are also necessary for the web app to function properly. 这样做的问题是,它返回HTML文件,而没有任何支持文件(Javascript,CSS,图像等),这些文件也是Web应用程序正常运行所必需的。

Is it possible for the servlet to return all necessary files along with the HTML file, so that the web app functions properly? Servlet是否可以返回所有必需的文件以及HTML文件,以便Web应用程序正常运行?

if you just want to deliver static content, then tomcat might not be the the beset option. 如果您只想提供静态内容,那么tomcat可能不是beset选项。 However, static file delivery should just work, if you put your files somewhere in the root folder of your webapp (NOT in WEB-INF). 但是,如果将文件放在Web应用程序的根文件夹中的某个位置(在WEB-INF中不是),则静态文件传递应该可以正常工作。 Also, your web.xml file could be configured in a way that it will serve static content (depending on your servlet mapping). 同样,可以通过配置web.xml文件的方式来提供静态内容(取决于servlet映射)。 So there's no simple answer to give you. 因此,没有简单的答案可以给您。 could you possible past your web.xml file here? 您能否在这里过去您的web.xml文件?

(as per request - made this an answer) (根据要求-将此作为答案)

You do not have to specify the static content inside the servlet. 您不必在Servlet中指定静态内容。 Use your favorite HTML editor and design a webpage and include all the css/scripts in the html code. 使用您喜欢的HTML编辑器并设计一个网页,并将所有CSS /脚本包含在html代码中。 Then, send the user to that html with sendRedirect() or RequestDispatcher through the servlet. 然后,通过Servlet使用sendRedirect()或RequestDispatcher将用户发送到该html。

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

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