Alternative Writings for Servlet: Using Jython

xiaoxiao2021-03-06  48

As we all know, servlet is an important part of Java's server-side programming. The servlet performs high efficiency, suitable for the logic processing of the program, the disadvantage is that it is more cumbersome, each servlet must be declared in Web.xml and when modified After servet, the RELOAD container is generally required. This situation is very disadvantageous for the development speed development. This article introduces an additional Server Vet development method: use Jython

This article assumes readers familiar with some application server (such as Tomcat), Java and Python programming language

Upload Jython on http://www.jython.org, assuming D: /JYTHON-2.1

Create a web application named Jython, put D: /JYTHON-2.1/JYTHON.JAR COPY under web-inf / lib under WebApp, and configure web.xml as follows: pyservlet org.python.util.pyservlet 11 Python. Home D: /JYTHON-2.1 python.path < PARAM-VALUE> D: /JYTHON-2.1/LIB

pyservlet *. py

Now write a HelloWorld.py as follows:

From javax.servlet.http import httpservlet

Class HelloWorld (httpservlet): # Requires class name and file name unanimous DEF Doget (Self, Request, Response): Response.setContentType ("text / html; charset = UTF-8"); OUT = response.getwriter () Print >> OUT, "

Hello World by Jython Servlet! ";

Start the application server, assume that you are located in localhost, port 8080, access http: // localhost: 8080 / jython / helloworld.py, is it to see the "Hello World By Jython Servlet!" String output?

As you imagined, Jython Servlet does not need to make any declarations in web.xml, and PyserLvet will intercept all .py requests and distribute to their respective Jython servlets, and after modifying the HelloWorld implementation, you can see change, no need to restart Application server, very convenient! Note: When writing helloWorld.py, please strictly indumb, Python is a language that indent the code block, incorrect indentation is a syntax error

转载请注明原文地址:https://www.9cbs.com/read-54180.html

New Post(0)