Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

lil stuck....##??!!

Hey,

ive got a little problem with the app i am developing and am unure of how to go about it.

Scenerio
A user enters info in html form, jsp verfiys info submitted if user is valid and hasant taken test before they are directed to the test page.

(all the above has been implemented)

i want to now time how long it takes the user to complete the test (after they have pressed submit on test page this means they have completed it) and then enter this value in the database as well as checking to see if their answers are correct (i no how to write to the database- but how do i get the time???)

also if the user does not finish within a specfic time ie 10mins then the exam shud be submiited and the databse values updated. and user redirected to another page which tells them of their score/


does ne one no how ii can do this?
how to improve this?
or shud i just try some other way?

thanks in advance ill
0
illinosis
Asked:
illinosis
  • 4
  • 2
  • 2
  • +1
1 Solution
 
cheekycjCommented:
two things you can implement (not as replacements but complementing each other)

1. JavaScript timer to let the user know how much time is left for the test (use JSP to popuplate the initial time)
like here:
http://javascript.internet.com/games/count-down.html

2. on the first page store the current time in session using

<% java.util.Calendar startTime = java.util.Calendar.getInstance();
   session.setAttribute("starttime", startTime);
%>
the above is the variable to use to populate the start time in Javascript.

Then you can tell the time difference on the submission page like this:

<%
   boolean inTime = false;
   java.util.Calendar startTime = (java.util.Calendar)session.getAttribute("starttime");
   if (startTime != null) {
     endTime = java.util.Calendar.getInstance();
     // setting end time to start time (initially)
     endTime.setTime(startTime.getTime());
     // adding ten minutes
     endTime.add(java.util.Calendar.MINUTE, 10);
     // if end time is before the current time set inTime to true
     if (endTime.before(java.util.Calendar.getInstance()) {
       inTime = true;
     }
%>
  <% if (inTime) { %>
    finished in time!
  <% } else { %>
   did not finish in time!
  <% } %>
<% } else { %>
  No start time error!
<% } %>

HTH,
CJ
0
 
kennethxuCommented:
for the jsp part, it might be easier do it this way.

when you display the test page:

<% session.setAttribute( "startTime", new Long(System.currentTimeMillis()/60000) ); %>

when user submit result back:

<% long duration = System.currentTimeMillis()/60000 - session.getAttribute( "startTime" ).longValue(); %>

the var duration contains the number of minutes users used to finish the test.
0
 
objectsCommented:
No need to use a Calendar object, simply store a new Date instance as the start time.

Use a meta refresh tag on each page to autorefresh the page after the remaining time has passed.

And when submitted calc time difference as posted above.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cheekycjCommented:
agreed that Calendar is not needed, I am too used to writing code that is adaptable to internationalization and java.util.Date is pretty much deprecated - I hate the deprecated warning in my builds :-)

CJ
0
 
illinosisAuthor Commented:
thanx for all ure advice...i will play around with the coding tonite and tmor and let u all no if there are ne probs etc...

hopefully it works out...i will post points on thurs after i have tried out ure suggestions.

thanx for everyones input.
0
 
objectsCommented:
> java.util.Date is pretty much deprecated

Date isn't deprecated, just some of it's methods. The Date class itself (and non-deprecated methods) are really independant of i18n.
0
 
cheekycjCommented:
4 out of its 6 constructors are deprecated.

Out of its 30 odd methods about 20 are deprecated.

And from Sun's online javadocs:
"Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated."

So we pretty much make it a Standard to use java.util.Calendar.

CJ
0
 
illinosisAuthor Commented:
well..thanku all for ure help...at the end i decided to use CJ way...ive adapted it to meet my needs...thanx CJ and have also decided to use the timer.

for some reason i kept having errors when trying to do it kennethxu way :(...(cant convert java lang to long...)...

thanku all nehow.

illinosis.
0
 
cheekycjCommented:
Glad I could help and Thanx for the "A"

CJ
0
 
kennethxuCommented:
>> for some reason i kept having errors when trying to do it kennethxu way :(...(cant convert java lang to long...)...
forget to cast the session attribute back to Long, this works for me:

<% if( request.getParameter( "submit" ) == null ) {
     session.setAttribute( "startTime", new Long(System.currentTimeMillis()) ); %>
Submit after finish the test:
<form method=post><input type=submit name=submit></form>
<% } else {
     long duration = System.currentTimeMillis() - ((Long)session.getAttribute( "startTime" )).longValue(); %>
You spent <%=duration/60000%> minutes<p>
You spent <%=duration/1000%> seconds<p>
You spent <%=duration/60000.0%> minutes<p>
<% } %>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now