Solved

Applets & HTTP

Posted on 2001-06-27
13
174 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now