Java 如何从JSP返回XML数据?

Java 如何从JSP返回XML数据?,java,xml,jsp,xml-parsing,Java,Xml,Jsp,Xml Parsing,我必须从jsp页面返回xml数据 我从数据库中获取一条记录并显示在Jsp中。我必须返回xml格式的显示记录。我必须用jsp显示,同时用xml返回一条记录 我已经完成了一个显示过程,并以xml格式返回: <% String selectsql = null; DBConnection DBCon = null; Connection Con = null; String maprule = null; PreparedSta

我必须从jsp页面返回xml数据

我从数据库中获取一条记录并显示在Jsp中。我必须返回xml格式的显示记录。我必须用jsp显示,同时用xml返回一条记录

我已经完成了一个显示过程,并以xml格式返回:

<%
        String selectsql = null;
    DBConnection DBCon = null;
    Connection Con = null;      
    String maprule = null;
        PreparedStatement pstm = null;
        ResultSet resultSet = null;
        String snomedid = request.getParameter("snomedcode");
        String snomedname = request.getParameter("snomednames");

        try {
            DBCon = new DBConnection();
            Con = DBCon.getConnection();
            selectsql = "SELECT referencedComponentId,sctName,mapTarget,icdName,mapRule,refid FROM snomedicd10map WHERE referencedComponentId=? or sctName=? ";
            pstm = Con.prepareStatement(selectsql);
            pstm.setString(1, snomedid);
            pstm.setString(2, snomedname);
            resultSet = pstm.executeQuery();
%>

    <h2 align="center">
        <font><strong>SNOMED CT TO ICD 10 </strong></font>
    </h2>

    <div style='min-height: 40px'>
        <table id="tblMain" width="100%" class='table1' border=0
            cellpadding='5' cellspacing='0' style='topmargin: -5px'>
            <tr>
                <td colspan='4' style='text-align: right;'>
                <!--    <input type="button" value="Back" onclick="goback();" style="background-color: #3399ff;color:#ffffff" /> -->    
                    <button onclick="goBack()" style="background-color: #3399ff;color:#ffffff">Back</button>
                </td>
            </tr>
            <tr class='headerText' style='height: 25px'>
                <td class='tdcellblue' style='width: 10%'>SNOMED</td>
                <td class='tdcellblue' style='width: 40%'>SNOMED Name</td>
                <td class='tdcellblue' style='width: 10%'>ICD-10</td>
                <td class='tdcellblue' style='width: 40%'>ICD Name</td>
            </tr>
            <%
                while (resultSet.next()) {
                    //String maprule = null;
                    maprule = (resultSet.getString("mapRule"));
                    if (maprule.matches("OTHERWISE TRUE")
                            || maprule.matches("TRUE")) {
            %>

            <tr>
                <td><%=resultSet.getString("referencedComponentId")%></td>
                <td><%=resultSet.getString("sctName")%></td>
                <td><%=resultSet.getString("mapTarget")%></td>
                <td><%=resultSet.getString("icdName")%></td>
            </tr>

            <%
                }
                }
            %>
        </table>

    </div>

        <% 
        if(request.getParameter("hiderefineproblem")==null  && maprule.matches("OTHERWISE TRUE")){ %> 

        <input type="button" value="Refine Problem" onclick="return showHide();" style="background-color: #3399ff;color:#ffffff;" />
        <%}%>    

     <div id="showHideDiv" style="display: none;"> 

        <p>Would one of the following diagnoses apply? Choose the most
            specific one:</p> 
        <FORM ACTION="snomedMapping.jsp#newres" METHOD="POST">
        <%
            pstm = Con.prepareStatement(selectsql);
            pstm.setString(1, snomedid);
            pstm.setString(2, snomedname);
            resultSet = pstm.executeQuery();
            boolean bSubmit=false;
            int refid=0;
            String[] pipe=null;
            while (resultSet.next()) {
                 refid=resultSet.getInt("refid");
                 pipe= resultSet.getString("mapRule").split("\\|");

                if (pipe.length > 1){
                    bSubmit=true;
        %>      

            <input type="radio" id="radioList" value="<%=refid%>" name="refId"/>            
            <tr><%=pipe[1]%></tr>
             <br /> 

        <%
            }
            }           
        %>

        <%if(bSubmit){%>
        <input type="hidden" name='hiderefineproblem' value='yes'/> 
        <INPUT TYPE="SUBMIT" value="Submit" style="background-color: #3399ff;color:#ffffff;"> 
        <%}%>

        </FORM>
          </div>

    <script>

        function showHide() {
            var ele = document.getElementById("showHideDiv")
            if (ele.style.display == "block") {
                ele.style.display = "none";
            } else {
                ele.style.display = "block";
            }
        }
        /* function goback(){
            window.location.href='/mobiledoc/jsp/catalog/xml/migration/snomedMapping/snomedIndex.jsp';
        } */
        function goBack() {
            window.history.back();
        }
    </script>


    <h2 align="center">
        <font><strong></strong></font>
    </h2>

    <div style='min-height: 40px' id="newres">
    <!--  <table id="tblMain" width="100%" class='table1' border=0
            cellpadding='5' cellspacing='0' style='topmargin: -5px'>

            <tr class='headerText' style='height: 25px'>
                <td class='tdcellblue' style='width: 10%'>SNOMED</td>
                <td class='tdcellblue' style='width: 40%'>SNOMED Name</td>
                <td class='tdcellblue' style='width: 10%'>ICD-10</td>
                <td class='tdcellblue' style='width: 40%'>ICD Name</td>
            </tr> -->
            <table>
            <%              
                String str=request.getParameter("refId");
                if(str.length()>0){
                refid=Integer.parseInt(str);

                //resultSet=Root.EcwCloseResultSet(resultSet);
                //pstm=Root.EcwClosePreparedStatement(pstm);

                selectsql = "SELECT * FROM snomedicd10map WHERE refid=?";
                pstm =  pstm = Con.prepareStatement(selectsql);
                pstm.setInt(1,refid);
                resultSet = pstm.executeQuery();
                while (resultSet.next()) {
            %>          
            <tr>
                <td colspan='2' style='font-weight:bold;'>SNOMED</td>

            </tr>
            <tr>
                <td><%=resultSet.getString("referencedComponentId")%>&nbsp;&nbsp;&nbsp;</td>
                <td><%=resultSet.getString("sctName")%></td>                
            </tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr>
                <td colspan='2' style='font-weight:bold;'>Target ICD</td>

            </tr>
            <tr>
                <td><%=resultSet.getString("mapTarget")%>&nbsp;&nbsp;&nbsp;</td>
                <td><%=resultSet.getString("icdName")%></td>
            </tr>
            <%
                 }
                }
            %>

        </table>
    </div>

<%  } 

如何实施?我需要再创建一个页面或在同一页面中创建一个页面。

您只需返回XML文本,而不是从JSP返回HTML文本

提供的代码具有HTML标记和JSP代码,用于创建HTML文档结构和内容


JSP的XML版本将包含XML标记和JSP代码,用于创建XML文档结构和内容。

是的,我使用XML标记,但我无法在前端UI中显示,因此,如果要在浏览器中显示XML数据,该怎么办,通常的方法是创建一个CSS样式表,告诉浏览器如何显示每个XML元素。你的拼写很糟糕,我真不敢相信你在小学就没有学过以大写字母开头的句子。