Calling Servlet from Servlets in Java

I gathered information from environment and sharing you this information with all.
When Ever you want to call any servlet from another servlet We can use two ways:-

  • A servlet can make an HTTP request of another servlet. Opening a connection to a URL
  • A servlet can call another servlet’s public methods directly, if the two servlets run within the same server.

I will let you know the second way to calling the servlet. To call another servlet’s public methods directly, you must:

  • You Should know the name of servlet that you want to call.
  • Acquire access to that servlet’s Servlet object
  • Calling the servlet’s public method

To get the object of servlet, use the ServletContext object’s getServlet method. Get the ServletContext object from the ServletConfig object stored in the Servlet object. An example should make this clear. When the EmployeeDetail servlet calls the BookDB servlet, the EmployeeDetail servlet obtains the EmployeeDB servlet’s Servlet object like this:

Once you have the servlet object, you can call any of that servlet’s public methods. For example, the EmployeeDetail servlet calls the EmployeeDB servlet’s get getEmployeeDetail method:

You Should take care of the few things.If your servlet is following the singlethreadedModel interface then your call violate that single threaded model. Then you should implement the first way..

public class EmployeeDetail extends HttpServlet {

public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
...
EmployeeDBdatabase = (EmployeeDB)
getServletConfig().getServletContext().getServlet(employeedB);
EmployeeDetail bd = database.getEmployeeDetails(empId);
...
}
}

Should Static Final Logger be declared in upper case ?

People always define Logger in upper case for not having PMD warning

According to SUN code convention only constants should be defined in upper case.

Every thing that you define as static and final doesn’t not become constant, but it should immutable too.
Logger are not immutable like other primitive types e.g number and String,  Logger are not constant and they should
not be treated like that.
Therefor it should be defined in lower case.

I know now PMD and other code checker will give warning on this, but i think We should be fix the rule, not code for these type of warning.

Adapter Design Pattern in Java

Adapter Design pattern – Structural Patterns

Adapter pattern Convert the existing interfaces to a new interface to achieve compatibility and re usability of the unrelated classes in one application. Also known as Wrapper pattern. An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface. The adapter translates calls to its interface into calls to the original interface, and the amount of code necessary to do this is typically small. The adapter is also responsible for transforming data into appropriate forms. . When a client specifies its requirements in an interface, you can usually create a new class that implements the interface and subclasses an existing class. This approach creates a class adapter that translates a client’s calls into calls to the existing class’s methods.

The Adapter in the Real World
The concept of an Adapter is as the name suggest. It will do what is expected It adapts one object to that of another. Suppose we have any device which will work on 10 Ampere. But we don’t have plug of 10 ampere, then we need an Adapter to work that device .In similar way this design pattern works.

The Adapter in the Software
Here also it is working as same.. When you want to work or use one class to another class that was not designed to fit together, we need a adapter class.

There are two ways of implementing the Adapter Pattern, either use the Inheritance or use the composition.
Let’s do it with the approach of Inheritance.Lets take an example.

PrintOutput.java

package structural.adapter.inheritance;

public interface PrintOutput {
public String getOutput();
}

SystemVinay.java

package structural.adapter.inheritance;
public class SystemVinay{
private String specification = "Hi ,This is adapter Design Pattern";
public String getInput() {
return specification;
}
}// End of class

Finally, there will be an adapter class. This will inherit the PrintOutput and give output for SystemVinay.

OutPutAdapter.java

package structural.adapter.inheritance;
/**
* OutPutAdapter has is the connector between
* the SystemVinayand PrintOutput so as to make the interface
* of one system to suit the client.
*/
public class OutPutAdapter implements PrintOutput {
/**
* Method coming from the interface
* SystemVinaywhich we have to make to
* fit the client SystemVinay
*
* @return Desired output of “Hi ,This is adapter Design Pattern”
*/
public String getOutput() {
SystemVinaysystem = new SystemVinay();
String output = system.getInput();
return output;
}
}// End of class

This class implements the getOutput() method of SystemVinayand sets it to fit the client output.

In- Summary We can say Adapter design pattern is – A class extends another class, takes in an object, and makes the taken object behave like the extended class.

pimp it