0

I dont have too much experience with JSP. But i dont know why this code fails.

<%

String palabra=(String) request.getAttribute("expd");
System.err.print(palabra);
String fec=(String) request.getAttribute("fecha");
System.err.print(fec);
public  String contarCaracteres(String pa){

}

%>  

Illegal modifier for the variable contarCaracteres; only final is permitted.

Anyone can help?? Probably i need several imports of java in my jsp, but i dont know who classes import of java to prevent the error. Thx

Dekker
  • 85
  • 2
  • 10

2 Answers2

0

You can think of a JSP page as one giant "main" method that executes all the statements inside of it. So effectively what you are trying to do is create a method inside another method, which is not allowed. However you can do so if you separate it in its own block, like here:

http://www.java2s.com/Code/Java/JSP/CreatingaMethod.htm

AnthonyG
  • 11
  • 2
0

All the code you include in a scriptlet <%...%> goes to the _jspService() method of the class created from the JSP. The public identifier cannot be used with an automatic variable (the ones that are declared in a method) like contarCaracteres. For a quick solution remove the public modifier. However, probably it would be a good idea to include all this code on a Java bean.

Andres
  • 10,561
  • 4
  • 45
  • 63