Let the embedded Jetty9.4.3 support JSP

First give a simple embedded Jetty program

package mp.http.server;

import org.eclipse.jetty.server.Server;< br />import org.eclipse.jetty.webapp.WebAppContext;

/**
*
* @author zkpursuit
*/
public class HttpServer {

public static void main(String[] args) throws Exception {
WebAppContext webapp_context = new WebAppContext();
webapp_context.setDescriptor("./src/webapp/ WEB-INF/web.xml");
webapp_context.setResourceBase("./src/webapp");
webapp_context.setContextPath("/");
webapp_context.setParentLoaderPriority(true) ;
webapp_context.setClassLoader(Thread.currentThread().getContextClassLoader());
Server server = new Server(8080);
server.setStopAtShutdown(true);
server. setHandler(webapp_context);
server.start();
server.join();
}

}

The base class of WebAppContext is ServletContextHandle r, so it naturally supports Servlet, but what about JSP? JSP will eventually be compiled into a Servlet to run. On the Internet, the latest version of Jetty supports JSP by default, but I reported an error directly when I tested it.

org.apache.jasper.JasperException: Unable to compile class for JSP< /pre> 

I have misunderstood it. The default Web/Servlet container that supports JSP should be the default Web/Servlet container that comes with Jetty. I haven't tested it, so I can't draw a conclusion.

The following will continue to discuss how to make the above code run to support jsp.

From the above error, it can be seen that the jsp file is not compiled, and compiling jsp to Servlet requires additional libraries. From the lib folder of the downloaded jetty, it contains the apache-jsp folder. Import all the jar packages and add them.

webapp_context.addServlet(JettyJspServlet.class, "*.jsp");

If you run it again, it still reports the error that cannot be compiled. I was depressed, and then I checked the information and finally found the following method

public static class JspStarter extends AbstractLifeCycle implements ServletContextHandler.ServletContainerInitializerCaller {

JettyJasperInitializer sci;
ServletContextHandler context;

public JspStarter(ServletContextHandler context) {
this.sci = new JettyJasperInitializer();
this.context = context;
this.context.setAttribute("org .apache.tomcat.JarScanner", new StandardJarScanner());
}

@Override
protected void doStart() throws Exception {
ClassLoader old = Thread. currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(context.getClassLoader());
try {
sci.onStartup(null, context.getServletContext());
super.doStart();
} finally {
Thread.currentThread().setContextClassLoader(old) ;
}
}
}

Then use this class and add it to the top main method

 webapp_context.addBean(new JspStarter(webapp_context));
webapp_context.addServlet(JettyJspServlet.class, "*.jsp");

Run again, I can hehe, finally I can run jsp , You're done

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4596 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.