Jython 和实现 HttpServlet.contextInitialized
我希望我的 Jython servlet 实现 HttpServlet.contextInitialized
方法,但我不确定如何在 web.xml
中表达这一点。我目前拥有的是:
from javax.servlet import ServletContextListener;
from javax.servlet.http import HttpServlet
class JythonServlet1 ( HttpServlet, ServletContextListener ):
def contextInitialized( self, event ):
print "contextInitialized"
context = event.getServletContext()
def contextDestroyed( self, event ):
print "contextDestroyed"
context = event.getServletContext()
def doGet( self, request, response ):
print "doGet"
def doPost( self, request, response ):
print "doPost"
我的 web.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JythonTest</display-name>
<servlet>
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>JythonServlet1</display-name>
<servlet-name>JythonServlet1</servlet-name>
<servlet-class>JythonServlet1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
如您所见,在最后一个
条目中,我想使用上下文初始化 servlet(我可以在其中启动调度程序),但它的工作方式似乎与 Java servlet 不同。
I'd like my Jython servlet to implement the HttpServlet.contextInitialized
method but I'm not sure how to express this in the web.xml
. What I currently have is:
from javax.servlet import ServletContextListener;
from javax.servlet.http import HttpServlet
class JythonServlet1 ( HttpServlet, ServletContextListener ):
def contextInitialized( self, event ):
print "contextInitialized"
context = event.getServletContext()
def contextDestroyed( self, event ):
print "contextDestroyed"
context = event.getServletContext()
def doGet( self, request, response ):
print "doGet"
def doPost( self, request, response ):
print "doPost"
And my web.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JythonTest</display-name>
<servlet>
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>JythonServlet1</display-name>
<servlet-name>JythonServlet1</servlet-name>
<servlet-class>JythonServlet1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
As you can see, in the last <servlet>
entry I'd like to initialize the servlet with the context (where I can start a scheduler) but it doesn't seem to work the same as with a Java servlet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不使用 Jython,但是
HttpServlet
API。您可能正在寻找ServletContextListener
接口,通常作为以下基于 Java 的示例实现:
...在
web.xml
code> 如下:这一定会让您了解如何使用 Jython 来获取它。
您还可以选择让您的 servlet 扩展
HttpServlet
并实现ServletContextListener
,如下所示:这样您就可以得到您发布的代码(不要忘记导入特定接口并将您的类定义为
web.xml
中的 两者servlet
和listener
)。但这并不总是被认为是一个好的做法。也就是说,您应该将类放在包中以避免可移植性问题。它可能在某些环境中起作用,但在其他环境中不起作用。 Sun 也不鼓励在非原型环境中使用无包类。它们通常不能被包中的其他类导入。
I don't do Jython, but there's no means of
contextInitialized
orcontextDestroyed
methods in theHttpServlet
API. You're probably looking forServletContextListener
interface which is normally to be implemented as the following Java-based example:...which is to be definied as
<listener>
inweb.xml
as follows:This must give you an idea how to pickup it using Jython.
You can optionally also let your servlet both extend
HttpServlet
and implementServletContextListener
like follows:so that you can end up with the code you've posted (don't forget to import the particular interface and define your class as both
servlet
andlistener
inweb.xml
). But this is not always considered a good practice.That said, you should be placing classes in a package to avoid portability problems. It may work in some environments, but not in other. Sun also discourages using packageless classes in non-prototyping environments. They can normally namely not be imported by other classes which are itself in a package.
您确实需要编写一些 Java 引导程序,例如 PyServlet,它将 init() 分派给预定义的 python 脚本。
或者..如果你想使用 ServletContextListener 接口,那么像 Pyservlet 这样的东西也实现了 ServletContextListner ,并再次分派到一些 python 脚本。
我正在寻找类似的解决方案,并且非常失望地看到 PyServlet 本身没有提供类似的解决方案。
You really need to write some java bootstrapper like PyServlet that dispatches init() to a pre-defined python script.
Or.. if you want to use the ServletContextListener interface then something like Pyservlet that also implements ServletContextListner and again, dispatches to some python script.
I'm looking for a similar solution and was very disappointed to see that PyServlet doesn't offer anything like this itself.