欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用jsp将xml文件解析到网页显示,并把数据提交保存到数据库

程序员文章站 2023-08-26 19:32:27
  <?xml version="1.0" encoding="utf-8"?> 
<questions id="1">   
   <question id="1" type="1"> 
       <title><![cdata[你是哪个国家的人?]]></title>
       <answer>
        <it><![cdata[1、中 国]]></it>  
        <it><![cdata[2、日 本]]></it>  
        <it><![cdata[3、韩 国]]></it>
        <it><![cdata[4、其 他]]></it>       
       </answer>         
   </question>   
   <question id="3" type="2">
      <title><![cdata[你是什么样的人?]]></title>
       <answer>
        <it><![cdata[1、文 静]]></it>  
        <it><![cdata[2、干 净]]></it>  
        <it><![cdata[3、豪 爽]]></it> 
        <it><![cdata[4、不 丑]]></it>            
       </answer>         
   </question>
   <question id="4" type="2">
      <title><![cdata[你是什么样的人?]]></title>
       <answer>
        <it><![cdata[1、文 静]]></it>  
        <it><![cdata[2、干 净]]></it>  
        <it><![cdata[3、豪 爽]]></it> 
        <it><![cdata[4、不 丑]]></it>             
       </answer>         
   </question>
   <question id="5" type="3"> 
       <title><![cdata[你怎样评价自己?]]></title>
       <answer>
        <it><![cdata[例如,我是一个很牛逼的人!]]></it>               
       </answer>
   </question> 
   <question id="6" type="3"> 
       <title><![cdata[你怎样评价自己?]]></title>
       <answer>
        <it><![cdata[例如,我是一个很牛逼的人!]]></it>               
       </answer>
   </question>            
