If you use the URL.openStream() method, there's no way to determine whether a request was successful or not. The only alternative is to use the URL.openConnection() method, which returns a URLConnection instance. The URLConnection is an abstract class, meaning that it provides a template of methods which other classes will implement. Even though the URL.openConnection() method returns a URLConnection instance, it is actually returning a concrete implementation of that class.
When a request is made for a resource using the Hypertext Transfer Protocol, the implementation of URLConnection that is returned is a java.net.HttpURLConnection. This class defines additional methods, one of which allow you to access the response status code. To find out the status of a request, you need to cast the URLConnection to a HttpURLConnection, and invoke the int HttpUrlConnection.getResponseCode() method.
URL url = new URL ( some_url );
URLConnection connection = url.openConnection();
connection.connect();
// Cast to a HttpURLConnection
if ( connection instanceof HttpURLConnection)
{
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int code = httpConnection.getResponseCode();
// do something with code .....
}
else
{
System.err.println ("error - not a http request!");
}
SOURCE : www.referjava.com
Tuesday, February 13, 2007
How can I create an event handling mechanism in Java? I'm used to ActiveX controls, which fire asynchronous events (i.e. when a control is finished a
The most common mechanism for this is the callback, where one class calls the method of another to notify it of an action or event. The class to be notified defines methods that will respond to specific events, such as when a mouse is clicked, dragged, or released. The AWT makes heavy use of this, with Listener interfaces. A class implements the event handling methods of a listener, and can then be registered with a component that generates these types of events. Classes that are event sources provide methods which register a listener, and at a later time when the event is generated, will invoke listener methods. The Abstract Windowing Toolkit (AWT) and Swing APIs would be a good place to start, to see if this suits your needs.
SOURCE : www.referjava.com
SOURCE : www.referjava.com
How can I poll a remote server, to see if it is still available? I'd like to check to see if the server has crashed, and if so, record the time.
If your application is a TCP server, the easiest way to do this is for a client to connect to it, using the java.net.Socket class. When the Socket is connected, if the server is down an IOException will be thrown, indicating that the server is not accepting connections.
However, this may not be a sufficient test for mission critical applications. What if the server has stalled, and will accept connections but not respond to them? In this situation, you'll need to write a valid request to the server (using the appropriate network protocol, such as HTTP or FTP). It doesn't matter what type of request is made, or what data is really returned (unless it is an error message). The only purpose of the request is to see if the server will response, and is still available for use.
SOURCE : www.referjava.com
However, this may not be a sufficient test for mission critical applications. What if the server has stalled, and will accept connections but not respond to them? In this situation, you'll need to write a valid request to the server (using the appropriate network protocol, such as HTTP or FTP). It doesn't matter what type of request is made, or what data is really returned (unless it is an error message). The only purpose of the request is to see if the server will response, and is still available for use.
SOURCE : www.referjava.com
Who created Java?
Java is still a relatively new language, so it is quite amusing to some to think that Java has a "history" behind it. One of the most frequent questions I've been getting lately though is about the origins of Java.
Java was created by engineers working at Sun Microsystems. The figure that stands out most of all is James Gosling, widely regarded as the "father" of Java. James and his team were working on a language whose original name was Oak. Oak was designed for embedded devices, such as mobile phones. The first publicly available version of Java, however, was as Java applets, in the original HotJava browser. From there, Java grew to what it is today.
SOURCE : www.referjava.com
Java was created by engineers working at Sun Microsystems. The figure that stands out most of all is James Gosling, widely regarded as the "father" of Java. James and his team were working on a language whose original name was Oak. Oak was designed for embedded devices, such as mobile phones. The first publicly available version of Java, however, was as Java applets, in the original HotJava browser. From there, Java grew to what it is today.
SOURCE : www.referjava.com
How do I pass a primitive data type by reference? For example, how do I make an int passed to a function modifiable.
Java passes all primitive data types by value. This means that a copy is made, so that it cannot be modified. When passing Java objects, you're passing an object reference, which makes it possible to modify the object's member variables. If you want to pass a primitive data type by reference, you need to wrap it in an object.
The easiest of all is to pass it as an array (or even a Vector). Your array only needs to contain a single element, but wrapping it in an array means it can be changed by a function. Here's a simple example of it in action.
public static void increment(int[] array, int amount)
{
array[0] = array[0] + amount;
}
public static void main(String args[])
{
int[] myInt = { 1 };
increment (myInt, 5);
System.out.println ("Array contents : " + myInt[0]);
}
Of course, if you're modifying the contents of parameters passed to a method, you really should try to avoid this behavior. It increases the complexity of code, and really should be avoided. The preferred way is to return a value from a method, rather than modifying parameter values directly.
www.referjava.com
The easiest of all is to pass it as an array (or even a Vector). Your array only needs to contain a single element, but wrapping it in an array means it can be changed by a function. Here's a simple example of it in action.
public static void increment(int[] array, int amount)
{
array[0] = array[0] + amount;
}
public static void main(String args[])
{
int[] myInt = { 1 };
increment (myInt, 5);
System.out.println ("Array contents : " + myInt[0]);
}
Of course, if you're modifying the contents of parameters passed to a method, you really should try to avoid this behavior. It increases the complexity of code, and really should be avoided. The preferred way is to return a value from a method, rather than modifying parameter values directly.
www.referjava.com
How do I debug my Java applets to see what's going wrong?
Often, when an applet behaves strangely or fails to work, the problem is hard to diagnose. Many applets behave fine under Netscape Navigator, only to fail under Internet Explorer (or vice versa). Sometimes a different Java Virtual Machine (JVM) will have less tolerance for a bug, and other times the fault lies in the actual JVM implementation.
The lack of information provided by the browser can be frustrating. However, there is a way to display a debugging console, which will provide developers with additional information. For example, an uncaught exception will generate an exception trace, telling you which method and which exception were involved. Both the later versions of Netscape Communicator and Internet Explorer support these debugging consoles.
To view the Java Console in Netscape Communicator
Load any page with an applet, and then select the following menu option
Communicator -> Tools -> Java Console
www.referjava.com
The lack of information provided by the browser can be frustrating. However, there is a way to display a debugging console, which will provide developers with additional information. For example, an uncaught exception will generate an exception trace, telling you which method and which exception were involved. Both the later versions of Netscape Communicator and Internet Explorer support these debugging consoles.
To view the Java Console in Netscape Communicator
Load any page with an applet, and then select the following menu option
Communicator -> Tools -> Java Console
www.referjava.com
How do I get parameters from a HTML form when using servlets?
When extending javax.servlet.http.HttpServlet, you should override either of the following two methods: -
public void doGet(HttpServletRequest req,
HttpServletResponse res)
public void doPost(HttpServletRequest req,
HttpServletResponse res)
Both of these methods accept as a parameter a HttpServletRequest instance. This allows the servlet to obtain information about the browser request, including the parameters passed to the servlet. By using the String getParameter(String) method, you can request any parameter you need. If the parameter is not present, a null value will be returned.
www.referjava.com
public void doGet(HttpServletRequest req,
HttpServletResponse res)
public void doPost(HttpServletRequest req,
HttpServletResponse res)
Both of these methods accept as a parameter a HttpServletRequest instance. This allows the servlet to obtain information about the browser request, including the parameters passed to the servlet. By using the String getParameter(String) method, you can request any parameter you need. If the parameter is not present, a null value will be returned.
www.referjava.com
Subscribe to:
Posts (Atom)