程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 用jsp將xml文件解析到網頁顯示,並把數據提交保存到數據庫

用jsp將xml文件解析到網頁顯示,並把數據提交保存到數據庫

編輯:關於JSP

<?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.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.*"%>
<%
 
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>
 
 
 
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved