0

I have a JavaBean that is deployed in a Java web application which is based on a complex workflow framework such as CMN, BPMN, and DMN. The point here is that my JavaBean is invoked from a BPMN and it seems to me like it is in a context of a rest API endpoint because I can test the deployed JavaBean from postman. When I invoke the JavaBean from the BPMN, I don't see any reference to ServletContext or JSP or request object or anything like that.

I need to modify the JavaBean to add some feature that requires loading json data file from a data folder under the web root path.

I did build a sample web application successfully using dynamic Java web application and JavaBean and was able to access the web root path from the request object, and I was able to load the json data file as intended.

I need now to do the same from the real Java web application framework. The only issue I'm facing is how to access the web root path if i don't have access to HttpServlet and the other related objects.

I used the APIs below in a sample web application and they worked successfully:

Request.getsession (). Getservletcontext (). Getrealpath ();
Servletconfig.getservletcontext (). Getrealpath ("/");

See the related repo below for the sample project to clarify the question:

https://github.com/tarekahf/JavaBeanExample2/tree/master

In the project presented in the above repo, I'm loading a JSON file from a folder under the webroot. I was able to do so because I have access to the request object and HttpServletContext. See the source code in the following files in the above repo:

  • src\com\app\EmployeeClass.java
  • WebContent\data\list.json
  • WebContent\index.jsp

The question now is how to access the ServletContext if I don't see it defined anywhere in the main java application?

If the JavaBean is invoked from the context of a rest API execution thread, how I can access the webroot path in this JavaBean so that I can load the needed JSON data file under the root path?

Tarek

tarekahf
  • 738
  • 1
  • 16
  • 42

1 Answers1

1

If the JavaBean is CDI managed, you can inject the ServletContext with:

@Inject
private ServletContext;

——

If you just want to read a json file in the java code, I would say that you maybe look at this: read ressource from folder


Edit:

Since it is found that the application is based on Spring, then the appropriate method to inject a servlet into a Bean is by using the @Autowired annotation or implementing the ServletContextAware interface. See this for details: https://roytuts.com/how-to-get-servletcontext-and-servletconfig-object-in-a-spring-bean/

Also, this is another resource for how to read files in Java:

https://www.baeldung.com/reading-file-in-java

tarekahf
  • 738
  • 1
  • 16
  • 42
Jelmen
  • 173
  • 9
  • Thank you so much. What if the JavaBean is not CDI managed? Can you give me some checks to do to find out if the JavaBean is CDI managed? – tarekahf May 15 '22 at 07:22
  • Maybe this helps: https://stackoverflow.com/questions/15520424/what-is-a-cdi-bean – Jelmen May 15 '22 at 07:24
  • Is there a method if it's not CDI managed? – tarekahf May 15 '22 at 16:59
  • Could you provide some more informations about the context you need to use the SessionContext? Add some code to your question, than it is easier to help you – Jelmen May 15 '22 at 19:15
  • Sure, check the updated question. I gave included a repo and I'll update it with the latest version soon. – tarekahf May 16 '22 at 03:05
  • I have updated the repo with the latest code. Check the updates to the question for details. – tarekahf May 16 '22 at 06:26
  • Please note that I have added the `@Inject...` as you mentioned and I got a compile this error in eclipse `Inject cannot be resolved to a type`. – tarekahf May 16 '22 at 07:02
  • What is your setting and your goal? You mention a BPMN framework - how is it related to the question? – Jelmen May 17 '22 at 12:39
  • A BPMN is a Process that runs on the server (not a UI component) and invokes the JavaBean. In other words, the JavaBean must exist within a Servlet Container since the BPMN can be tested from Postman and it runs under Tomcat. So, there must be a way to access the Servlet from JavaBean. I just discovered yesterday that this application uses Spring, so I will try using `@Autowired private ServletContext servletContext;` – tarekahf May 17 '22 at 14:16
  • Ok - that the project is using Spring is an important information. `@Autowired` is the (more or less) equivalent to Java EEs/CDI `@Inject` – Jelmen May 17 '22 at 16:08
  • Which library do I have to import to use `@Inject`? From where I can get the related JAR file? – tarekahf May 17 '22 at 19:00
  • I think you need to read more about the ideas of Java EE and/or Spring and the underlaying application server. The injection is coming from the container implementation of the EE standard (or in Springs case the autowiring). If you just want to read a json file in the java code, I would say that you maybe look at this: [read ressource from folder](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) – Jelmen May 18 '22 at 21:21
  • Thanks a lot! I've already done the reading and looks like I have a solution and I need to try it out. The link you provided is very useful. – tarekahf May 19 '22 at 20:29
  • Nice - I would add the resource information to the answer. Maybe you could accept the answer, than the question is marked as “done” – Jelmen May 19 '22 at 20:52
  • Thanks, I accepted your answer. I have also edited it, once approved please review and update it as needed. – tarekahf May 19 '22 at 21:24