Solved

Applets & HTTP

Posted on 2001-06-27
13
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

626 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