Content received from: http://JavaFAQ.nu/java-article341.html


Java Newsletters Archive: 15
Monday, February 02, 2004 (14:56:08)

Posted by jalex

New Page 1

**********************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
*
* Issue No: 15 14 December 2000
* http://www.javafaq.nu/java
*
*
* Please recommend us and our FREE "100 Java Tips" book to your
* friends and colleagues!
* http://javafaq.nu/java/advert/our_book.shtml
**********************************************

Table of Contents

1. I want to send a POST request, but I can't find such
functionality in the servlet API, how can I do this?
2. How do I delete a file in Java?
3. How can I get an IP Adress in the class InetAdress? The
constructor is private, so I can’t use it.
4. Is there a mod (x, y) function that returns the remainder when
x is divided by y?
5. This SUPER tip you can read just on our site 24 December!
6. What is difference capacity() and size() methods for vector?
7. How do I generate mouse click event but without a real click?
**********************************************

Hello dear friends!

We published again pdf-version (Acrobat Reader) of our "100 Java Tips"
http://javafaq.nu/java/advert/our_book.shtml
We made more descriptive contents: instead of "Tip 1" you can see
"Tip 1. How I can ..." and also made small corrections.
We continue our job on book updating and will add new tips in it.


By the way, I found very useful free application - Zapper that
helps to make life much easier and offers a new, more efficient
way to get the information you need from the Web.
It takes you from wherever you are on your desktop to exactly
what you're looking for on the Web - in just one click of your mouse!


**********************************************

Tip 1 Q: I want to send a POST request, but I can't find such
functionality in the servlet API, how can I do this? Must I
implement this with a socket connection to port 80?

Answer: A servlet can do anything a standalone Java application
can do. It doesn't need anything beyond what the java.net package
already provides. You can use an httpURLConnection to POST to a
server program like a servlet or CGI script:

Create a string with the parms you want to post and convert it to
a byte array. You may need to pass the values through
java.net.URLEncoder.encodeURL(). If they have embedded blanks
or special characters

String parms = "a=10" + "&b=20" + "&c=30";
byte[] bytes = parms.getBytes();

Create a URL pointing to the servlet or CGI script and open an
HttpURLConnection on that URL

URL url = new URL(TARGET_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

Indicate that you will be doing input and output, that the method
is POST, and that the content length is the length of the byte
array

con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-length",
String.valueOf(bytes.length));

// Write the parameters to the URL output stream

OutputStream out = con.getOutputStream();
out.write(bytes);
out.flush();
// Read the response
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null) break;
System.out.println(line);
}
in.close();
out.close();
con.disconnect();

--
Phil Hanna
Author of Instant Java Servlets
http://www.philhanna.com

**********************************************
Tip 2 Q: How do I delete a file in Java? I have programmed a Java
application that needs to delete a file, but I couldn't find
anything on the topic.

Answer: in java.io.File there is delete
public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Create a new File object representing the file, and then use the delete () method. If you use the Microsoft JVM, make sure the file is not shared before you delete it, otherwise that will not work (the delete () method returns "false").

**********************************************

Tip 3 Q: How can I get an IP Adress in the class InetAdress? The
constructor is private, so I can’t use it. I want to call the
method getName () to get the domain name out of an IP Adress.

A: It is not necessary to construct something Smile
Just do it like this:
String hostname = InetAddress.getLocalHost().getHostName();


**********************************************

Tip 4 Q: Is there a mod (x, y) function that returns the
remainder when x is divided by y? Something equivalent to
fmod(x,y) in C?

Answer: a = x%y;

**********************************************

Tip 5 This SUPER tip you can read just on our site 24 December!

**********************************************

Tip 6 Q: What is difference between

public final int capacity()
Returns the current capacity of this vector.

and

public final int size()
Returns the number of components in this vector.

Answer: please read this method: ensureCapacity
public final synchronized void ensureCapacity(int minCapacity)

Increases the capacity of this vector, if necessary, to ensure
that it can hold at least the number of components specified
by the minimum capacity argument.
Parameters:
minCapacity - the desired minimum capacity.

**********************************************

Tip 7 How do I generate mouse click event but without a real click?
Q: Could you tell me how I can generate an event (like a mouse
click) from a Java application? (I want generate a mouse click
event but without a real click on the mouse).

Answer: See "simulate keyboard pressing" below: You can use
java.awt.Robot" since java1.3


The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

John Andersson,
Copyright (c) 2000 John Andersson
**********************************************
* You can find our tips on site also! *
* Please recommend us and our FREE "100 Java Tips" book to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
* *
**********************************************