I am having trouble calling a JSP from a java application. I am trying
to call a JSP to do a search. To do this, I have a java application
that takes the word to search on as a command line argument. I am
pretty sure I am builing the URL correctly because I can paste the URL
into my browser and I get the results I expect; however, when I use
URLConnection to call the JSP, I get an HTML page back, but the JSP
says "Sorry, unable to execute search" "Empty term."
I am not an HTML person, so I may be missing something that is obvious
to you. Please mention anything you think, even you think it should be
"obvious" to an idiot.
Google got me past the proxy I must sit behind, but I didn't have any
luck finding help with what appears to be missing parameters.
I am doing this exercise to learn about the HTTP related classes in
Java, but I am hoping I don't have to implement the "Get" protocol to
make this work.
I will attach the source code for the program I am using (actually an
example program I got from java.sun.com). If you try to run the
program, you will need to remove or change the lines in method "main"
that allow the app to get through a proxy. Due to all the things I
have tried to get this work, the program is getting messy, so let me
know if I need to prune more code to make it easier to read.
thanks in advance
Hal
PS: The URL I am trying to hit is
http://ipac.plcmc.org/ipac20/ipac.jsp
__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
/**
* @author Administrator
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class Library {
// public static String URL_PREFIX =
"http://ipac.plcmc.org/ipac20/ipac.jsp?session=1T4628T0613V8.483&menu=search&aspect=basic_search&npp=25&ipp=20&profile=plcmc&ri=&index=TL&term=";
public static String URL_STRING = "http://198.86.35.154/ipac20/ipac.jsp";
public static String URL_PREFIX =
"http://ipac.plcmc.org/ipac20/ipac.jsp?session=1T4628T0613V8.483&menu=search&aspect=basic_search&npp=25&ipp=20&profile=plcmc&ri=&index=TL&term=";
public static String URL_SUFFIX = "&aspect=basic_search&x=15&y=13";
private static String DATA_PREFIX =
"session=1T4628T0613V8.483&menu=search&aspect=basic_search&npp=25&ipp=20&profile=plcmc&ri=&index=TL&term=";
public static String DATA_SUFFIX = "&aspect=basic_search&x=15&y=13";
public static String OUTPUT_FILE_NAME = "c:/temp/ar/ar.html";
/**
* Constructor for Library.
*/
public Library() {
super();
}
public static void main(String[] args) {
try {
String htmlResults = "Search Failed";
// go through a proxy
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "proxy.reveregroup.com" );
System.getProperties().put( "proxyPort", "80" );
HttpURLConnection.setFollowRedirects(false);
htmlResults = searchFor(args[0]);
System.out.println(htmlResults);
FileWriter out = new FileWriter(OUTPUT_FILE_NAME);
out.write(htmlResults);
out.flush();
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace(System.out);
}
} // end method public static void main(String[] args)
public static String searchFor(String keywords) throws MalformedURLException,
IOException {
String urlString = URL_PREFIX + keywords + URL_SUFFIX;
StringBuffer buff = new StringBuffer(20000);
/* TRY 1
URL url = new URL(urlString);
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
String str;
*/
urlString =
"http://ipac.plcmc.org/ipac20/ipac.jsp?session=A046357066O5X.420&menu=search&aspect=basic_search&npp=25&ipp=20&profile=plcmc&ri=&[EMAIL
PROTECTED]&index=TW&term=Duck&aspect=basic_search&x=7&y=12";
urlString =
"http://ipac.plcmc.org/ipac20/ipac.jsp?session=10V635J9E8390.545&menu=search&aspect=basic_search&npp=25&ipp=20&profile=plcmc&ri=&index=TW&term=duck&aspect=basic_search&x=20&y=18";
urlString =
"http://ipac.plcmc.org/ipac20/ipac.jsp?session=D046360140M96.616&menu=search&aspect=basic_search&npp=25&ipp=20&profile=plcmc&ri=&[EMAIL
PROTECTED]&index=TL&term=duck&aspect=basic_search&x=13&y=15";
// urlString = "http://ipac.plcmc.org";
//urlString = URLEncoder.encode(urlString);
String data = DATA_PREFIX;
data += URLEncoder.encode(keywords) + "=" + DATA_SUFFIX;
/* TRY 2 */
// Construct data
// String data = URLEncoder.encode("key1", "UTF-8") + "=" +
URLEncoder.encode("value1", "UTF-8");
// data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" +
URLEncoder.encode("value2", "UTF-8");
// Send data
System.out.println("using URL <" + urlString + ">");
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
buff.append(line);
buff.append("\n");
}
wr.close();
rd.close();
return buff.toString();
} // end method public static String searchFor(String keywords)
} // end class Library
---
You are currently subscribed to jdjlist as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
http://www.sys-con.com/fusetalk