【问题标题】:Invoke the doGet Servlet method from java class从 java 类调用 doGet Servlet 方法
【发布时间】:2012-11-12 05:00:32
【问题描述】:

我有一个计划的作业类,实现了 Quartz Job 类,我想从中调用一个 Servlet 类来更新数据库表中的标志,然后它将电子邮件发送到适当的电子邮件列表。

我收到了java.net.ConnectException。尽管通过在浏览器中输入其 URL 或在 JSP 页面中通过 JavaScript 正确调用 servlet。

我的 Java 类如下:

public class ExpirationJob implements Job {
    org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ExpirationJob.class");

    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            URL serv = new URL("http://localhost:8080/app/emailExpirationServlet");
            URLConnection sr = serv.openConnection();
            sr.connect();
            InputStream is = sr.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader in = new BufferedReader(isr);

            String inputLine;
            int i =0;
            while ((inputLine = in.readLine()) != null)
                i = i +1;
            log.debug("Input line: " + inputLine);
            in.close();
        } catch (MalformedURLException e) {
            log.debug("Error while calling the emailExpirationServlet -- MalformedURLException: "+ e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.debug("Error while calling the emailExpirationServlet -- IOException: "+ e.getMessage());
            e.printStackTrace();
        }
    }
}

我的 servlet 代码是:

public class emailExpireServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
    org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("emailExpireServlet.class");

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        log.debug("Initializing emailExpireServlet");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        log.debug("Starting emailExpireServlet");

        //do some business logic here - I have commented it out to rule out any other blocking issue
        response.setStatus(200);
        response.getOutputStream().print("Invoked emailExpireServlet! Status is OK");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

我得到的例外是:

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:525)
    at java.net.Socket.connect(Socket.java:475)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
    at schedulers.ExpirationJob.execute(Unknown Source)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)

我使用的是 Tomcat 5.5。

我看过很多其他相关帖子但没有帮助,我也尝试过HttpClient,但同样的例外。

谁能找出问题所在?

web.xml上的servlet配置是:

<servlet>
    <servlet-name>emailExpireServlet</servlet-name>
    <servlet-class>emailExpireServlet</servlet-class>
</servlet>
...
<servlet-mapping>
    <servlet-name>emailExpireServlet</servlet-name>
    <url-pattern>/emailExpireServlet</url-pattern>
</servlet-mapping>

【问题讨论】:

  • 检查一下:执行石英作业的应用程序与 tomcat 服务器在同一台机器上运行,对吧?
  • 在浏览器中打开网址localhost:8080/app/emailExpirationServlet,会出现什么?
  • 另外,您不应该将 GET 用于非幂等操作。
  • 关闭防火墙并绑定 inet 地址。
  • @JB 是的,它在同一台机器上。

标签: java web-applications servlets httpurlconnection urlconnection


【解决方案1】:

您应该重构您的代码,以将此功能从 servlet 中移出并移到一个公共类中,该类既可以从您的 servlet 也可以从您的计划作业中访问。

【讨论】:

  • 这是我的下一步。实际上这个 servlet 是遗留代码,它曾经是从 jsp/javascript 调用的。但我还是忍不住想知道哪里出了问题。这似乎是一种常见的做法,我不知道为什么我不能让它按预期工作。
  • 连接失败。我们无法为您提供帮助;您的代码使用了错误的 IP 地址或端口。
  • 我目前正在 localhost:8080 本地测试这个(或使用 IP)。这会出什么问题?
  • 重构确实按预期工作。我对 servlet 调用的问题似乎是一个奇怪的连接问题。谢谢大家的回答。
【解决方案2】:

URLConnection 打开指向 URL 所引用资源的通信链接,如果尚未建立这样的连接URL.openConnection() 返回一个 URLConnection 对象,该对象表示与 URL 引用的远程对象的连接。 每次都会打开一个新连接,方法是为此 URL 调用协议处理程序的 openConnection 方法。

【讨论】:

  • 使用 /emailExpirationServlet
  • 感谢您的回复。我将 url 改为 127.0.0.1:8080/app/emailExpireServlet 并再次出现相同的异常。
  • 删除这个sr.connect();
  • 我删除了它,当我试图在输入流“InputStream is = sr.getInputStream();”中获取连接响应时出现异常我认为这可能与 Windows 7 中的套接字/端口有关...
猜你喜欢
  • 1970-01-01
  • 2015-06-02
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 2011-08-18
  • 2012-02-27
相关资源
最近更新 更多