• Post Reply Bookmark Topic Watch Topic
  • New Topic

Connecting to a JSP inside a Java app?

 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jsp running on a server, and I want to connect to it (POST-GET), send it parameter pairs (parm1="value1") and get an answer from it.

I know this is normally done with a web browser, but I want to do it behind the scenes inside a Java app.

My tried this using a C++ class but there is no info on how to send parameters. Is there a Java solution?
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Well, I made some progress. But I am having trouble passing parameters. I get a "null" back from the jsp.

import java.io.*;
import java.net.*;

public class GetAddress {

public static void main(String[] args) throws Exception {

URL url = new URL("http://localhost:8081/testjsp/test.jsp");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

PrintWriter out = new PrintWriter( connection.getOutputStream());

out.println("address=\"1818+main+st\"");
out.close();

BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;

System.out.println("read response:");

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
}



Here is the JSP running on Tomcat

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test JSP Call</title>
</head>
<body>

<% String addr1 = request.getParameter("address1"); %>

address = <%= addr1 %>


</body>
</html>
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say you are getting "null" back from your web server, are you getting back an empty InputStream or an HTML document with the text "address = null"?
According to the documentation:

# The connection object is created by invoking the openConnection method on a URL.
# The setup parameters and general request properties are manipulated.
# The actual connection to the remote object is made, using the connect method.
# The remote object becomes available. The header fields and the contents of the remote object can be accessed.

I don't see you invoking connect() anywhere.
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting an HTML document with the text "address = null". I tested the jsp with a web browser and it reads the parameter and spits it back out ok. I just can get it to work in code.

The examples I can find all use a server CGI script, not a JSP. Could it be I need to do something different for a JSP?

I have been working on the code, and I added some calls. But it still does not work.

import java.io.*;
import java.net.*;

public class GetAddress {

public static void main(String[] args) throws Exception {

String query;

query = URLEncoder.encode("address", "UTF-8") + "=" +
URLEncoder.encode("1818 main", "UTF-8") + "\r\n";

URL url = new URL("http://localhost:8081/testjsp/work/test.jsp");
URLConnection connection = url.openConnection();

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-type", "application/octet-stream");
connection.setRequestProperty("Content-length",""+query.length());

connection.connect();

DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(query);
out.flush();
out.close();

BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed this line and it started working

connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!