</questions>
questions.
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ page contenttype="text/html;charset=utf-8"%>
<%@ page import="javax.xml.parsers.*,org.w3c.dom.*"%>
<%
 
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
  <head>
    <base href="<%=basepath%>">  
    <meta http-equiv="content-type" content="text/html; charset=utf-8">    
    <title>question</title>
  </head> 
  <body bgcolor="#79cdcd">
   <%  
    documentbuilderfactory questiondbf=documentbuilderfactory.newinstance();   
 documentbuilder questiondb = questiondbf.newdocumentbuilder();
    document document=questiondb.parse(pagecontext.getservletcontext().getresourceasstream ("question.xml"));
    element element = document.getdocumentelement();       
    nodelist questions = element.getelementsbytagname("question");
    %>
    <form name="questionform" id="questionform" action="questionsave.jsp" method="post">     
    <%
    out.println("<input type="+"\"hidden\""+" name="+"\"question_id\""+" value="+"\""+element.getattribute("id")+"\""+"/>");
    for (int i = 0; i < questions.getlength(); i++) {
   element questionelement = (element) questions.item(i);     
   if(integer.parseint(questionelement.getattribute("type"))==1){
    nodelist childnodes = questionelement.getchildnodes();    
    for (int j = 0; j< childnodes.getlength(); j++) {
     //element answerelement = (element)answers.item(j);
     if (childnodes.item(j).getnodetype() == node.element_node) {
      if ("title".equals(childnodes.item(j).getnodename())) {
       out.println("<h3>"+questionelement.getattribute("id")+"、"+childnodes.item(j).getfirstchild().getnodevalue()+"</h3>");
       //out.println("<input type="+"\"hidden\""+" name="+"\"question_single_"+questionelement.getattribute("id")+"\""+" value="+"\""+questionelement.getattribute("id")+"\""+"/>");  
      }else if ("answer".equals(childnodes.item(j).getnodename())) {
          nodelist itemnodes=childnodes.item(j).getchildnodes();
       for(int k=0;k<itemnodes.getlength();k++){
        //element itemelement=(element)itemnodes.item(k);
        if(itemnodes.item(k).getnodetype()==node.element_node){
         if("it".equals(itemnodes.item(k).getnodename())){
          out.println("<input name="+"\"sing_"+questionelement.getattribute("id")+"_"+questionelement.getattribute("type")+"\" "+" type="+"\"radio\""+" value="+"\""
          +itemnodes.item(k).getfirstchild().getnodevalue()+"\""+">"+itemnodes.item(k).getfirstchild().getnodevalue()+"</input>");         
         }
        }
       }     
      }
     }
     }
   }
   if(integer.parseint(questionelement.getattribute("type"))==2){
    nodelist childnodes = questionelement.getchildnodes();    
    for (int j = 0; j< childnodes.getlength(); j++) {    
     if (childnodes.item(j).getnodetype() == node.element_node) {
      if ("title".equals(childnodes.item(j).getnodename())) {
       out.println("<h3>"+questionelement.getattribute("id")+"、"+childnodes.item(j).getfirstchild().getnodevalue()+"</h3>");
       //out.println("<input type="+"\"hidden\""+" name="+"\"question_chebox_"+questionelement.getattribute("id")+"\""+" value="+"\""+questionelement.getattribute("id")+"\""+"/>");  
      }else if ("answer".equals(childnodes.item(j).getnodename())) {
          nodelist itemnodes=childnodes.item(j).getchildnodes();
       for(int k=0;k<itemnodes.getlength();k++){
        //element itemelement=(element)itemnodes.item(k);
        if(itemnodes.item(k).getnodetype()==node.element_node){
         if("it".equals(itemnodes.item(k).getnodename())){
          out.println("<input name="+"\"che_"+questionelement.getattribute("id")+"_"+questionelement.getattribute("type")+"\" "+" type="+"\"checkbox\""+" value="+"\""
          +itemnodes.item(k).getfirstchild().getnodevalue()+"\""+">"+itemnodes.item(k).getfirstchild().getnodevalue()+"</input>");
         }
        }
       }     
      }
     }
     }
   }
   if(integer.parseint(questionelement.getattribute("type"))==3){
    nodelist childnodes = questionelement.getchildnodes();       
    for (int j = 0; j< childnodes.getlength(); j++) {    
     if (childnodes.item(j).getnodetype() == node.element_node) {
      if ("title".equals(childnodes.item(j).getnodename())) {
       out.println("<h3>"+questionelement.getattribute("id")+"、"+childnodes.item(j).getfirstchild().getnodevalue()+"</h3>");
       //out.println("<input type="+"\"hidden\""+" name="+"\"question_answer_"+questionelement.getattribute("id")+"\""+" value="+"\""+questionelement.getattribute("id")+"\""+"/>");
      }else if ("answer".equals(childnodes.item(j).getnodename())) {
          nodelist itemnodes=childnodes.item(j).getchildnodes();
       for(int k=0;k<itemnodes.getlength();k++){
        //element itemelement=(element)itemnodes.item(k);
        if(itemnodes.item(k).getnodetype()==node.element_node){
         if("it".equals(itemnodes.item(k).getnodename())){
          out.println("<textarea name="+"\"te_"+questionelement.getattribute("id")+"_"+questionelement.getattribute("type")+"\" "+" rows="+"\"4\""+" cols="+"\"100\""+" value="+"\""
          +itemnodes.item(k).getfirstchild().getnodevalue()+"\""+">"+itemnodes.item(k).getfirstchild().getnodevalue()+"</textarea>"+"<br>");
         }
        }
       }     
      }
     }
     }
   }  
    }
   %>
   <br/>   
     用户名:<input type="text" name="username" id="username" value="" size="20" />&nbsp;&nbsp;                               
          <input type="submit" value="提交" name="submit" />&nbsp;&nbsp;
          <input type="reset" value="重置" name="reset" />              
      </form>
  </body>
