This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Make your JSp compatible with Java 7 standards. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Make your JSp compatible with Java 7 standards." Watch "Make your JSp compatible with Java 7 standards." New topic
Author

Make your JSp compatible with Java 7 standards.

Anshum Kocher
Greenhorn

Joined: Nov 26, 2012
Posts: 5
I got this error, how could i resolve it


org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 12 in the jsp file: /disp.jsp
Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum constants are permitted
9: <body>
10: <%
11: String s=(String)request.getAttribute("null");
12: switch(s){
13: case "true":
14: out.println("Cool new session");
15: break;

Benny Zlo
Greenhorn

Joined: Jan 28, 2012
Posts: 7

As specified in the error - you cannot use switch on strings before Java 7. Change the compliance level or change the code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
With an If/else statement. Of course, Java code should not be part of a JSP, so if want to keep logic in a JSP you should use the corresponding JSTL tags instead.


Ping & DNS - my free Android networking tools app
Anshum Kocher
Greenhorn

Joined: Nov 26, 2012
Posts: 5
How would i Change the compliance level of the code , currently i am using default settings in eclipse [eclipse newbie alert]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Step 1: Use JSTL instead of Java scriptlets in the JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
The default Tomcat JSP compiler will compile to Java 1.6. As such you can't use Strings in a switch statement which is a Java 7 feature.
Check out the Tomcat documentation for how to specify which compiler is used for JSP pages.
Of particular interest should be the parameters for

- compiler
- compilerTargetVM

Setting compilerTargetVM to 1.7 should allow the page to compile as is.
However the better solution as already mentioned is not to use scriptlets in your JSP page.

 
Don't get me started about those stupid light bulbs.
 
subject: Make your JSp compatible with Java 7 standards.