Solved

Applets & HTTP

Posted on 2001-06-27
13
177 Views
Last Modified: 2013-11-23
I need to write a Java applet that can request pages possibly passing form or query string data to the pages from the server the applet came from.

i need to know if there is a good HTTP class for java, where to get it etc and an example of requesting a page sending form data and then displaying the response on the command line would be nice.

thanx alot.

Paul
0
Comment
Question by:makerp
  • 7
  • 5
13 Comments
 
LVL 5

Expert Comment

by:Calron
ID: 6231046
Have a look at:

http://java.sun.com/docs/books/tutorial/networking/index.html

This tutorial explains the use of the URL class which is exactly what you are looking for. It allows you to post and get data from Http connections.
0
 
LVL 10

Author Comment

by:makerp
ID: 6231060
cool, okay i will leave this open for a few days because i want to get someother links and advice, also i dont intend to start this until next monday. but dont worry i will award the points (to the best answer of course).

(a little example would be nice)

Paul
0
 
LVL 5

Expert Comment

by:Calron
ID: 6231091
One example of reading from a URL (pretty much what the tutorial shows):

It read the whole text from that URL and returns it as a string.

 public static String getPage(String urlString)
  {
    String page = new String();
    try
    {
      URL dest = new URL(urlString);
      BufferedReader in = new BufferedReader(
                     new InputStreamReader(
                     dest.openStream()));

      String inputLine;

      while ((inputLine = in.readLine()) != null)
      {
     page = page + "\n" + inputLine;
      }

      in.close();
    }
    catch (MalformedURLException e)
    {
      return new String("MalformedURLException occurred: " + e.getMessage());
    }
    catch (IOException e)
    {
      return new String("IOException occurred: " + e.getMessage());
    }

    return page;
  }
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 10

Author Comment

by:makerp
ID: 6231106
what about posting data to the page
0
 
LVL 5

Expert Comment

by:Calron
ID: 6231113
And posting to a URL:

public void sendToServer () {
   try {
    URL query = new URL( "http://yourserver.com/abc.cgi " );

    HttpURLConnection conn = ( HttpURLConnection) query.openConnection();

    conn.setRequestMethod( "POST" );
    conn.setDoOutput(true);

    OutputStream os =  conn.getOutputStream();

    /* write to the OutputStream here */

    os.close();

   } catch ( Exception E ) {
     E.printStackTrace();
   }
 }
0
 
LVL 5

Expert Comment

by:Calron
ID: 6231122
The first example could also be solved using the URLConnection class, as this class has the getOuputStream and a getInputStream class.
0
 
LVL 5

Expert Comment

by:Calron
ID: 6231146
One thing, if you want to do the same using https protocol you will need JSSE packeage from sun. Once you have that included in you classpath https connections can be made using pretty much the same methods as above.

http://java.sun.com/products/jsse/index.html
0
 

Expert Comment

by:SimonSay
ID: 6233119
Here is a HTTPMessage class that you can use to simplify your production :

http://www.servlets.com/cos/javadoc/com/oreilly/servlet/HttpMessage.html


You can also take a look at the HTTP client source or the applet source in this page as an example.

http://www.servlets.com/jservlet2/examples/ch10/index.html
0
 
LVL 10

Author Comment

by:makerp
ID: 6243979
where can i get JDK 1.new version, i only have 1.1.8, also the http stuff, does this come with it?

Paul

0
 
LVL 5

Expert Comment

by:Calron
ID: 6243991
Try:

http://java.sun.com/j2se/1.3/

to download the JDK 1.3, which is currently the newest version. 1.4 is available as Beta and will be released inb Fall.
0
 
LVL 10

Author Comment

by:makerp
ID: 6244068
does this have all the http stuff i need
0
 
LVL 5

Accepted Solution

by:
Calron earned 300 total points
ID: 6244080
It has all the classes you need unless you are planning to access https connections where you will require the JSSE package, which can be downloaded from:

http://java.sun.com/products/jsse/index.html
0
 
LVL 10

Author Comment

by:makerp
ID: 6244112
thank you for your help and being patient for me to acept your answer, i hope you can help me again sometime
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question