-1
我必須實現jspInit()
方法才能讀取文本文件並在網頁上打印內容。但我不知道該把它放在哪裏。 我試過<DOCTYPE>
之前,其他人之間的jsp進口,但我無法訪問像out
對象。我在哪裏寫jspInit()
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.PrintWriter"%>
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="ML.*"%>
<%!
public void jspInit() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("/home/ambra/a.txt"));
String s = br.readLine();
while(s != null){
}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<!-- Print here the content of the text file -->
</body>
</html>
謝謝。那麼,如果我必須在網頁上打印什麼呢?像「這是一個jspInit方法」?我試着用'response.getEritter()'得到了這個寫入器,但是沒有工作。任何想法 ? – bogALT
響應也是一個隱含的對象。所以你不能那樣做。通過這個鏈接:http://stackoverflow.com/questions/138999/how-to-output-html-from-jsp-block – Bikku
但是...''jspInit()'不默認調用,不是嗎? ?我必須明確地稱呼它。順便說一句,我解決了它:'<%! public void jspInit(javax.servlet.jsp.JspWriter o)throws IOException o.println(「Yupiiii」); } %' 然後在scriptlet中調用它:'<% jspInit(out); %>' 可以嗎? – bogALT