如何从作为 Web 应用程序插件一部分的 servlet 中访问 .js 文件?
How to access .js file from within a servlet which is part of a web application plugin?
我找不到访问 javascript 文件的方法,该文件放在我试图扩展的插件的项目文件夹中。不幸的是, 提供的解决方案无效。我希望这不是特定于我正在为其编写插件的 webapp(可以说它称为 thiswebappname
)。
我首先扩展了一个现有的示例插件,它具有以下项目结构:
项目结构
myprojectsnamespace.myproject
- JRE 系统库
- 插件依赖项
- 来源
- myprojectsnamespace.myproject
- MyServlet.java
- ...
- 元信息
- 网络应用
- WEB-INF
- javascript
- myScript.js
- web.xml
- 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));
//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>"); */
}
当我在浏览器中输入 http://myserver/thiswebappname/LoadScript
时,doGet()
会按预期从 MyServlet
调用,但脚本不会加载。我错过了一些明显的东西吗?我还没有找到像在 web.xml.
中使用 MyServlet 那样“发布”.js 文件的方法
您可以使用:
ServletContext context = getContext();
URL resourceUrl = context.getResource("/WEB-INF/javascript/myScript.js");
或者如果您只想要输入流:
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/javascript/myScript.js");
即使 Servlet 容器从不扩展 WAR 文件(如 Tomcat),这仍然有效。
我找不到访问 javascript 文件的方法,该文件放在我试图扩展的插件的项目文件夹中。不幸的是,thiswebappname
)。
我首先扩展了一个现有的示例插件,它具有以下项目结构:
项目结构
myprojectsnamespace.myproject
- JRE 系统库
- 插件依赖项
- 来源
- myprojectsnamespace.myproject
- MyServlet.java
- ...
- myprojectsnamespace.myproject
- 元信息
- 网络应用
- 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));
//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>"); */
}
当我在浏览器中输入 http://myserver/thiswebappname/LoadScript
时,doGet()
会按预期从 MyServlet
调用,但脚本不会加载。我错过了一些明显的东西吗?我还没有找到像在 web.xml.
您可以使用:
ServletContext context = getContext();
URL resourceUrl = context.getResource("/WEB-INF/javascript/myScript.js");
或者如果您只想要输入流:
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/javascript/myScript.js");
即使 Servlet 容器从不扩展 WAR 文件(如 Tomcat),这仍然有效。