1

I'm having trouble accessing the Arrays class in a jsp. I want to make a basic page that lists the substrings in the storeNames parameter, i.e.:

http://localhost:8080/JSPExample/index.jsp?storeNames=John,Mary,Gary

should show

John
Mary
Gary

Here is my source code:

index.jsp:

<%@ page import="java.util.Arrays,java.util.List" %>

    <html>
    <body>
        This is my first web page
        <%
            String storeNames = request.getParameter("storeNames");
            List<String> namesList = Arrays.asList(storeNames.split(","));
        %>
        <p><b><%= namesList.get(0) %></b></p>
        <p><b><%= namesList.get(1) %></b></p>
        <p><b><%= namesList.get(2) %></b></p>
    </body>
    </html>

and what i get when running the jsp:

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

An error occurred at line: 6 in the generated java file
The import java.util.Arrays cannot be resolved

An error occurred at line: 8 in the jsp file: /index.jsp
Arrays cannot be resolved
5:     This is my first web page
6:     <%
7:         String storeNames = request.getParameter("storeNames");
8:         List<String> namesList = Arrays.asList(storeNames.split(","));
9:     %>
10:     <p><b><%= namesList.get(0) %></b></p>
11:     <p><b><%= namesList.get(1) %></b></p>

Stacktrace:

Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I know using scriptlets is considered bad practice, but for now just please tell me how i can resolve this import problem.

Thank you!

Jens
  • 67,715
  • 15
  • 98
  • 113
Alex Bugnar
  • 19
  • 1
  • 3
  • I would also recommend checking if `storeNames` is NULL before creating the `namesList` to avoid a NPE – Ascalonian Feb 03 '15 at 16:29
  • Of course, and thanks for the suggestion, but for now I'm just struggling to get the import to work... – Alex Bugnar Feb 03 '15 at 16:35
  • Please check these links: http://stackoverflow.com/questions/24490663/java-util-arrays-not-working-java-8 – ZakiMak Feb 03 '15 at 16:44
  • http://stackoverflow.com/questions/16918959/import-cannot-be-resolved [dont forget to check the comments, they might help] – ZakiMak Feb 03 '15 at 16:45
  • How about just using the fully qualified name in the scriptlet? Does that work? `java.util.Arrays.asList(storeNames.split(","));` – bdkosher Feb 03 '15 at 20:13

1 Answers1

-2

Switch to

Java-7 from Java-8

as running environment of JBoss.
This solved my problem.

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94