Skip to main content

Controller (MVC) Tips for Java Servlets / JSP

I was inspired by a face-to-face technical interview awhile ago that is why I am writing this down.


To avoid having the Servlet’s doXXX() methods clogged, use reflection by breaking down your controller code into modules. Here’s how.


You must have the following.


1. Reflection Interface (ServletHandler.java) - An interface for reflection. Nice definition!
2. Main Servlet (MainServlet.java) - A class extending HttpServlet.
3. Module Handler (CreditHandler.java) - A class containing the module’s controller code, for this example, the Credit Module.


in file ServletHandler.java,



import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface ServletHandler {
public abstract void setServlet(HttpServlet servlet);
public abstract void handle(HttpServletRequest request, HttpServletResponse response);
}

in file MainServlet.java,



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
Class HandlerClass = null;
ServletHandler handler = null;

try {
//assuming handlerClassName was defined beforehand as CreditHandler
HandlerClass = Class.forName("com.eradicus.diamondbank.controller." + handlerClassName);
} catch (ClassNotFoundException classNotFoundException) {
//some logging facility here
}
if (HandlerClass != null) {
try {
handler = (ServletHandler) HandlerClass.newInstance();
} catch (InstantiationException instantiationException) {
//some logging facility here
} catch (IllegalAccessException illegalAccessException) {
//some logging facility here
}

if (handler != null) {
//set executing servlet
handler.setServlet(this);
//switch control
handler.handle(request, response);
}
}
...
}

in file CreditHandler.java,



public class CreditHandler implements ServletHandler {
//use this if you need some attributes / methods from the executing servlet such as connection methods, application variables, etc.
private MainServlet servlet;

public void setServlet(HttpServlet servlet) {
this.servlet = (MainServlet) servlet;
}

//this is where the control comes in after invoking handler.handle() above
public void handle(HttpServletRequest request, HttpServletResponse response) {
//some good code here
}
}

There you go. I hope that helps. God bless.

Comments

Popular posts from this blog

Repair Windows 7 System Files

8 out of 10 average PC users have their box’s system files altered by malwares, viruses, etc. We usually reinstall the OS if the antivirus and anti malware software did not perform their job well. Here’s one way to fix the corrupted system files without the need of restarting your Windows 7 box. 1. Run the Command Prompt as Administrator 2. Type the following command C:\Windows\system32\> sfc /scannow 3. After the verification phase, you will receive a message about your system files’ integrity Windows Resource Protection did not find any integrity violations.

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Android Studio:Unknown Host Error

After installing Android Studio, I got the following error: Unknown host 'services.gradle.org'. Please ensure the host name is correct. If you are behind an HTTP proxy, please configure the proxy settings either in Android Studio or Gradle. Consult IDE log for more details (Help | Show Log) Solution File --> Settings --> HTTP Proxy --> Auto-detect proxy settings