如何从 Web 应用程序插件中的 Servlet 中访问 .js 文件?

huangapple go评论100阅读模式
英文:

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
      • ...
  • META-INF
  • webapp
    • 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));
    //上述行找不到文件。我还尝试了"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
      • ...
  • META-INF
  • webapp
    • WEB-INF
      • javascript
        • myScript.js
    • web.xml
  • 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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?eclipse version=&quot;3.0&quot;?&gt;
&lt;plugin&gt;
   &lt;extension
         point=&quot;com.thiswebappname.portal.tomcat.webapps&quot;&gt;
      &lt;webapp
            contextRoot=&quot;webapp&quot;
            name=&quot;thiswebappname/myprojectsnamespace&quot;/&gt;
   &lt;/extension&gt;
&lt;/plugin&gt;

I'm somehow not able to load the content of myScript.js from MyServlet. The servlet is kinda published by the web.xml:

&lt;...&gt;
&lt;servlet&gt;
  &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
  &lt;display-name&gt;My Servlet&lt;/display-name&gt;
  &lt;servlet-class&gt;myprojectnamespace.myproject.MyServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/LoadScript&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

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(&quot;text/html&quot;);
    resp.getWriter().println(&quot;&lt;script&gt;&quot;);
    resp.getWriter().println(new String(Files.readAllBytes(Paths.get(&quot;getPopup.js&quot;)),StandardCharsets.UTF_8));
    //above line doesn&#39;t find the file. I also tried &quot;myprojectsnamespace.myproject/webapp/WEB-INF/javascript/myScript.js&quot; etc., same problem
    resp.getWriter().println(&quot;&lt;/script&gt;&quot;);

    /* following approach has the same problem, i.e. can&#39;t find the file:
    resp.setContentType(&quot;text/html&quot;);
    resp.getWriter().println(&quot;&lt;script language=&#39;text/javascript&#39; src=&#39;myScript.js&#39;&gt;&quot;);
    resp.getWriter().println(&quot;&lt;/script&gt;&quot;); */
}

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(&quot;/WEB-INF/javascript/myScript.js&quot;);

or alternatively if you just want the input stream:

InputStream resourceContent = context.getResourceAsStream(&quot;/WEB-INF/javascript/myScript.js&quot;);

This works even if the Servlet Container never expands the WAR file (like Tomcat).

huangapple
  • 本文由 发表于 2020年8月4日 22:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63248802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定