首页>代码>ajax自动提交考试试卷的java web demo>/ajax自动提交试卷的demo/Test/src/com/service/StartExam.java
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";
    }// </editor-fold>
    }
最近下载更多
223497  LV1 2021年12月14日
夜空中最亮的星  LV8 2021年6月24日
1973356987  LV13 2021年6月18日
czh0510  LV12 2021年4月23日
李振林  LV12 2021年3月24日
qianjin1129  LV15 2021年2月23日
TerryGaoBo  LV9 2020年6月11日
kinglong  LV16 2020年5月3日
anyone  LV2 2020年4月18日
安阳工学院  LV8 2019年10月21日
最近浏览更多
taoshen95  LV14 2月29日
auqfnifc  LV3 2月7日
gjw19884260466  LV3 1月8日
fesfefe  LV13 2023年9月11日
3334004690  LV3 2023年8月19日
qazws123  LV1 2023年6月7日
yangliyang 2023年1月30日
暂无贡献等级
qhdjod  LV7 2022年9月26日
lc649937689  LV2 2022年8月30日
anbangchen89  LV2 2022年7月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友