• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connecting to a JSP inside a Java app?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.net.URLConnection
 
M Burke
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed this line and it started working

connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic