用applet實現線上考試系統

來源:互聯網
上載者:User

這個學期學校的實驗是做一個教學網站,在做這個東西時,我們嘗試了使用java applet來實現一個線上考試系統.原因在於雖然使用者下載APPLET時速度比較慢,但一旦完成時,在做題期間就有了很大的自由度,在用戶端的表現立也更強. 伺服器端我們採用的是java servlet,每次applet向servlet請求時,servlet從資料庫中按類別隨機選出若干題目和答案封裝成對象再發向APPLET,applet接收並顯示給使用者.

applet端請求接收題目代碼:
 private ArrayList Setquestions(){
  ArrayList temp=new ArrayList();
  try{
   //擷取網址
      URL url = new URL(getCodeBase(), "/DBeducation/TestServlet");
      URLConnection con = url.openConnection();
      con.setUseCaches(false);
      InputStream in = con.getInputStream();
      ObjectInputStream objStream;
      objStream = new ObjectInputStream(in);
      temp=(ArrayList)objStream.readObject();
      objlength=temp.size();

      System.out.println("ok");
  }catch(Exception e){
   
  }return  temp;
 }

servlet端選擇題目發送代碼:
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
 Connection con; 
 PreparedStatement stm;
 ResultSet rs;
 //PrintWriter out;
 String sql;
 int id[]=new int[200];//用於存放類型ID號的數組
 int temp=0;
 ArrayList all=new ArrayList();
    response.setContentType("text/html;charset=gb2312");
  //  out = response.getWriter();
 try{
  con=new ContactSql().getcon();
  sql="select id from testlibkind";
  stm=con.prepareStatement(sql);
  rs=stm.executeQuery();//獲得測試題所有的類型ID號
  while(rs.next()){
   id[temp]=rs.getInt(1);//獲得ID號
   temp++;
  }
  
  for(int i=0;i<id.length;i++){
   sql="select question,answer,a,b,c,d from testlib where kid=? order by rand() limit 3";//隨機獲得每個類型習題的題目和答案3道
   stm=con.prepareStatement(sql);
   stm.setString(1,""+id[i]);
   rs=stm.executeQuery();
   while(rs.next()){
    ArrayList v=new ArrayList();
    v.add(rs.getString("question"));//獲得問題
    v.add(rs.getString("answer"));//獲得答案
    v.add(rs.getString("a"));
    v.add(rs.getString("b"));
    v.add(rs.getString("c"));
    v.add(rs.getString("d"));
    all.add(v);
   }
   
  }
  
     OutputStream out1;//發送arraylist對象到applet
     ObjectOutputStream objStream;
     out1 = response.getOutputStream();
     objStream = new ObjectOutputStream(out1);
     objStream.writeObject(all);
     rs.close();
     stm.close();
     con.close();
    
     return;

  
 }catch(Exception e){
  //out.println(e.getMessage());
  System.out.println(e);
 }
 
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.