Home > Mobile >  JSP-Error: Only a type can be imported. com.ibm.ws.webcontainer.webapp.WebAppErrorReport resolves to
JSP-Error: Only a type can be imported. com.ibm.ws.webcontainer.webapp.WebAppErrorReport resolves to

Time:03-20

I am migrating a project from Websphere server to OpenLiberty-21.0.0.1 along with other technical stack. While running the application , in one of the JSP page it gives me runtime error as bellow:

Only a type can be imported. com.ibm.ws.webcontainer.webapp.WebAppErrorReport resolves to a package

There is an import present for this class in the .jsp page as bellow :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
   <%@page import="com.ibm.ws.webcontainer.webapp.WebAppErrorReport"%>
   
    <html>
    <head>

I suspect this error is because the required jar is not present in my server, so I tried searching a jar with the name starting something like com.ibm.ws.webcontainer.webappin /lib directory of my Openliberty installation, but I was unable to find. I tried searching if any maven dependency is available for the same, but I am unable to find .

Any help would be appriciated.

OR Any workaround for bellow code would also fine:

WebAppErrorReport errorReport = (WebAppErrorReport)request.getAttribute("ErrorReport");
Throwable cause = errorReport.getCause();

CodePudding user response:

The com.ibm.ws.* package contains application-server-internal APIs that are not for application use. These classes were visible in traditional WebSphere, but are not in Liberty.

Add fragments of your jsp where this class is used, so one can suggest workaround.

You can also check for sources of WebAppErrorReport here https://github.com/OpenLiberty/open-liberty/blob/integration/dev/com.ibm.ws.webcontainer/src/com/ibm/ws/webcontainer/webapp/WebAppErrorReport.java to write workaround by yourself.

This class in general is used for error handling/formatting so it should not be very difficult to replace it (depending how it is used in your app).

UPDATE:

The easy, clean Java EE way would be to:

  1. Define your jsp as error page in web.xml:
    <error-page>
        <location>/error.jsp</location>
    </error-page>   
  1. Define your page as "error page" by adding isErrorPage="true" at the top. This adds implicit exception object to the page.
<%@ page language="java" isErrorPage="true"%>
  1. Use the exception object. So instead of:
WebAppErrorReport errorReport = (WebAppErrorReport)request.getAttribute("ErrorReport");
Throwable cause = errorReport.getCause();

you just use exception e.g:

out.println("Exception: "   exception.getMessage());

the exception is the cause object from your code.

If you want some more details about attributes available in error pages check this: How to get the message in a custom error page

And if you really must use WebAppErrorReport, which I would strongly discourage, you can use ugly reflection trick:

Object myReport = request.getAttribute("ErrorReport");
Class myClass = myReport.getClass();
Method myMethod = myClass.getMethod("getCause", (Class[])null);
Throwable cause = (Throwable)myMethod.invoke(myReport, (Object[])null);

  • Related