为什么不在jsp中打印?

时间:2012-08-21 07:38:17

标签: java jsp inputstream bufferedreader

下面的代码不会在jsp(while {}中的out.println)中打印,但它在Java程序中就像一个魅力。你能解释一下为什么不能在jsp中打印,我应该在代码中做些什么改变?谢谢!

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="javax.servlet.ServletException" %>
<%@ page import="javax.servlet.http.HttpServlet" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpServletResponse" %>
<%@ page import="java.net.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Test

<%try{
URL url = new URL("http://gesi-ro-test.banat.enelro:8010/dynamic/gesi/ri/elab/endcallrequest/wind.ser?id=008201dfa306f4a6&es=&is=2011/04/20%2013:09:46.593&rt=RE");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

String inputLine = "";
while ((inputLine = in.readLine()) != null)
    {
        out.println(inputLine);
    }
in.close();
}catch(Exception e){
    out.println(e);
    }%>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

此网址returns XML标记,但不会显示。您必须添加<pre> </pre>标记才能显示XML标记。

out.println("<pre>");
String inputLine = "";
while ((inputLine = in.readLine()) != null)
    {
        out.println(inputLine);
    }
out.println("</pre>");
in.close();

must必须在JSP中使用JSTL的<import/>而不是Java代码。

<c:import var="xmlData" url="http://your.url" />

<c:out var="${xmlData}"/>

答案 1 :(得分:1)

再次检查您的网址。我在其他网站上尝试了您的代码并且可以正常运行:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="javax.servlet.ServletException" %>
<%@ page import="javax.servlet.http.HttpServlet" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpServletResponse" %>
<%@ page import="java.net.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>

<%
URL url;
try {
    url = new URL("http://www.w3schools.com/xml/note.xml");
    BufferedReader in = new BufferedReader(new InputStreamReader(
            url.openStream()));

    String inputLine = "";
    while ((inputLine = in.readLine()) != null) {
%>

<c:out value="<%=inputLine%>"/>

<%
    }
    in.close();
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

%>

</body>
</html>

就个人而言,我不喜欢在jsp文件中放入很多“if”,“while”命令(它会使你的系统后来变得很乱),所以,尝试在你的业务逻辑中处理eveything然后将结果作为属性

发送到jsp