从Java类调用doGet Servlet方法 [英] Invoke the doGet Servlet method from java class

查看:640
本文介绍了从Java类调用doGet Servlet方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我得到一个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>

解决方案

您应该重构代码,以将此功能移出servlet并移到可以从servlet和计划的作业中访问的通用类. /p>

I have a scheduled job class, implements the Quartz Job class, from which I want to invoke a Servlet class that updates a flag in a DB table and then it sends emails to an appropriate emailing list.

I get a java.net.ConnectException. Though the servlet is properly invoked by either entering its URL in the browser or by JavaScript in a JSP page.

My Java class is the following:

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();
        }
    }
}

My servlet code is:

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);
    }
}

The exception I get is:

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)

I am using Tomcat 5.5.

I have seen lots of other relative posts but did not help, I also tried with HttpClient but the same exception.

Could anyone figure out what the problem is?

The servlet configuration on the web.xml is:

<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>

解决方案

You should refactor your code to move this functionality out of the servlet and into a common class that can be reached both from your servlet and from your scheduled job.

这篇关于从Java类调用doGet Servlet方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