java试卷提交,ajax自动提交考试试卷的java web demo

package com.service;

import java.io.IOException;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

/**

*

* @author administrator

*/

public class StartExam extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = 1L;

protected int examTime=10; //考试时间

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");

String action = request.getParameter("action");

if("startExam".equals(action)){

this.startExam(request,response); //开始考试

}else if("showStartTime".equals(action)){//显示考试时间

this.showStartTime(request,response);

}else if("showRemainTime".equals(action)){//显示剩余时间

this.showRemainTime(request,response);

}

}

public void startExam(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException{

HttpSession session = request.getSession();

request.setAttribute("time", examTime); //保存考试时间

session.setAttribute("startTime",new Date().getTime());//保存当前时间的毫秒数

request.getRequestDispatcher("startExam.jsp").forward(request, response);

}

// 显示考试计时

public void showStartTime(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");

HttpSession session = request.getSession();

String startTime=session.getAttribute("startTime").toString();

long a=Long.parseLong(startTime);//将开始时间转换为毫秒数

long b=new java.util.Date().getTime();//获取当前时间的毫秒数

int h=(int)Math.abs((b-a)/3600000);//获取小时

int m=(int)(b-a)%3600000/60000;//获取分钟

int s=(int)((b-a)%3600000)%60000/1000;//获取秒数

String hour="",minute="",second="";

if(h<10){

hour= "0"+h;

}else{

hour=""+h;

}

if(m<10){

minute= "0"+m;

}else{

minute= ""+m;

}

if(s<10){

second = "0"+s;

}else{

second = ""+s;

}

String time=hour+":"+minute+":"+second;//组合已用时间

response.getWriter().print(time);//加上这句话时间久显示了(2014-07-18)

request.setAttribute("showStartTime",time);//将生成的时间保存到showStartTime参数中

//request.getRequestDispatcher("showStartTime.jsp").forward(request, response);//重定向页面

}

public void showRemainTime(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");

HttpSession session = request.getSession();

String startTime=session.getAttribute("startTime").toString();

long a=Long.parseLong(startTime);//获取开始时间的毫秒数

long b=new java.util.Date().getTime();//获取当前时间的毫秒数

long r=examTime*60000-(b-a-1000);//计算考试剩余时间的毫秒数

int h=(int)Math.abs(r/3600000);//计算小时

int m=(int)(r)%3600000/60000;//计算分钟

int s=(int)((r)%3600000)%60000/1000;//计算秒数

String hour="",minute="",second="";

if(h<10){

hour= "0"+h;

}else{

hour=""+h;

}

if(m<10){

minute= "0"+m;

}else{

minute= ""+m;

}

if(s<10){

second = "0"+s;

}else{

second = ""+s;

}

String time=hour+":"+minute+":"+second;//组合剩余时间

response.getWriter().print(time);//加上这句话时间久显示了(2014-07-18)

request.setAttribute("showRemainTime",time);//将生成的时间保存到showRemainTime参数中

//request.getRequestDispatcher("showRemainTime.jsp").forward(request, response);//重定向页面

}

public void init() throws ServletException {

examTime=Integer.parseInt(getInitParameter("examTime")); //获取配置文件中设置的考试时间

}

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException,

IOException {

processRequest(request, response);

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException,

IOException {

processRequest(request, response);

}

@Override

public String getServletInfo() {

return "Short description";

}//

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值