Jython 和实现 HttpServlet.contextInitialized

发布于 2024-08-21 13:08:12 字数 2086 浏览 3 评论 0原文

我希望我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-08-28 13:08:12

我不使用 Jython,但是 HttpServlet API。您可能正在寻找 ServletContextListener接口,通常作为以下基于 Java 的示例实现:

package com.example;

import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        // ...
    }
    public void contextDestroyed(ServletContextEvent event) {
        // ...
    }
}

...在 web.xml code> 如下:

<listener>
    <listener-class>com.example.MyServletContextListener</listener-class>
</listener>

这一定会让您了解如何使用 Jython 来获取它。

您还可以选择让您的 servlet 扩展 HttpServlet 并实现 ServletContextListener,如下所示:

public class MyServlet extends HttpServlet implements ServletContextListener {
    // ...
}

这样您就可以得到您发布的代码(不要忘记导入特定接口并将您的类定义为 web.xml 中的 两者 servletlistener)。但这并不总是被认为是一个好的做法。

也就是说,您应该将类​​放在包中以避免可移植性问题。它可能在某些环境中起作用,但在其他环境中不起作用。 Sun 也不鼓励在非原型环境中使用无包类。它们通常不能被包中的其他类导入。

I don't do Jython, but there's no means of contextInitialized or contextDestroyed methods in the HttpServlet API. You're probably looking for ServletContextListener interface which is normally to be implemented as the following Java-based example:

package com.example;

import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        // ...
    }
    public void contextDestroyed(ServletContextEvent event) {
        // ...
    }
}

...which is to be definied as <listener> in web.xml as follows:

<listener>
    <listener-class>com.example.MyServletContextListener</listener-class>
</listener>

This must give you an idea how to pickup it using Jython.

You can optionally also let your servlet both extend HttpServlet and implement ServletContextListener like follows:

public class MyServlet extends HttpServlet implements ServletContextListener {
    // ...
}

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 and listener in web.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.

ゞ花落谁相伴 2024-08-28 13:08:12

您确实需要编写一些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文