Java Script to Tell Time and Limit by it?

M

Mike A

Hi,

I'm hoping someone can help me with this.

I have a URL for which I'd like to limit access to by time. For
example,
say I have a URL that I don't want accessable on Monday mornings
between 10am-noon and Fri. afternoons between 2-4pm. So when click
upon a message comes up?

Is this possible with Java? if yes, would someone be able to provide
it.

TIA,
Mike A
 
A

Anton Spaans

Hi Mike

This is possible, but I'm wondering whether you meant how to do it in
JavaScript (subject of your msg) or how to do it in Java (body of your
message).

If it is in Java (on the web-server), just make get a timestamp (new Date())
and check whether the date is OK. If not, return an error HTTP -status
saying that the resource is (temporarily) unavailable.

If it is in JavaScript (on the browser), it is possible too. However,
computer-savvy users may easily find a way around it (since JavaScript is
running on their browser and JavaScript can always be read and examined) and
defeat the purpose of your 'timed URL'.

On the link, add an onclick event handler:

<a href="#"
onclick="javascript:goToTimedURL('http://www.server.com/yoururl.file');">my
link</a>

Then add a JAvaScript function goToTimedURL(pURL):

// As an example.
function goToTimedURL(pURL)
{
var now = new Date(); // note; This is the date on the browser's
computer... can be fooled....

....
if 'now' is in the allowed dates/times: location.href = pURL
otherwise: alert("Not allowed");
}

You can make this as complex as possible to prevent users as much as
possible to manually open the pURL in their browser (cut and paste from the
HTML/JavaScript source into the address-bar of the browser), but you can not
avoid it....

-- Anton.
 
M

Mike A

Anton,
Thank you for your response, sorry for mixing up terminology. I want
to build
this date limiting code into my web page sitting on my server. So is
the code you posted below what I need? if yes, I'm confused on how to
limit by monday mornings and fri afternoons.

Thanks for any assistance.
Mike
 
S

Sudsy

Mike said:
Anton,
Thank you for your response, sorry for mixing up terminology. I want
to build
this date limiting code into my web page sitting on my server. So is
the code you posted below what I need? if yes, I'm confused on how to
limit by monday mornings and fri afternoons.

Thanks for any assistance.
Mike

Again, it depends on what you're using on the server. Here's a quick-
and-dirty framework which is based on HttpServlet:

import java.io.IOException;
import java.util.GregorianCalendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TimeSensitiveServlet extends HttpServlet {

public TimeSensitiveServlet() {
super();
}

public void init() throws ServletException {
super.init();
// add any initilization code here
}

public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException {
if( ! timeOK() )
resp.sendError( HttpServletResponse.SC_MOVED_TEMPORARILY );
// otherwise process normally
}

private boolean timeOK() {
GregorianCalendar now = new GregorianCalendar();
int dayOfWeek = 0;
int hourOfDay = 0;

dayOfWeek = now.get( GregorianCalendar.DAY_OF_WEEK );
if( dayOfWeek == GregorianCalendar.MONDAY ) {
hourOfDay = now.get( GregorianCalendar.HOUR_OF_DAY );
if( ( hourOfDay >= 10 ) && ( hourOfDay < 12 ) )
return( false );
}
else if( dayOfWeek == GregorianCalendar.FRIDAY ) {
hourOfDay = now.get( GregorianCalendar.HOUR_OF_DAY );
if( ( hourOfDay >= 14 ) && ( hourOfDay < 16 ) )
return( false );
}
return( true );
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top