Alternative servlet writing: Using Jython

Source: Internet
Author: User

As we all know, servlet is Java on the server sideProgramAn important part of the design, Servlet execution efficiency is high, suitable for the logic processing of the program, the disadvantage is compared to use
Tedious. Each servlet must be declared in Web. XML and the reload container is usually required after the modified and deployed Servet. This situation is very unfavorable for fast development,
This article introduces another Serlvet Development Method: Using Jython

This article assumes that the reader is familiar with certain application servers (such as Tomcat), Java and Python programming languages.

Download Jython on the http://www.jython.org, assuming D:/jython-2.1

Create a web application named Jython, Copy D:/jython-2.1/Jython. jar to the WEB-INF/lib under webapp, and configure web. XML as follows:
<Servlet>
<Servlet-Name> pyservlet </servlet-Name>
<Servlet-class>
Org. Python. util. pyservlet
</Servlet-class>
<Load-on-startup> 11 </load-on-startup>
<Init-param>
<Param-Name> python. Home </param-Name>
<Param-value> D:/jython-2.1 </param-value>
</Init-param>
<Init-param>
<Param-Name> python. Path </param-Name>
<Param-value> D:/jython-2.1/lib </param-value>
</Init-param>
</Servlet>

<Servlet-mapping>
<Servlet-Name> pyservlet </servlet-Name>
<URL-pattern> *. py </url-pattern>
</Servlet-mapping>

Now write a helloworld. py, as follows:

From javax. servlet. Http import httpservlet

ClassHelloworld(Httpservlet): # The Class Name and file name must be consistent
DefDoget(Self, request, response ):
Response. setcontenttype ("text/html; charset = UTF-8 ");
Out = response. getwriter ()
Print> out, "

Start the application server. Assume that the application server is located at localhost, port 8080, and access http: // localhost: 8080/Jython/helloworld. py.
"Hello world by Jython servlet !" What about string output?

As you can imagine, Jython servlet does not need to make any declaration in Web. xml. pyserlvet intercepts all. py requests and distributes them to their own Jython servlet,
After modifying the implementation of helloworld, you can immediately see the changes without restarting the application server. It is very convenient!

Note: strict indentation is required when writing helloworld. py, which is distinguished by indentation.CodeBlock language. Incorrect indentation is a syntax error.

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.