java - 为什么在jsp中不能打印?

标签 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>

最佳答案

再次检查您的网址。我在另一个网站上尝试了您的代码,它有效:

<%@ 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”命令(这会让你的系统以后变得一团糟),所以,尽量在你的业务逻辑中处理所有的事情,然后将结果作为属性发送到jsp

关于java - 为什么在jsp中不能打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12050256/

相关文章:

Java:摩尔斯电码转换器

java - WebLogic 12.2.1 中的最大帖子大小

java - 通过 gradle 构建公开依赖项

java - Struts 标签中的 # 、 % 和 $ 符号有什么区别?

jsp - 如何在jsp的函数内使用 "request"对象

java - ObjectInputStream 对 FileInputStream 满意,对 getResourceAsStream 不满意

java - 如何将jdom文档放入Inputstream

java - Android:JSONObject 无法转换为 JSONArray

getInputStream() 上的 java.io.FileNotFoundException

java - 将 bufferedImage 转换为 jsp 成功,但几秒钟后它就消失了