Solved

Running a JSP script every 5 mins?

Posted on 2003-12-09
17
534 Views
Last Modified: 2011-10-03
Hello,

I am new to JSP. I would like to know how I can make a JSP script run automatically every 5 mins. This will be used to pull data from a webpage and store it in a database. If the server restarts, I need this script to continue to run.

Thank you,

- 007
0
Comment
Question by:jsp007
  • 6
  • 4
  • 3
  • +2
17 Comments
 

Author Comment

by:jsp007
ID: 9906968
I am testing the code on a tomcat server on Win XP, but the final production server will most likely be IBM websphere running on an undecided OS.

Thanks in advance

-007
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9908308
When the servlet container starts, you can have a servlet automatically load.  From this servlet's init() method, you could start a separate TimerTask that runs every 5 minutes to do your update.

To automatically load the servlet (and therefore execute the init() method) when the server starts, you need to include the <load-on-startup> tag (inside the <servlet> tag) in the web.xml file.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9910531
That's the only way I can think of doing it...

Of course instead of running a JSP, you would be better moving your code into the TimerTask

Tim
0
 
LVL 6

Expert Comment

by:jarasa
ID: 9910591
Hi 007.
If your goal is to pickup data from the Web and put it on your database, with out caring of your webserver access, I really don't understand why you want to do it on a web based application. You should just make it on any language as an stand alone application and configure it as you like.
Javier
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9910674
It's probably part of his web-application, so it can cache regular web-pages, or feeds...
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9911032
>> Of course instead of running a JSP, you would be better moving your code into the TimerTask

;-)

You cannot run a JSP from the new thread (unless the new Thread is simulating a browser).  So Tim is right here :-)
0
 

Author Comment

by:jsp007
ID: 9911336
hello everyone,

Thank you for taking time to answer my Q. The most sensible thing would be to write a standalone program, but that is not an option in this case. As of now the following answer seems to be the most reasonable one...
Comment from jimmack
Date: 12/09/2003 02:59PM PST

Is it possible for you to give me a step by step method of doing this i.e. loading the servlet into the init() method? Also, will I need any admin access to do this once it goes up on the sever?

Thanks

- 007
0
 
LVL 15

Accepted Solution

by:
jimmack earned 125 total points
ID: 9911359
The init() method is already inside the servlet.  It is one of the methods that you can override from GenericServlet.  So, you could do:

public TaskServlet extends GenericServlet
{
    public void init() throws ServletException
    {
        // Create and start the timer task here.
    }
}

For help with Timer and TimerTask, look at: http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:jimmack
ID: 9911398
No, you shouldn't need any extra rights on the server.  If your servlet can execute, it should be able to start another thread.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9911473
Don't forget:

> you need to include the <load-on-startup> tag (inside the <servlet> tag) in the web.xml

as jim said earlier as well :-)
0
 

Expert Comment

by:kdkrueger
ID: 9921450
I tested the following code on Tomcat 4.1.24.  The page continued to refresh every 5 minutes even after restarting the server.  The URL parameter should be the page you're calling it from (this page).

<HTML>
<HEAD>
<meta http-equiv="refresh" content="300; URL=http://your_url/test_refresh.jsp">
<title>test_refresh.jsp</title>
</HEAD>
<font size=+1>
This page should reload every five minutes.
<br>
<br><br>
<%@ page import="java.util.Calendar" %>
<%
      Calendar calendar = Calendar.getInstance();
%>
Loaded at <%=calendar.getTime()%>
</html>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9921606
> The page continued to refresh every 5 minutes even after restarting the server.

As long as you open it in a browser...  it will stop running when you close your browser...
0
 

Expert Comment

by:kdkrueger
ID: 9921625
> This will be used to pull data from a webpage and store it in a database.

From the original post it sounds like a webpage will already be open.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9922978
>> and store it in a database

?
0
 

Expert Comment

by:kdkrueger
ID: 9923296
Storing the information in the database would be done with code on the jsp page that refreshes every 5 minutes.  This could be done with code like the following enhancement of my earlier post.  Put code to handle the submit before code to display the page.:

<HTML>
<HEAD>
<meta http-equiv="refresh" content="300; URL=http://yoururl/test_refresh.jsp">
<title>test_refresh.jsp</title>
</HEAD>
<body onunload="document.the_form.submit()">
<font size=+1>
This page should reload every five minutes.
<br>
Old value = <%=request.getParameter("the_time")%>
<br><br>
<%@ page import="java.util.Calendar" %>
<%
      Calendar calendar = Calendar.getInstance();
%>
<form name="the_form" action="http://your_url/test_refresh.jsp" method="post">
Loaded at <input name="the_time" value="<%=calendar.getTime()%>">.
</form>
</body>
</html>
0
 

Author Comment

by:jsp007
ID: 9927497
Hello everyone,

I just wanted to thank you all for answering my Qs. Although most of your answerswere right, it was not what I was looking for... The meta tag refresh (which I thought of previously) would not work because I cannot have a browser open day and night refreshing the page.

I have chosen the following answer because it is exactly what I wanted
Comment from jimmack
Date: 12/10/2003 03:22AM PST

I will follow this URL and and the rest is up to me to figure it out

Thanks a lot guys :-)

- Sid
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9927520
;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How To Remove a Principal from HttpServletRequest 4 40
trouble with PDF encryption using itext-1.3.1 17 81
maven project in eclipse 11 57
maven project jars 7 50
In the hope of saving someone else's sanity... About a year ago we bought a Cisco 1921 router with two ADSL/VDSL EHWIC cards to load balance local network traffic over the two broadband lines we have, but we couldn't get the routing to work consi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now