开发者

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

<%
               InputStreamReader in= new InputStreamReader(new FileInputStream("C:\\Tomcat\\data\\xml\\T开发者_JAVA百科mpXML.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.

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

Can any one from JSP expert group help me to guide how can i do this. Additionaly my xml file location is

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

inside my tomcat directoy is there any way to refer this location in reletive way rather than absolute one.

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. 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. 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).

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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