2016-06-08 77 views
-1

我要调用一个JSP页面(如:http://www.google.jsp)从一个独立的Java类。我不想使用servlet。 HttpURLConnection似乎不起作用。它没有把我带到JSP页面。当我使用下面的代码调用JSP时,我的JSP中的print语句没有被调用,但是如果我单独调用JSP URL,则可以使用print语句。尝试搜索,但没有明确的答案。调用JSP页面从一个独立的Java类(如果没有的Servlet)

我的代码片段使用HttpURLConnection的低于:

URL obj = new URL(url); 
HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); 
conn.setReadTimeout(5000); 
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); 
conn.addRequestProperty("User-Agent", "Mozilla"); 
conn.addRequestProperty("Referer", "google.com"); 
System.out.println("Request URL ... " + url); 
boolean redirect = false; 
// normally, 3xx is redirect 
int status = conn.getResponseCode(); 
if (status != HttpURLConnection.HTTP_OK) { 
    if (status == HttpURLConnection.HTTP_MOVED_TEMP 
     || status == HttpURLConnection.HTTP_MOVED_PERM 
      || status == HttpURLConnection.HTTP_SEE_OTHER) 
    redirect = true; 
} 
conn.setInstanceFollowRedirects(true); 
System.out.println("Response Code ... " + status); 
System.out.println("Response Message ... " + conn.getResponseMessage()); 
if (redirect) { 
    // get redirect url from "location" header field 
    String newUrl = conn.getHeaderField("Location"); 
    // get the cookie if need, for login 
    String cookies = conn.getHeaderField("Set-Cookie"); 
    // open the new connnection again 
    conn = (HttpURLConnection) new URL(newUrl).openConnection(); 
    conn.setRequestProperty("Cookie", cookies); 
    conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); 
    conn.addRequestProperty("User-Agent", "Mozilla"); 
    conn.addRequestProperty("Referer", "google.com"); 
    System.out.println("Redirect to URL : " + newUrl); 
} 
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
String inputLine; 
StringBuffer html = new StringBuffer(); 
while ((inputLine = in.readLine()) != null) { 
    html.append(inputLine); 
} 
in.close(); 
System.out.println("Done") 

输出:

sResultsURL....http://www.google.jsp 
Request URL ... http://www.google.jsp 
Response Code ... 200 
Response Message ... OK 
Done 
+2

*当我使用下面的代码调用JSP *时,我的JSP中的打印语句没有被调用......您能否灵活地解释您做了什么,会发生什么以及您会期望什么? –

+0

我的猜测是你可能想**使用'html''StringBuffer'的内容?另外考虑使用'StringBuilder' – 2016-06-08 11:41:41

+0

当我使用HttpURLConnection时,我在JSP中添加的Sysout语句没有得到打印。而如果我直接调用JSP URL,则会打印它。 我想从Java类调用我的JSP。 – Jeeva

回答

0

我执行你的代码和它的作品。只是注意到我只改

HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); 

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

这是我的JSP的唯一代码:

<% System.out.println("writing"); %> 

此外,添加以下内容到Java类,你可以日志在jsp的内容,以确保您呼叫到正确的网址:

InputStream is = conn.getInputStream(); 

BufferedReader buffer = null; 
buffer = new BufferedReader(new InputStreamReader(is, "iso-8859-9")); 

StringBuffer builder = new StringBuffer(); 
int byteRead; 
while ((byteRead = buffer.read()) != -1) { 
    builder.append((char) byteRead); 
} 
buffer.close(); 
System.out.println(builder.toString()); 
+0

写作得到打印? 我在本地的代码实际上是这样声明的 URL obj = new URL(url); – Jeeva

+0

是的,文本出现在Java控制台。你为什么不尝试在java类中打印jsp的内容以确保你正在调用它? –