>

Wednesday, January 16, 2013

Simple Application Use Servlet Part II Creating Baseline

Simple Application Use Servlet Part II
Creating Baseline

we will create the baseline for our simple application as we mentioned in last post, we just using one servlet for centralizing the request and response, as we know that our application not just, one request, but more. To meet this condition.
the step i chose:
  • we stay have one servlet
  • we have a lot url pattern servlet but stay one servlet and one name servlet
  • on the method doPost and doGet we instantiate only one object i will be named it CallingAction,
  • in callingAction we just call one method and pass parameter, userpath (request path/ url pattern)

    shabby note - simple application
    Baseline for Simple Application


why we create CallingAction, the idea is every time user do a request we provide by specific url pattern for each different request type, in the servlet class we can retrieve request path(url pattern name) that user hit. For handling the right method for the right url path. I chose step as describe bellow:

  • create labels for each name url pattern, method for the right request and also the class belong to the method
  • each specific url pattern has method, and has class belong to method, have the same labels name
  • based on labels that we created, by using java reflection for picking the right object and method for specific url pattern in CallingAction

For storing labels, I will use interface and grouping for each url path, method and the class belong to method, I will use interface too. The labels will have value as points bellow:
url path → url pattern name
method → method name
class →package_name.class_name

For storing data, I used Hashmap in singleton method. Every time user create user name or edited will be put in the hashmap .

Monday, January 14, 2013

Simple Application Use Servlet Part I

Simple Application Use Servlet Part I

Servlet is a java class can accept request from another java class, web client or other servlet, which can trigger the response, Sometime servlet is named HTTP Servlet because servlet were used combine with HTTP, but servlet itself not one of client server protocol specification.

We can make web application in java use servlet,  in web application there are many request-response process and make a lot servlet then make it our application is hard to maintain, web application requires a centralized access point for HTTP request handling to support the integration of system services like security, data validation etc, content retrieval, view management, and dispatching. When the user accesses the view directly without going through a centralized mechanism, few problems may occur :

  • Each view is required to provide its own system services often resulting in duplicate code.
  • View navigation is left to the views. This may result in shared code for view content and view navigation.
  • Distributed control is more difficult to maintain, since changes will often need to be made in numerous places.
Generally we write specific servlets for specific request handling. These servlets are responsible for data validation, error handling, invoking business services and finally forwarding the request to a specific JSP view to display the results to the user.

According that we better only have one Servlet (instead of having specific Servlet for each specific request) centralizing the handling of all the requests and delegating the functions like validation, invoking business services etc to a command or a helper component. The benefit that we have only one servlet are :
  • Avoid duplicating the control logic like security check, flow control etc.
  • Apply the common logic, which is shared by multiple requests in one controller.
  • Separate the system processing logic from the view processing logic.
  • Provides a controlled and centralized access point for your system.
As I describe about let’s gather requirement for making simple application using servlet.
Tool :
  • java JDK 1.6 or above because all servlet management such as naming, mapping or url pattern, initial params, because we can use annotation. I used Java JDK 1.7.
  • server I used tomcat 7
  • editor Eclipse Indigo
  • for storing data, I will use Collection class, map and stay in JVM in singleton state
Application process constraint:
  • user can log in to application and can edit his/ her profile after save the page will be directed to login form. For checking that the profile has been change user just login again and she/ he will see the profile and edit again.
  • If user not register yet, can go to page sign up after finish entry all data and submit user will be directed to login form and For checking that the profile has been change user just login again and she/ he will see the profile and edit again.

Tuesday, January 1, 2013

JAVA REFLECTION

JAVA REFLECTION

Java reflection is a class in java from package java.lang.reflect we can get all the properties and explore from other classes such as its constructors, methods, and fields. let’s try.
First we create super class 

abstract public class Person {
   abstract void yourName();
}

and then we create the class which is extends Person Class

public class Myself extends Person{
   @Override
   void yourName() {
      System.out.println("my name is Iman");
   }
}


now we create demo class how java reflection work, in our demo class we just try to retrieve all the methods in a class and its super class .
One thing that we should aware that when to call the name of class when using java reflection Class for “Class”, if we didn’t put our java file in package we can invoke Class.forName(“class_name”) but if we put our class in package Class.forName(“package_name.class_name”)


import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionDemo {

   public static void main(String[] args) {
     try {
     Class demo = Class.forName("demo.Myself");
     // this for retrieving method name
     Method method[] =demo.getDeclaredMethods();
     for (int i = 0; i < method.length; i++) {
       System.out.println(" the method name is  --"
        +method[i].toString());
     }

    // retrieving super class from Myself Class
    Class superClass = demo.getSuperclass();
    System.out.println("Name of super class for Myself is --" 
    +superClass.getName());

    // this for retrieving method name in super class
    Method superMethods[] = superClass.getDeclaredMethods();
    for (int i = 0; i < superMethods.length; i++) {
     System.out.println(" the method name is -- "   
        +superMethods[i].toString());
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
  }
}

the console will show bellow :


the method name is -- void demo.Myself.yourName()
Name of super class for Myself is -- demo.Person
the method name is -- abstract void demo.Person.yourName()



java reflection can retrieve all methods or fields even in private mode on, one unique things that we can access private method using reflection, let’s try how to access the private method that in real OOP we can’t access.
in MySelf class we add method :

private String sayHello(String hello){
    return hello;
}

and in DemoReflection class we add few line code for accessing private method




// try to access private method
Myself myself = new Myself();
// get the method which is private
Method privateMethod = demo.Myself.class.getDeclaredMethod("sayHello", String.class);
// this the key for making us access the private method
privateMethod.setAccessible(true);
String returnValue = (String)privateMethod.invoke(myself, "hello...guys!!!");
System.out.println("AHHA !!!!! we got the method .... "+returnValue);

if we run DemoReflection the console will look like bellow :

the method name is -- private java.lang.String demo.Myself.sayHello(java.lang.String)
the method name is -- void demo.Myself.yourName()
Name of super class for Myself is -- demo.Person
the method name is -- abstract void demo.Person.yourName()
AHHA !!!!! we got the method .... hello...guys!!!




Now we access the private field using reflection, by giving value to private field, execute method that not private method which invoke private field.
Add code bellow to Myself

private String name;

String yourNameIs(){
   return name;
}


And then we add code to our DemoReflection to accessing private field

// get the field which is private
Field field = Myself.class.getDeclaredField("name");
// this the key let us accessing the private field
field.setAccessible(true);
field.set(myself, "iman");
System.out.println(" --- "+myself.yourNameIs());

and the console we show like bellow :

the method name is -- java.lang.String demo.Myself.yourNameIs()
Name of super class for Myself is -- demo.Person
the method name is -- abstract void demo.Person.yourName()
AHHA !!!!! we got the method .... hello...guys!!!
--- iman



as seen above we can access field private and private method, what about with OOP concept, private it means the others class cannot access fields or methods, because of java has reflection class we can access all private mode. one thing that we should note that private is intended to prevent misuse, but not security mechanis