Enable the inline jetty server to support JSP

Source: Internet
Author: User

What is 1.jetty?

Jetty is a lightweight Web server, similar to Tomcat, but more flexible than Tomcat, especially for inline use. The so-called inline type starts jetty in the form of Java statements so that we can achieve the same effect without having to deploy and start the Web container. This is much more convenient for simple HTTP protocol-based applications and debugging programs.

2. A simple jetty server

Simple to just like the following statements:

public class Jettysample {public static void main (string[] args) throws Exception{server server=new Server (8087); Context Context=new Context (server, "/"); Resourcehandler resource_handler=new Resourcehandler (); Resource_handler.setwelcomefiles (New String[]{"index.html" }); Resource_handler.setresourcebase ("."); Context.sethandler (Resource_handler); Server.setstopatshutdown (true); Server.start ();}}

When run as Java application is selected, a Web server with a port number of 8087 is started.


Of course, the above example can only parse the HTML file, such as the need to parse the servlet and JSP needs some other work. Servlet Fortunately said, debugging JSP cost a bit hard, the following is the recording process.

3. Tried a lot of different methods, read many of the posts, and finally I found that parsing the JSP and parsing the servlet method is not only by adding a few statements (such as Context.addservlet (new  helloservlet ()),  ) can be done, and a so-called Web application must be built, so the format is slightly different from the code above.

public static void main (String[] args)  throws  exception{server server = new server (); Connector connector = new selectchannelconnector (); Connector.setport (8080); Server.setconnectors (NEW CONNECTOR[] { CONNECTOR });  webappcontext webappcontext  = new webappcontext ("WebContent", "/"); Webappcontext.setdescriptor ("Webcontent/web-inf/web.xml" ); Webappcontext.setresourcebase ("webcontent"); Webappcontext.setdisplayname ("Jetty"); Webappcontext.setclassloader (Thread.CurrentThread (). Getcontextclassloader ()); Webappcontext.setconfigurationdiscovered (True); Webappcontext.setparentloaderpriority (true); Server.setHandler ( Webappcontext); Try{server.start ();} catch (Exception e) {}} 

Here we create folders according to the standard specifications of the WEB application WebContent, Web-inf, and Web.xml,web.xml are also written in the prescribed format, even if the simplest one is to write a paragraph

<welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.jsp</ Welcome-file></welcome-file-list>

after launching the JSP page in the browser, found still error: JSP Support Not Configured
For a long time, I found that this was the case when I introduced the jetty package with a few jetty jar files under the plugins directory in Eclipse, which is not enough. Only a complete jetty was extracted and all the jar files in the lib/jsp directory were introduced. execution, or error, but this time changed: a full JDK (not just JRE) is required. The reason is that the JSP needs to be compiled into an implied servlet to execute, all requiring a full JDK. This solution is relatively simple--run Configuration, select Jre,alternate JRE, Installed JREs, then add a new JRE and point to a JDK directory.


After some toss, finally make the embedded jetty can parse JSP file.

This article is from the "Empty" blog, make sure to keep this source http://6738767.blog.51cto.com/6728767/1614226

Enable the inline jetty server to support JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.