从独立Java类调用JSP页面(不带Servlet)
我想从一个独立的Java类调用一个JSP页面(例如:)。我不想使用servlet。HttpURLConnection似乎不起作用。它不会带我去JSP页面。当我使用下面的代码调用JSP时,JSP中的print语句没有被调用,但是如果我单独调用JSP URL,print语句是可用的。试图寻找,但没有明确的答案 下面是我使用HttpURLConnection的代码片段:从独立Java类调用JSP页面(不带Servlet),java,jsp,call,Java,Jsp,Call,我想从一个独立的Java类调用一个JSP页面(例如:)。我不想使用servlet。HttpURLConnection似乎不起作用。它不会带我去JSP页面。当我使用下面的代码调用JSP时,JSP中的print语句没有被调用,但是如果我单独调用JSP URL,print语句是可用的。试图寻找,但没有明确的答案 下面是我使用HttpURLConnection的代码片段: URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnectio
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
我已经执行了你的代码,它工作正常。请注意,我只是改变了
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
到
这是我的jsp的唯一代码:
<% System.out.println("writing"); %>
当我使用下面的代码调用JSP时,JSP中的print语句没有被调用。。。你能准确地解释一下你做了什么、发生了什么以及你会期望什么吗?我的猜测是你可能想使用
html
StringBuffer
?还考虑使用<代码> StringBuilder <代码>。当我使用HTTPURLCONNECT时,我在JSP中添加的SysOutt语句没有被打印。而如果我直接调用JSP URL,它就会被打印出来。我想从Java类调用我的JSP。你不需要所有重定向的东西。默认情况下,它会这样做。文字被打印出来了吗?我的本地代码实际上是这样声明的:only URL obj=newurl(URL);是的,文本显示在java控制台中。为什么不尝试在java类中打印jsp的内容以确保调用它?
<% System.out.println("writing"); %>
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());