0

I am trying to check for a file exists and then include the JSP file if true. But I am getting errors from below code.

Syntax error on tokens, delete these tokens

file.handler cannot be resolved to a type

Syntax error on token "=", . expected

This is my code:

String uri = request.getRequestURI();
String pageName = uri.substring(uri.lastIndexOf("/")+1);
String filename = pageName.replace(".jsp", "");

String path = request.getServletPath();  
path = path.replace( pageName , "");

String handler=path+"handler/"+filename+"_handler.jsp";

if(null != application.getResource(handler)){
    <%@include file="${handler}"%>
}

I am a PHP programmer but I am new to JSP. Please advice

Community
  • 1
  • 1
Leon Armstrong
  • 1,285
  • 3
  • 16
  • 41

3 Answers3

1

You might try a dispatcher to include a resource

if(null != application.getResource(handler)){
    request.getRequestDispatcher(handler).include(request, response);
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

Try with JSP Expression something like:

<%@include file="<%=handler %>" %>
SMA
  • 36,381
  • 8
  • 49
  • 73
0

What about <jsp:include page="${handler}"/>?

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Markus W Mahlberg Dec 25 '14 at 12:02
  • 1
    @Markus How is this a critique or clarification request? It is a one line answer to a simple question. – Predrag Maric Dec 25 '14 at 12:16
  • 1
    Asking the OP actually does neither shows that you are sure your answer will work, nor do you explain why the OP should try it or how this uncommented code snippet relates to the original question. Other than that, my comment is a prefabricated comment of the review system, which was the one most suitable when I flagged your answer for deletion. – Markus W Mahlberg Dec 25 '14 at 12:31
  • The only thing that is wrong with this answer is that it's in a form of a question. I could rephrase it to something like "Try this..." but it doesn't essentially change anything. "Not sure if it works", well I have plenty of pages where this code snippet works perfectly, does that mean it will work anywhere? Obviously not, because it didn't work for the OP. By that logic, all answers other than accepted ones should be deleted. – Predrag Maric Dec 25 '14 at 12:39