0

We have a Java Web application running in IBM Websphere Liberty profile server. We have recently developed a Java Servlet which is responsible to generate JFreeCharts using Java library.

Code in web.xml

<servlet>
    <servlet-name>GraphicServlet</servlet-name>
    <servlet-class>com.test.GraphicServlet</servlet-class>
    <load-on-startup>10</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>GraphicServlet</servlet-name>
    <url-pattern>*.img</url-pattern>
</servlet-mapping>

Code in index.html file

<img src="summary/chart.img" height="100" width="100" />

Code in GraphicsServlet.java

This Servlet has doGet method implemented and the business logic in the method generates a chart image file(.png) using JFreeCharts and this image is returned as response in form of stream.

Application flow:

When application is accessed, index.html file is displayed by default and this page has a section where chart generated by JFreeChart needs to be displayed.

Scenario Description:

The Image file is not getting displayed when application is accessed. Using the browser's developer tools, I have found that 404 error is printed when a request is sent to *.img Url. Next, I tried to find out if that Servlet is really deployed on to the server or missed in the .war file by any chance, and I checked the deployment folder in websphere and found the Servlet deployed. Next, I tried to check if Servlet is up and running. From a new browser, I sent a test request like http://localhost:9080/myapp/summary/chart.img, it returned 404 error on to the screen.

I wanted to know why servlet is not getting up. So, I implemented the init() method of HttpServlet in my GraphicsServlet and written a print statement. I didnot get that print when the application finished loading. Finally, I tried with commenting out every line of doGet method, even then I received 404 Error on the screen.

Problem Description: I want to know why the GraphicsServlet is not getting up. Please let me know if there are any techniques to know the reason behind the failure of GraphicServlet.

Update 1: I tried removing the configuration in web.xml and annotated the GraphicServlet with @WebServlet annotation. Now, I am able to see those print statements and it is now confirmed that GraphicServlet is up and running when annotation is used. So, I started looking at web.xml DOCTYPE tag. The version of Web app DTD was very old. It was 2.3 version, and the Java version we installed is 1.8. Can anyone here let me know whats the problem with WebApp-2.3 ?

Update 2: I removed the @WebServlet annotation from GraphicServlet and again I tried with xml configuration and also changing the Web-app version from 2.3 to 3.1 (also 3.0) in web.xml file. The GraphicServlet is still not up and running.

Update 3: I have noticed that javaee-7.0 feature is being used in the Websphere server.xml file.

Thanks for your patience.

  • I've created the servlet with `@WebServlet("*.img")` annotation and it works perfectly fine. So remove `web.xml` from your app. Also if you are using Eclipse , install WebSphere Developer Tools from Eclipse Marketplace and it will generate correct deployment descriptors if you need them. Liberty only supports web apps 3.0 and higher, so that might be issues with your 2.3. – Gas May 24 '17 at 18:00
  • Thanks Gas. The application is very large and it requires huge effort to remove `web.xml` file. Can you please point me to some resource which clearly tells that Liberty only support web app 3.0 ? – DilipKumarC May 24 '17 at 19:14
  • So just update web.xml to 3.0. You dont have to remove it. And update application.xml if you have it in ear to at least Java EE 6. – Gas May 24 '17 at 20:30
  • I already tried with changing the web.xml to 3.0 and 3.1. This did not work. Please let me know a resource which clearly tells that Liberty only supports web app 3.0. – DilipKumarC May 25 '17 at 07:46

1 Answers1

1

Maybe you should change your servlet mapping in web.xml to be like this:

/summary/*.img

tsolakp
  • 5,858
  • 1
  • 22
  • 28
  • I tried even that but no use. However to confirm if that is causing the error, I created a dummy test application with dummy servlet which is mapped to *.img. The dummy servlet picked up all the requests Url which have .img in the ending, So, by this, I think that when servlet is mapped to *.img, any url ending with .img can be served by this servlet. – DilipKumarC May 22 '17 at 20:05
  • It is not necessary that servlet "init" method will be called on server startup. It can be lazy loaded and initialized when getting first request. Check to make sure you dont have errors in the log when servlet container is creating your actual servlet. – tsolakp May 22 '17 at 20:37