简体   繁体   English

像静态资源一样,是否有可能服务放置在Webapp之外的JSP

[英]Like static resource, is it possible to serve JSPs which are placed out side of webapps

I am using Struts2 in my application with Tomcat. 我在使用Tomcat的应用程序中使用Struts2。

I am serving static resource of my application by placing them outside of webapps with setting in server.xml . 我通过将server.xml设置放置在server.xml之外,从而为我的应用程序提供静态资源。

Now I have to serve JSPs in same way, which are outside of my context. 现在,我必须以同样的方式来服务JSP,这超出了我的上下文。 Is it possible? 可能吗? Or is there any way to achieve this? 还是有什么办法可以做到这一点?

You can place JSPs anywhere, but the web application should be aware of the location of the resources. 您可以将JSP放置在任何地方,但是Web应用程序应注意资源的位置。

Since you didn't pointed where exactly you want to store JSPs, I couldn't tell you how to do that. 由于您没有指出要存储JSP的确切位置,因此我无法告诉您如何执行此操作。 I can say that resources are retrieved via URL in Java, if you can point the server to some URL where resources are stored, then you can access these resources. 我可以说,资源是通过Java中的URL检索的,如果您可以将服务器指向存储资源的某些URL,则可以访问这些资源。

If you need to learn more about how in Java accessing resources you can read Location-Independent Access to Resources . 如果您需要了解有关Java如何访问资源的更多信息,可以阅读位置无关的资源访问

Resources, names, and contexts 资源,名称和上下文

A resource is identified by a string consisting of a sequence of substrings, delimited by slashes (/), followed by a resource name. 资源由包含子字符串序列的字符串标识,该子字符串序列由斜杠(/)分隔,后跟资源名称。 Each substring must be a valid Java identifier. 每个子字符串必须是有效的Java标识符。 The resource name is of the form shortName or shortName.extension . 资源名称的格式为shortNameshortName.extension Both shortName and extension must be Java identifiers. shortNameextension必须是Java标识符。

The name of a resource is independent of the Java implementation; 资源的名称与Java实现无关。 in particular, the path separator is always a slash (/). 特别是,路径分隔符始终为斜杠(/)。 However, the Java implementation controls the details of how the contents of the resource are mapped into a file, database, or other object containing the actual resource. 但是,Java实现控制如何将资源内容映射到文件,数据库或其他包含实际资源的对象的细节。

The interpretation of a resource name is relative to a class loader instance. 资源名称的解释是相对于类加载器实例的。 Methods implemented by the ClassLoader class do this interpretation. ClassLoader类实现的方法进行此解释。


The difference between static resources and dynamic resources: 静态资源和动态资源之间的区别:

  • static resources could be run directly by browser 静态资源可以由浏览器直接运行
  • dynamic resources (like JSPs) are run on server. 动态资源(如JSP)在服务器上运行。

Don't confuse loading dynamic resources the same way as static resources, because the static resources could be returned with the response, but dynamic resources should be returned to the server. 不要混淆以与静态资源相同的方式加载动态资源,因为可以将静态资源与响应一起返回,但是应该将动态资源返回给服务器。

JSPs get compiled into servlets (hence java classes) that are executed by a JSP container using a fairly complex life-cycle, which you can read about here: https://www.tutorialspoint.com/jsp/jsp_life_cycle.htm JSP被编译为servlet(因此为Java类),该Servlet由JSP容器使用相当复杂的生命周期执行,您可以在此处了解有关该生命周期的信息: https : //www.tutorialspoint.com/jsp/jsp_life_cycle.htm

So the short answer is: no. 因此,简短的答案是:不。 You can't serve JSP pages outside your web app, since one of the primary functions of that web app is to compile and execute JSPs. 您不能在Web应用程序外部提供JSP页面,因为该Web应用程序的主要功能之一是编译和执行JSP。

As the other answer suggests, you can access java resources using a variety of mechanisms, but you are always going to need something that looks like a JSP container to render JSPs. 正如另一个答案所建议的那样,您可以使用多种机制来访问Java资源,但是始终需要看起来像JSP容器的东西来呈现JSP。

You'll have to move onto a different technology if you want to serve more of your app from something like a content delivery network. 如果要通过内容交付网络等服务来提供更多应用程序,则必须采用其他技术。

You might look into creating a purely client side app that talks to the back end via REST, graphql, or web sockets. 您可能会考虑创建一个通过REST,graphql或Web套接字与后端通信的纯客户端应用程序。

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

相关问题 Spring Boot是否可以通过独立的JAR包装提供JSP服务 - Is it possible with Spring Boot to serve up JSPs with a stand alone JAR packaging 我应该如何构建jsps使用的资源包属性文件? - How should I structure resource bundle property files which are used by jsps? 如何使用Guice 3.0提供JSP? - How to serve JSPs using Guice 3.0? 静态资源在servlet路径之类的文件夹中不可用 - Static resource not avaliable in folder like servlet path JSP,静态内容,SpringBoot jar和IntelliJ - JSPs, static content, SpringBoot jars and IntelliJ 我可以从lib中的JAR内部提供JSP,还是有解决方法? - Can I serve JSPs from inside a JAR in lib, or is there a workaround? 声明一个js变量,该变量将被我的2个jsps访问 - Declare a js variable which will be accessed my 2 jsps 如何将放置在war中的UI资源(如CSS,Images和Js文件)用作放置在单独war中的源jsp文件? - How to use UI resources like CSS, Images and Js files which are placed inside a war into the source jsp files that are placed in a separate war? 使用静态字符串在JSP中定义输入字段名称-好主意吗? - Using static strings to define input field names in JSPs - good idea or not? 如何删除预编译的 JSP 并仅使用 JSP 来提供文件 - how to remove pre-compiled JSPs and just use JSP to serve files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM