简体   繁体   English

使用JSP在浏览器中显示XML内容

[英]Showing XML content in browser using JSP

I am not familir with JSP but coomfortable with Java, I am trying to show XML content in the browser using JSP only and came up with this code 我对JSP不熟悉,但对Java不满意,我试图仅使用JSP在浏览器中显示XML内容,并提出了以下代码

<%
               InputStreamReader in= new InputStreamReader(new FileInputStream("C:\\Tomcat\\data\\xml\\TmpXML.xml"));
               BufferedReader br = new BufferedReader(in);
               String line = br.readLine();
               while(line!=null){
               out.println(line);
               line = br.readLine();
              }

              %>

Its showing me the XML perfectly but i have more requirements with respect to this,like i will have 3-4 XML files and i want to show user links so moment user click on the link, it should fetch data (XML or any other ) from the specified location and must show that on browser as currently above code is getting execute at page load time. 它完美地显示了XML,但对此我有更多要求,例如我将拥有3-4个XML文件,并且我想显示用户链接,因此,当用户单击链接时,它应该获取数据(XML或任何其他)从指定位置开始,并且必须显示当前在浏览器上的上述代码正在页面加载时执行。

i tried to do something like 我试图做类似的事情

<%!
                  public void showTempXML(){
                                      InputStreamReader in= new InputStreamReader(new FileInputStream("C:\\Tomcat\\data\\xml\\TmpXML.xml"));
                  BufferedReader br = new BufferedReader(in);
                  String line = br.readLine();
                  while(line!=null){
                   out.println(line);
                   line = br.readLine();
                  }
              } %>

but it showing me error out cannot be resolved 但显示出错误out cannot be resolved

Can any one from JSP expert group help me to guide how can i do this. JSP专家组的任何人都可以帮助我指导如何做到这一点。 Additionaly my xml file location is 另外,我的xml文件位置是

Tomcat\\data\\xml\\TmpXML.xml

inside my tomcat directoy is there any way to refer this location in reletive way rather than absolute one. 在我的tomcat directoy内部,有什么方法可以通过重复的方式而不是绝对的方式来引用此位置。

Thanks in advance 提前致谢

You could do something like this: 您可以执行以下操作:

<%@page import="java.util.Arrays"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStreamReader"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String[] allowedFiles = {"File1.xml", "File2.xml", "File3.xml", "File4.xml"};

    String reqFile = request.getParameter("file");

    if(!Arrays.asList(allowedFiles).contains(reqFile)){
%>
    <html>
        <body>
            <p>Please choose a file:</p>
            <ul>
<%
        for(String file : allowedFiles){
%>
                <li><a href="?file=<%=file%>"><%=file%></a></li>
<%
        }
%>
            </ul>
        </body>
    </html>
<%
    }else{
        response.setContentType("text/xml");
        InputStreamReader in = new InputStreamReader(new FileInputStream(reqFile));
        char[] buffer = new char[2048];
        int read;
        while((read = in.read(buffer)) >= 0){
            out.write(buffer, 0, read);
        }   
    }
%>

Basically, your accept the file that the user wants to see as a parameter. 基本上,您接受用户希望看到的文件作为参数。 If that parameter does not yet exist (or is invalid), display the list of choices to the user. 如果该参数尚不存在(或无效),请向用户显示选择列表。

Note that I changed the reading of the XML to not use a BufferedReader. 请注意,我将XML的读取更改为不使用BufferedReader。 There is no need to search for the various lines of the XML in this scenario, so just buffering a fixed number of characters will be more efficient. 在这种情况下,无需搜索XML的各行,因此仅缓冲固定数量的字符将更加有效。 If you don't need to worry about the actual encoding of the source XML file matching something that the client's web browser can display, you could further improve this by avoiding the Reader* classes entirely, and just copying bytes instead - though this would be much better suited from within an actual servlet (instead of a JSP that gets compiled into a servlet). 如果您不必担心源XML文件的实际编码与客户端的Web浏览器可以显示的内容相匹配,则可以通过完全避免使用Reader *类,而只复制字节来进一步改进此方法-尽管这样做可以更好地适合于实际的servlet(而不是将JSP编译为servlet)。

Note also, somewhere you will need to include the path of the files you're looking to serve. 另请注意,您将需要在某些地方包含要提供的文件的路径。 You could either do this one-by-one within the allowedFiles list - helpful if you're looking to serve from different paths, or you could append a base file on to reqFile within the call to the FileInputStream constructor. 您可以在allowedFiles列表中一个接一个地执行此操作-如果要从其他路径提供服务,则reqFile ,或者可以在对FileInputStream构造函数的调用reqFile基本文件附加到reqFile Be warned that you do not want provide free reign to the user here, as doing so would allow read access to the entire server. 但是要注意,你不想在这里为用户提供免费的统治,因为这样做将允许对整个服务器的读取访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM