Created
April 19, 2012 23:00
-
-
Save rubydubee/2424756 to your computer and use it in GitHub Desktop.
Tiny MVC for J2EE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Howdy, ${user.name}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountAction implements Action | |
{ | |
@Override | |
public void process(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
//do your processing here, take values from DB, may be! | |
SomeBean user = new SomeBean("Paddy") | |
//Very important. we are setting attribute to the request and forwarding that request. | |
// forward will keep the request intact unlike redirect! | |
request.setAttribute("user", user); | |
ServletContext context = request.getSession().getServletContext(); | |
context.getRequestDispatcher("/WEB-INF/users/account.jsp").forward(request, response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Action | |
{ | |
public void process(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapp; | |
public class FrontController extends HttpServlet{ | |
private Map<String, Action> actions = new HashMap<String, Action>(); | |
public void init() | |
{ | |
this.actions.put("/account.do", new AccountAction()); | |
} | |
public void takeAction(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException | |
{ | |
String reqUri = request.getRequestURI(); | |
Action action = (Action)actions.get(reqUri); | |
action.process(request, response); | |
} | |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | |
{ | |
takeAction(request, response); | |
} | |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | |
{ | |
takeAction(request, response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<servlet> | |
<servlet-name>FrontController</servlet-name> | |
<servlet-class>com.myapp.FrontController</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>FrontController</servlet-name> | |
<url-pattern>*.do</url-pattern> | |
</servlet-mapping> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment