Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Newsletters Archive: 15

JavaFAQ Home » Java Newsletters Go to all tips in Java Newsletters


Bookmark and Share 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 *
* *
**********************************************


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy