Home » Archive

Articles Archive for March 2009

Java Codes »

[6 Mar 2009 | No Comment | 44 views]

This example shows how to forward a http call from a servlet to another page in the same web application.To do a forward we need to get an instance of RequestDispatcher by calling the getRequestDispatcher() method on the HttpServletRequest object.The page to which the call will be forwarded is sent to the method as parameter. Finally the forward() method of the RequestDispatcher is called with both the request and response objects as parameters.
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/** * Example Servlet * @au*#116;*#104;o*#114; *#119;*#119;w*#46;javadb.*#99;*#111;*#109; */public class ExampleServlet extends HttpServlet {        /** Processes requests for both HTTP …

Java Codes »

[6 Mar 2009 | No Comment | 64 views]

To redirect a call from a servlet to some other url, use the sendRedirect() method of the HttpServletResponse object that is passed to the service() method.The sendRedirect() method takes the new destination url as a string parameter.
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/** * Example Servlet * *#64;*#97;*#117;*#116;*#104;*#111;*#114; *#119;w*#119;.*#106;a*#118;*#97;d*#98;.*#99;*#111;*#109; */public class ExampleServlet extends HttpServlet {        /**      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.     * @param request servlet request     * @param response servlet response     */    protected void service(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {        //Redirect call to another url        response.sendRedirect(“http://www.java.com”);    }    }

Java Codes »

[6 Mar 2009 | No Comment | 60 views]

This java code example shows how to invoke methods on an object at runtime without knowing the names of them in advance.This is possible using the reflection API. What we do is to get an instance of the class object of the particular class we want to call methods on and by using that instance we can get the array of Method objects by calling getDeclaredMethods().We don’t actually call the methods of the class instance, instead we have to create an object that we call the methods on by sending …

Java Codes »

[6 Mar 2009 | No Comment | 69 views]

In this example we use the Reflection api to obtain the methods of a particular class called Person, which is an inner class. First we need to get the Class object and we do so by calling ‘Person.class’. Once we have the Class object we can use it to call the getDeclaredMethods() method which will return an array of type Method.To find out the names of the methods that the Person class contains we simply loop through the array and call getName() for each Method object.
import java.lang.reflect.Method;/** * * @author javabout.com */public class …

Java Codes »

[6 Mar 2009 | No Comment | 125 views]

This java code example shows how to create an object at runtime for which the name of the class is not know at compile time.We use the forName() method to load the class and then use the newInstance() method to create the object.Then we use reflection to get the methods of the class and invoke them. The class we are instantiating is called MyClass (see further below) and we assume thatthe methods we are interested in starts with the string ‘say’ (we don’t want to call all the methods inherited …

Java Codes »

[6 Mar 2009 | No Comment | 68 views]

This code example performs a NS lookup and prints out the IP-address along with the host name.This is possible due to the class java.net.InetAdress which we can instantiate through the static method getByName().
import java.net.InetAddress;import java.net.UnknownHostException;/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method performs a NS Lookup     */    public void performNSLookup() {                try {                        InetAddress inetHost = InetAddress.getByName(“cnn.com”);            String hostName = inetHost.getHostName();            System.out.println(“The host name was: “ hostName);            System.out.println(“The hosts IP address is: “ inetHost.getHostAddress());                    } catch(UnknownHostException ex) {                        System.out.println(“Unrecognized host”);        }    }    /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().performNSLookup();    }}

Java Codes »

[6 Mar 2009 | No Comment | 75 views]

This Java code example shows how to download a webpage into a StringBuilder instance using the java.net.URL and java.net.URLConnection classes.First we create an URL object specifying the url for the page as argument. URLConnection is an abstract class and cannot be instantiated so we need to call its static method openConnection() to get an instance of it. Now the connection to the web page is open and we can start reading data from it.To do that we call getInputStream on the URLConnection object and use it as an argument to …

Java Codes »

[6 Mar 2009 | No Comment | 63 views]

This example shows how to sent a POST request to a server with attached parameters. Two parameters are sent in the example code below, width and height.We use the URL and URLConnection classes to open the connection to the destination. Then the output stream is retrieved by calling getOutputStream() on the URLConnection object.With the output stream we can write the parameters and then start reading the response from the server using the input stream which we get by calling getInputStream() on the same URLConnection object.We assume there will only be …