Issue
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.
Solution
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).
Answered By - Armando Ballaci