</html>
questionsave.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ page contenttype="text/html;charset=utf-8"%>
<%@ page import="javax.xml.parsers.*,org.w3c.dom.*"%>
<%@ page import="com.mysql.jdbc.driver"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.text.*"%>
<%@ page language="java" import="java.util.*"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>question_saves</title>
 </head>
 <body background="#123456">
  <%        
     //驱动程序名
  string drivername="com.mysql.jdbc.driver";
  //用户名
  string username="root";
  //密码
  string userpasswd="520123412";
  //数据库名
  string dbname="xml";  
  //连接字符串
  string url="jdbc:mysql://localhost:3306/"+dbname+"?user="+username+"&password="+userpasswd+"&useunicode=true&characterencoding=utf-8";
 //加载驱动程序
  class.forname("com.mysql.jdbc.driver").newinstance();
  connection conn=null;
  statement stmt=null;
  resultset rs=null;
  request.setcharacterencoding("utf-8");
  string question_id=request.getparameter("question_id");
  string username=request.getparameter("username");
  if(username!=null&&username!=""){  
   map map=request.getparametermap();  
   iterator iter = map.entryset().iterator();
   while (iter.hasnext()) { 
       map.entry entry = (map.entry) iter.next();         
         // object val = entry.getvalue();
         if(((string)entry.getkey()).matches("sing_[0-99]_[0-99]")){        
          try{
           string  sing= request.getparameter((string)entry.getkey());
           string[] str=((string)entry.getkey()).split("_");
           //out.println("<p>"+str[0]+":"+str[1]+":"+str[2]+"</p>");
           //out.println("<p>"+entry.getkey()+":"+request.getparameter((string)entry.getkey())+"</p>");
           conn=drivermanager.getconnection(url);
      //创建执行语句 
     string sql="insert into question(question_id,username,q_id,q_type,qanswer)"
      +" values('"+question_id
      +"','"+username
      +"','"+str[1]
      +"','"+str[2]
      +"','"+sing
      +"')";    
     stmt=conn.createstatement();
     stmt.execute(sql);    
     stmt.close(); //关闭连接、释放资源
     conn.close();
     out.println("<p>保存问卷"+question_id+": 单选题"+str[1]+"成功!"+"</p>");
     }catch(sqlexception e){
       e.printstacktrace();
     }
         }else if(((string)entry.getkey()).matches("che_[0-99]_[0-99]")){
          try{
           string[]  ches= request.getparametervalues((string)entry.getkey());
           string cheanswer="";
           for(int i=0;i<ches.length;i++){
            cheanswer+=ches[i]+" ";
           }
           string[] str=((string)entry.getkey()).split("_");
           conn=drivermanager.getconnection(url);
      //创建执行语句 
     string sql="insert into question(question_id,username,q_id,q_type,qanswer)"
      +" values('"+question_id
      +"','"+username
      +"','"+str[1]
      +"','"+str[2]
      +"','"+cheanswer
      +"')";    
     stmt=conn.createstatement();
     stmt.execute(sql);    
     stmt.close(); //关闭连接、释放资源
     conn.close();
     out.println("<p>保存问卷"+question_id+": 多选题"+str[1]+"成功!"+"</p>");
     }catch(sqlexception e){
       e.printstacktrace();
     }
         }else if(((string)entry.getkey()).matches("te_[0-99]_[0-99]")){
          try{
           string  te= request.getparameter((string)entry.getkey());
           string[] str=((string)entry.getkey()).split("_");          
           conn=drivermanager.getconnection(url);
      //创建执行语句 
     string sql="insert into question(question_id,username,q_id,q_type,qanswer)"
      +" values('"+question_id
      +"','"+username
      +"','"+str[1]
      +"','"+str[2]
      +"','"+te
      +"')";    
     stmt=conn.createstatement();
     stmt.execute(sql);    
     stmt.close(); //关闭连接、释放资源
     conn.close();
     out.println("<p>保存问卷"+question_id+": 问答题"+str[1]+"成功!"+"</p>");
     }catch(sqlexception e){
       e.printstacktrace();
     }
         }         
     }
    
     }else{
      out.println("<p>保存失败,没有输入用户名</p>");
  }  
    %> 
 </body>
</html>