英文:
How to access .js file from within a servlet which is part of a web application plugin?
问题
我找不到一种访问放在插件项目文件夹中的JavaScript文件的方法,我正在尝试扩展这个插件。这个主题提供的解决方案不幸地没有起作用。我希望这不仅限于我正在为其编写插件的Web应用程序(假设它叫做 thiswebappname
)。
我首先是在现有的示例插件的基础上进行扩展,该插件具有以下项目结构:
项目结构
myprojectsnamespace.myproject
- JRE系统库
- 插件依赖项
- src
- myprojectsnamespace.myproject
- MyServlet.java
- ...
- myprojectsnamespace.myproject
- META-INF
- webapp
- WEB-INF
- javascript
- myScript.js
- javascript
- web.xml
- WEB-INF
- build.properties
- plugin.xml
以下是我怀疑可能有助于找到解决方案的一些文件:
build.properties
source.my-servlet.jar = src/
src.includes = my-servlet.jar
bin.includes = META-INF/,\
webapp/,\
plugin.xml
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
<extension
point="com.thiswebappname.portal.tomcat.webapps">
<webapp
contextRoot="webapp"
name="thiswebappname/myprojectsnamespace"/>
</extension>
</plugin>
我以某种方式无法从 MyServlet
中加载 myScript.js
的内容。该Servlet 在 web.xml
中发布如下:
<...>
<servlet>
<servlet-name>MyServlet</servlet-name>
<display-name>My Servlet</display-name>
<servlet-class>myprojectnamespace.myproject.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/LoadScript</url-pattern>
</servlet-mapping>
在 MyServlet.java
中,我尝试了以下方法,但都没有成功:
MyServlet.java
public class MyServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp ) throws IOException{
resp.setContentType("text/html");
resp.getWriter().println("<script>");
resp.getWriter().println(new String(Files.readAllBytes(Paths.get("getPopup.js")), StandardCharsets.UTF_8));
//上述行找不到文件。我还尝试了"myprojectsnamespace.myproject/webapp/WEB-INF/javascript/myScript.js"等等,但问题相同
resp.getWriter().println("</script>");
/* 以下方法也有相同的问题,即找不到文件:
resp.setContentType("text/html");
resp.getWriter().println("<script language='text/javascript' src='myScript.js'></script>"); */
}
当我在浏览器中输入 http://myserver/thiswebappname/LoadScript
时,doGet()
会如预期地从 MyServlet
中调用,但脚本不会被加载。我是否遗漏了一些显而易见的东西?我还没有找到一种类似于在 web.xml
中发布 MyServlet
的方式来“发布” .js
文件。
英文:
I can't find a way to access a javascript file which I put in the project folder of a plugin which I am trying to extend. The provided solution from this topic unfortunately didn't work. I hope this is not specific to the webapp (lets say its called thiswebappname
) I am writing the plugin for.
I started by extending on an existing example plugin, which has following project structure:
Project structure
myprojectsnamespace.myproject
- JRE System Library
- Plug-in Dependencies
- src
- myprojectsnamespace.myproject
- MyServlet.java
- ...
- myprojectsnamespace.myproject
- META-INF
- webapp
- WEB-INF
- javascript
- myScript.js
- javascript
- web.xml
- WEB-INF
- build.properties
- plugin.xml
here some of the files which I suspect might be helpful for finding a solution:
build.properties
source.my-servlet.jar = src/
src.includes = my-servlet.jar
bin.includes = META-INF/,\
webapp/,\
plugin.xml
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
<extension
point="com.thiswebappname.portal.tomcat.webapps">
<webapp
contextRoot="webapp"
name="thiswebappname/myprojectsnamespace"/>
</extension>
</plugin>
I'm somehow not able to load the content of myScript.js
from MyServlet
. The servlet is kinda published by the web.xml
:
<...>
<servlet>
<servlet-name>MyServlet</servlet-name>
<display-name>My Servlet</display-name>
<servlet-class>myprojectnamespace.myproject.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/LoadScript</url-pattern>
</servlet-mapping>
In MyServlet.java
I tried the following, all without success:
MyServlet.java
public class MyServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp ) throws IOException{
resp.setContentType("text/html");
resp.getWriter().println("<script>");
resp.getWriter().println(new String(Files.readAllBytes(Paths.get("getPopup.js")),StandardCharsets.UTF_8));
//above line doesn't find the file. I also tried "myprojectsnamespace.myproject/webapp/WEB-INF/javascript/myScript.js" etc., same problem
resp.getWriter().println("</script>");
/* following approach has the same problem, i.e. can't find the file:
resp.setContentType("text/html");
resp.getWriter().println("<script language='text/javascript' src='myScript.js'>");
resp.getWriter().println("</script>"); */
}
When I enter http://myserver/thiswebappname/LoadScript
in a browser, doGet()
does get called from MyServlet
as expected, but the script doesn't get loaded. Am I missing something obvious? I haven't found a way to "publish" the .js file like I did with MyServlet in the web.xml.
答案1
得分: 1
你可以使用以下代码:
ServletContext context = getContext();
URL resourceUrl = context.getResource("/WEB-INF/javascript/myScript.js");
或者,如果你只需要输入流:
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/javascript/myScript.js");
即使Servlet容器从不扩展WAR文件(例如Tomcat),这也可以工作。
英文:
You could use:
ServletContext context = getContext();
URL resourceUrl = context.getResource("/WEB-INF/javascript/myScript.js");
or alternatively if you just want the input stream:
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/javascript/myScript.js");
This works even if the Servlet Container never expands the WAR file (like Tomcat).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论