0

I have a situation where I have a series of similar JSPs, each of which is called from a servlet based upon an option entered by a user.

However, I would like to adjust these JSPs so that they can be additionally called in batch from a program which runs hourly on the server, and write the JSP output to text file.

Can anyone tell me how this might be done at all?

I am thinking along the lines of:

URL url = new java.net.URL("http://127.0.0.1/myServlet");
URLConnection con = url.openConnection();

Or is there a better way?

OK: I must be doing something very foolish here because this doesn't appear to work: I have a batch program which runs every hour and it contains the following code:

try {
        URL url = new java.net.URL("http://127.0.0.1:8084//myApp//myServletMapping?par=parValue");
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setDoInput(true);
        InputStream response = connection.getInputStream();
   }
   catch (Exception ex) {
        logger.error("Error calling servlet in batch", ex);
   }

According to my understanding of the instructions in this tutorial, the above should be enough to trigger the get method in the servlet which is mapped to by myServletMapping in the code above. This servlet's get method contains a simple System.out.println("Here"); which I would expect to see.

What am I doing wrong?

Community
  • 1
  • 1
Mr Morgan
  • 307
  • 3
  • 5
  • 13
  • You're never actually reading from the input stream. – Matt Ball Jun 30 '11 at 19:06
  • I've been thinking in terms of the doGet method actually being called. So how do I get it to do something? I've just tried System.out.println(response.toString()); but nothing. – Mr Morgan Jun 30 '11 at 19:10
  • BTW `setDoInput(true)` is unnecessary since the default is `true`. – Matt Ball Jun 30 '11 at 19:13
  • `response.toString()` does not just read the response into a string. You need to actually **[read the `InputStream`](http://stackoverflow.com/search?q=java+read+inputstream).** – Matt Ball Jun 30 '11 at 19:15
  • I've removed this and have tried an OutputStream for doPost but nothing appears to work. What am I missing? – Mr Morgan Jun 30 '11 at 19:15
  • Don't worry about output streams and POSTing for now. Removing the `setDoInput(true)` call does not mean POST. You just need to read the data out of `response`. – Matt Ball Jun 30 '11 at 19:17
  • InputStream response = connection.getInputStream(); int i = 0; while ((i = response.read()) != -1) { System.out.println("Here " + i); } – Mr Morgan Jun 30 '11 at 19:23
  • @MattBall let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1026/discussion-between-mr-morgan-and-matt-ball) – Mr Morgan Jun 30 '11 at 19:24
  • @Matt Ball: many thanks for your guidance. The problem turned out to be in the servlet mappings. – Mr Morgan Jun 30 '11 at 19:57

6 Answers6

3

Or is there a better way?

Not really. That's about as basic as it gets. The servlet is "called" when the server receives an HTTP, and that's exactly what your proposed code will do.

You could use a library such as HTTPUnit, or a different programming language, but it's all going to boil down to sending an HTTP request.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Thanks. Assuming I call an OutputStreamWriter in the JSP via a scriptlet, is there anything else I need to close down when the servlet has run? I'm using Tomcat as servlet container. – Mr Morgan Jun 30 '11 at 17:06
  • [There's a mini tutorial here at SO.](http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests) Why are you using scriptlets, though? `:(` – Matt Ball Jun 30 '11 at 17:09
  • In this case, I need the JSPs to generate Javascript containing data which is required for sending requests to the Google Visualisation API. So I'm using scriptlets to do this. – Mr Morgan Jun 30 '11 at 17:14
3

In addition to what Matt said about the servlet being called when you make a HTTP request:

There is no need to a Java class that is called from the batch file. Just use wget to retrieve the page:

wget http://127.0.0.1/myServlet -O mypage.jsp

wget is open source and available for (nearly) all operating systems

2

I think your solution is fine. The only thing I would change is not actually do the reading of the resource your self. I would suggest using Google's Resources.toString lib like so

String data = Resources.toString(url, ...);
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
1

im not sure what you mean by asking

I would like to adjust these JSPs so that they can be additionally called in batch

do they need authentication ?

anyways, depending on your requirements you could even try it with spring batch

http://static.springsource.org/spring-batch/

Michael Pralow
  • 6,560
  • 2
  • 30
  • 46
1

you could also look at Apache HTTP Client library... again this might be too much of a library to call a simple Servlet..

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
0

If possible you should refactor your servlets and move the logic out to a service class that doesn't rely on request/response so that you can call this from wherever you like.

I did something similar two weeks ago - a report that was generated from the web app needed to be ran as a scheduled job. I moved all the code from a Spring controller (what was it doing there anyway?) into a service class and called that code from the controller and a scheduled Quartz job.

blank
  • 17,852
  • 20
  • 105
  • 159