testaddmanservlet.java

来自「培训考试系统代码」· Java 代码 · 共 190 行

JAVA
190
字号
package com.huawei.icd30.agt.testman;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.huawei.icd30.agt.util.*;
import com.huawei.icd30.common.db.*;

import com.huawei.icd30.agt.code.StringCode;

/**
 * <p> 接收增加试题页面提交的请求,增加成功定向到同类型试题录入页面,
 *     以便录入下一道题目,操作失败统一定向到出错页面。</p>
 * <p> </p>
 * <p> </p>
 * @author 龙燕茜
 * @version 1.0
 */

public class TestAddManServlet extends HttpServlet
{
    private static final String CONTENT_TYPE = "text/html; charset=gb2312";
    //Initialize global variables
    public void init() throws ServletException
    {
    }
    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("gb2312");
        response.setContentType(CONTENT_TYPE);

        //定义连接的实例
        SysDbConn aplcoms = null;

        try
        {
            MultiRequest mr = new MultiRequest(request) ;

            //获取参数值
            String testType = mr.getParameter("testType");
            String content = OperateUtility.strDelEnterChar(mr.getParameter("content"));
            String optType = mr.getParameter("opeType");
            String optionRs = mr.getParameter("optionRs");
            String option = mr.getParameter("option");
            String explain = mr.getParameter("explain");
            String degree = mr.getParameter("degree");
            String classId = mr.getParameter("classId");
            String testMeans = mr.getParameter("testMeans");
            String useLevel = mr.getParameter("useLevel");
            String referenceTime = mr.getParameter("referenceTime");

            //接收图片文件
            File toFile = null ;
            BufferedOutputStream os = null;
            File imgPath = new File(this.getServletContext().getRealPath("agt"));
            String subImgPath = "testman" + File.separatorChar + "images" + File.separatorChar;
            HashMap imgNameMap = new HashMap();
            LinkedList imgNameList = new LinkedList();

            String imgFile = mr.getFullFilename();
            long increase = System.currentTimeMillis();
            while(imgFile!=null && !imgFile.trim().equals(""))
            {
                String suffix = imgFile.substring(imgFile.lastIndexOf(".") ,imgFile.length());
                String newName = String.valueOf(increase++) + suffix;
                toFile = new File(imgPath , subImgPath + newName);

                imgNameList.add(imgPath+subImgPath+newName);

                //保存图片的名字映射
                String imgSaveName = File.separatorChar + "agt" + File.separatorChar
                                        + subImgPath + newName ;
                imgNameMap.put(imgFile.replace('\\' ,'/') ,imgSaveName.replace('\\' ,'/')) ;

                toFile.getParentFile().mkdirs();
                os = new BufferedOutputStream(
                        new FileOutputStream(toFile));
                mr.upload(os);
                os.close();
                imgFile = mr.getFullFilename() ;  // 下一个文件
            }

            String newContent = null;
            if(!(content==null || content.trim().equals("")))
            {
                // 替换试题内容中的图像信息
                newContent = OperateUtility.convertImgName(content ,imgNameMap);
            }

            //保存试题到数据库中

            //得到一个连接的实例
            aplcoms = SysConnPool.getInstance().getAplComs();

            //调用存储过程P_Agt_TestItemAdd完成试题的添加
            aplcoms.preparedSP();
            aplcoms.setString(1,newContent);
            aplcoms.setString(2,explain);
            aplcoms.setString(3,classId);
            aplcoms.setString(4,testType);
            aplcoms.setString(5,degree);
            aplcoms.setString(6,option);
            aplcoms.setString(7,optionRs);
            aplcoms.setString(8,testMeans);
            aplcoms.setString(9,useLevel);
            aplcoms.setString(10,referenceTime);

            SysDataSet ds = aplcoms.csCommonSP("P_Agt_TestItemAdd");
            SysRecord rc = ds.getParamSet();
            if(rc!=null&&rc.getInt(0)==0)
            {//试题添加成功,定向到下一个试题页面
               SysResultSet typeRs = TestGetInfo.getTestTypeRs();
               if(typeRs != null)
               {
                   for(int i=0;typeRs.setRecord(i)&&i<typeRs.getMetaData().getRecordCount();i++)
                   {
                      if(testType.equalsIgnoreCase(typeRs.getString(0)))
                      {
                         response.sendRedirect(typeRs.getString(2)+"?testType="+typeRs.getString(0)+"&success=1");
                         return;
                      }
                   }
               }
               else
               {
                   response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.UNKNOW_ERROR);
                   return;
               }
            }
            else
            {//试题添加失败
               //删除所保存的图片
               if(imgNameList != null)
               {
                  for(int i=0;i<imgNameList.size();i++)
                  {
                      new File(((String)imgNameList.get(i))).delete();
                  }
               }
               //定向到错误页面,错误号为0602,插入试题失败
               response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.TEST_ADDERROR);
               return;
            }
        }
       catch (SysDbException aple)
       {//捕获CommonService系统异常,定向到出错页面
           aple.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.COMMONSERVICE_ERROR);
           return;
       }
       catch(java.sql.SQLException sqle)
       {//捕获调用aplcoms异常,定向到出错页面
           sqle.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.DATABASE_ERROR);
           return;
       }
       catch(IOException ioe)
       {
           ioe.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.FILE_EORROR);
           return;
       }
       catch(SecurityException se)
       {
           se.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.FILE_SECURITY_EORROR);
           return;
       }
       catch(Exception e)
       {//捕获未知异常,定向到出错页面
           e.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.UNKNOW_ERROR);
           return;
       }
       finally
       {//关闭连接实例
           if(aplcoms != null)
           {
              aplcoms.close();
           }
       }

    }
    //Clean up resources
    public void destroy()
    {
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?