-1

I am trying to setup a embedded jetty 9.3.9 server, have configured the JSP support for the Jetty server - not very well experienced with this stuff, however, whenever I try to load any JSP page, I receive -

Unable to compile class for JSP: ||An error occurred at line: [1] in the generated java file: [/tmp/jetty-0.0.0.0-8080-webapp-_webMvc-any-4185149399836239637.dir/jsp/org/apache/jsp/WEB_002dINF/views/index_jsp.java]|The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files||An error occurred at line: [1] in the generated java file: [/tmp/jetty-0.0.0.0-8080-webapp-_webMvc-any-4185149399836239637.dir/jsp/org/apache/jsp/WEB_002dINF/views/index_jsp.java]|The type java.io.ObjectInputStream cannot be resolved|Syntax error on token "<", ? expected after this token|

My JSP support in ServerStart.java is:

`public static void main(String[] args) throws Exception {

    Server server = new Server(8080);
    System.setProperty("org.apache.jasper.compiler.disablejsr199", "false");

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setDescriptor("/webMvc/src/main/webapp/WEB-INF/web.xml");
    webAppContext.setConfigurations(new Configuration[] {
                            new AnnotationConfiguration(),
                            new WebInfConfiguration(),
                            new WebXmlConfiguration(),
                            new MetaInfConfiguration(),
                            new FragmentConfiguration(),
                            new EnvConfiguration(),
                            new PlusConfiguration(),
                            new JettyWebXmlConfiguration()
                            });

    webAppContext.setAttribute("javax.servlet.context.tempdir", getScratchDir());
    webAppContext.setResourceBase("/webMvc/src/main/webapp/");
    webAppContext.setContextPath("/webMvc");
    webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
                            ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/.*taglibs.*\\.jar$");

     webAppContext.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
     webAppContext.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
     webAppContext.addBean(new ServletContainerInitializersStarter(webAppContext), true);
     webAppContext.setClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader()));                    
     webAppContext.setParentLoaderPriority(true);       


    ProtectionDomain protectionDomain = ServerStart.class.getProtectionDomain();
    URL location = protectionDomain.getCodeSource().getLocation();
    webAppContext.setWar(location.toExternalForm());

    server.setHandler(webAppContext);        
    server.start();
    server.join();
}        

private File getScratchDir() throws IOException {
        File tempDir = new File(System.getProperty("java.io.tmpdir"));
        File scratchDir = new File(tempDir.toString(), "webMvc-jetty-jsp");
        scratchDir.mkdirs();
        return scratchDir;
}

private ServletHolder jspServletHolder() {
    ServletHolder holderJsp = new ServletHolder("jsp", JettyJspServlet.class);
                    holderJsp.setInitOrder(0);
                    holderJsp.setInitParameter("logVerbosityLevel", "INFO");
                    holderJsp.setInitParameter("fork", "false");
                    holderJsp.setInitParameter("xpoweredBy", "false");
                    holderJsp.setInitParameter("compilerTargetVM", "1.8");
                    holderJsp.setInitParameter("compilerSourceVM", "1.8");
                    holderJsp.setInitParameter("keepgenerated", "true");
                    return holderJsp;
}`

My Project Structure is - `

jettyStartup
   |-src/main/java
     |- ServerStart.java
webMvc
   |- src/main/java
   | |- Package com.admin.controllers
   |    |- BaseController
   |- src
     |- main
       |- webapp
         |- META-INF
         |- WEB-INF
           |- web.xml
           |- views
             |-index.jsp
             |-error.jsp

`

I have a separate project for Starting Server and another for MVC (for being modular) I create the war file and feed it to jetty server. Everything works fine until I hit a jsp page.

My index.jsp is:

<%@ page import="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter" %>
<%@ page import="org.springframework.security.core.AuthenticationException" %>

<!DOCTYPE html>
    <head>
        <link href="css/themes/jquery-ui.css" rel="stylesheet" type="text/css"/> 
        <link href="css/themes/page-theme.css" rel="stylesheet" type="text/css"/> 
        <link href="css/index.css" rel="stylesheet" type="text/css">
        <link href="css/jquery.modaldialog.css" rel="stylesheet" type="text/css">

        <script type="application/javascript" src="js/jquery/jquery-1.4.4.js"></script>
        <script type="application/javascript" src="js/jquery/jquery-ui-1.8.10.custom.js"></script>
        <script src="js/index.js" type="text/javascript"></script>          
    </head>
    <body>
        <div id="page-login" >
                <form id="login" method="POST" action="j_spring_security_check" autocomplete="off">
                    <div id="login-credentials">                                    
                        <label>Username:
                                <input type="text" name="j_username" id="j_username"/>
                        </label>
                        <label> Password:
                                <input type="password" name="j_password" id="j_password" />
                                <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                        </label>
                    </div>
                    <input type="submit" id="login-btn" value="Log in" /> 
                    <div style="clear:right"></div>
                </form>
        </div>        
    </body>
</html>
devop
  • 49
  • 2
  • 12

1 Answers1

1

You've got methods inside of methods, or you've got code outside of any method. Taking a small piece of your code... This, for example, will never compile:

          ProtectionDomain protectionDomain = ServerStart.class.getProtectionDomain();
            URL location = protectionDomain.getCodeSource().getLocation();          
            webAppContext.setWar(location.toExternalForm());

            private File getScratchDir() throws IOException {
                File tempDir = new File(System.getProperty("java.io.tmpdir"));
                File scratchDir = new File(tempDir.toString(), "webMvc-jetty-jsp");
                scratchDir.mkdirs();
                return scratchDir;
            }

It's the same as doing something like this:

public static void main(String[] args){
    public void file(){}
}

You can't have methods inside methods.

It's nice you tried to use private methods, but you can't do what you're trying to do in a JSP.

You need to put all the code in-line, with no methods. Preferably, you shouldn't put any Java code inside a JSP.

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • Thanks for figuring it out and apologies for the confusion, I have edited the java code to be more clear. The java code in the question is used to start the embedded-jetty server. This is not the part of any JSP. – devop Dec 02 '16 at 00:21
  • Then you need to include the file that is causing the error: `An error occurred at line: [1] in the generated java file: [/tmp/jetty-0.0.0.0-8080-webapp-_webMvc-any-4185149399836239637.dir/jsp/org/apache/jsp/WEB_002dINF/views/index_jsp.java` Which would indicate index.jsp is not correct – Christopher Schneider Dec 02 '16 at 14:06
  • It's a basic login page. I get that error on all the jsp pages I try to load. I have attached my index.jsp. All other jsp pages are similar to it. – devop Dec 02 '16 at 15:24