Calling a JSP from java

J

Juan Pardillos

Hi everybody,

I trying to call a .jsp page from Java, but no success for the moment.

The code I'm using is:

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.io.*;

public class MyClass {

public static void download(InputStream in, File file) throws
IOException {
System.out.println("Creating output file");
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
System.out.println("Created output file");
}
public static void main(String[] args) throws IOException {
URL url = new URL("PutHereTheURL");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();

connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
connection.setFollowRedirects(false);
connection.connect();

// Send the data
String postString = "option1=" + ;
DataOutputStream dos = new DataOutputStream(
connection.getOutputStream() );
dos.writeBytes(postString);
dos.close();

// Read the resulting data from the Post
InputStream is = connection.getInputStream();
download(is, new File("myFile.html"));
connection.disconnect();
}
}

But the html page that I obtain as a result is the initial page
corresponding to the input form. What I want is to submit that input
form with value "valueOfOption1" for the input choice "option1".

Any ideas of what can be the problem?

Thanks in advance
 
W

Wendy S

Juan Pardillos said:
But the html page that I obtain as a result is the initial page
corresponding to the input form. What I want is to submit that input
form with value "valueOfOption1" for the input choice "option1".
Any ideas of what can be the problem?

Look at the source for the HTML form. Are you sure the form posts to the
same .jsp? In my world, HTML forms post to _Servlets_ not to JSPs.

You might find myPage.jsp contains
<html><form action="/myServlet"> ...

And then the Servlet mapped to that URL does its job and forwards to yet
another JSP to display the results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top