简体   繁体   中英

Calling Java Method from Servlet 2.5

I got this little code snippet from one of my JSP files:

 <c:when test="${not empty param['filePath'] && not empty param['revision']}">
<c:out value="${sessionScope.fileHelper.getContentsForPath(param.filePath,param.revision)}" escapeXml="false"/>
</c:when>

Unfortunately I have to migrate back to Servlet 2.5 , currently I was using 3.0 .

The problem with this is, that EL (Expression Language) does not support calling methods like this in prior versions. So I asked me how to accomplish the same thing with 2.5 compatible code.

The fileHelper gets added to the sessionScope in a different JSP file like:

<jsp:useBean id="fileHelper"
    class="de.myPackage.util.FileHelper" scope="session" />

What I tried was:

<%@ page import="de.myPackage.util.FileHelper"%>

<c:when test="${not empty param['filePath'] && not empty param['revision']}">
<c:out value="<%=(FileHelper)session.getAttribute("fileHelper").getContentsForPath(request.getParameter("filePath"),(String)request.getParameter("revision"))%>" escapeXml="false"/>
</c:when>

But this doesn't work since it writes:

The method getContentsForPath(String, String) is undefined for the type Object.

Any ideas ?

You have to wrap the cast session object within () to use it's .getContentsForPath() method. Something like this:

((FileHelper) session.getAttribute("fileHelper")).getContentsForPath(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM