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.