I used to be studying Santosh Sir’s article on utility controller sample final night time. Those that have some understanding on J2EE design sample know the completely different between Entrance Controller and Utility controller sample. Struts 1.x is a greatest instance of each.
You may learn the Santosh Sir’s article on Front Controller and Application controller sample.
So Right this moment I’ve determined to implement utility controller design sample which may use java’s properties file to learn and configure controllers.
Begin with Controller
interface.
1
2
3
4
5
6
7
8
9
10
|
package deal org.techzoo.aqsa;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
public View execute(HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
|
View.java is a wrapper view resolver return from Controller’s execute technique.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package deal org.techzoo.aqsa;
import java.util.Map;
public class View {
non-public Map mannequin;
non-public String ahead;
public View(String ahead) {
this.setForward(ahead);
}
public View(String ahead, Map knowledge) {
this.setForward(ahead);
if (knowledge != null) {
this.mannequin = knowledge;
}
}
public void clear() {
this.mannequin = null;
}
public Map getModel() {
return mannequin;
}
public void setModel(Map mannequin) {
this.mannequin = mannequin;
}
public String getForward() {
return ahead;
}
public void setForward(String ahead) {
this.ahead = ahead;
}
}
|
ControllerConfig
is used to carry controller’s motion with it’s class identify.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package deal org.techzoo.aqsa.config;
import static java.lang.Class.forName;
import org.techzoo.aqsa.Controller;
public class ControllerConfig {
non-public String motion;
non-public String controllerClass;
public ControllerConfig(String motion, String controllerClass) {
tremendous();
this.motion = motion;
this.controllerClass = controllerClass;
}
public Controller invokeController() {
Controller controller = null;
if (controllerClass != null && !controllerClass.isEmpty()) {
attempt {
controller = (Controller)forName(controllerClass).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
return controller;
}
public void setAction(String motion) {
this.motion = motion;
}
public void setControllerClass(String controllerClass) {
this.controllerClass = controllerClass;
}
public String getAction() {
return motion;
}
public String getControllerClass() {
return controllerClass;
}
}
|
WebConfig
is used to handle all controller configured in properties file. This class will learn, and make obtainable all controller Object to Most important Servlet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package deal org.techzoo.aqsa.config;
import static java.lang.String.format;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public last class WebConfig {
non-public static last String MAPPING = “mappings.properties”;
non-public Map<String, ControllerConfig> controllers = null;
public WebConfig(String mappingFile) {
File mapping = new File(mappingFile);
System.out.println(mapping.getPath());
if (mapping.isFile() && mapping.exists()) {
String xmlFileName = mapping.getName();
if (!xmlFileName.equals(MAPPING))
throw new UnsupportedOperationException(format(
“mapping file identify ought to be %s.”, MAPPING));
controllers = new HashMap<String, ControllerConfig>();
readMappingFile(mapping);
} else {
System.err.println(“Not a Mapping file or not exist..”
+ mapping.getPath());
}
}
non-public void readMappingFile(File propFile) {
Properties props = new Properties();
attempt {
props.load(new FileInputStream(propFile));
Set keys = props.keySet();
Iterator it = keys.iterator();
whereas(it.hasNext()){
String key = (String)it.subsequent();
String className = (String)props.get(key);
ControllerConfig cc = new ControllerConfig(key, className);
addControllerConfig(key, cc);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
non-public void addControllerConfig(String actionUrl,
ControllerConfig controllerConf)
{
if (!controllers.containsKey(actionUrl)) {
controllers.put(actionUrl, controllerConf);
} else throw new UnsupportedOperationException(
format(“Motion %s is exist already. “
+ “Two controller cannot map to at least one motion.”,
actionUrl));
}
public Map<String, ControllerConfig> getControllers() {
return controllers;
}
}
|
And Final is our Entrance Controller servlet (AqsaServlet
). Aqsa Servlet is a Centralized management in utility, a sigle entry level which handle Protocol Dealing with, Request Navigation and View Dispatching.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package deal org.techzoo.aqsa;
import static java.lang.String.format;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.techzoo.aqsa.config.ControllerConfig;
import org.techzoo.aqsa.config.WebConfig;
last public class AqsaServlet extends HttpServlet {
non-public static last lengthy serialVersionUID = 1L;
non-public static last String MAPPING_FILE = “mapping”;
non-public ServletContext context;
non-public WebConfig webConfig = null;
public void init(ServletConfig config) throws ServletException {
tremendous.init(config);
this.context = config.getServletContext();
String mappingFile = null;
String controllersProps = config.getInitParameter(MAPPING_FILE);
mappingFile = context.getRealPath(controllersProps);
webConfig = new WebConfig(mappingFile);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}
non-public void doProcess(HttpServletRequest request,
HttpServletResponse response) {
last String servletPath = request.getServletPath();
last String actionPath = servletPath.substring(1,servletPath.lastIndexOf(“.”));
last Map controllers = webConfig.getControllers();
last ControllerConfig ctrConfig = controllers.get(actionPath);
View view = null;
if (ctrConfig != null) {
String actionUrl = ctrConfig.getAction();
if (actionPath.equals(actionUrl)) {
attempt {
last Controller controller = ctrConfig.invokeController();
view = controller.execute(request, response);
prepareModelData(request, view);
} catch (Exception e) {
System.out.println(e.getMessage());
}
dispatchRequestToView(view, webConfig, request, response);
}
} else
throw new UnsupportedOperationException(format(
“motion %s isn’t present in mapping file.”, actionPath)
);
}
non-public void dispatchRequestToView(View view, WebConfig webConfig,
HttpServletRequest request, HttpServletResponse response) {
attempt {
String ahead = view.getForward();
last RequestDispatcher rd = context.getRequestDispatcher(ahead);
rd.ahead(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
non-public void prepareModelData(HttpServletRequest request, View view) {
last Map mannequin = view.getModel();
if (mannequin != null) {
for (Entry knowledge : mannequin.entrySet()) {
request.setAttribute(knowledge.getKey(), knowledge.getValue());
}
}
}
}
|
net.xml and controller config properties file will Look comparable under…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
xml model=“1.0” encoding=“UTF-8”?>
<net–app id=“WebApp_ID” model=“2.5”>
<show–identify>AqsaFramework</show–identify>
<servlet>
<servlet–identify>motion</servlet–identify>
<servlet–class>org.techzoo.aqsa.AqsaServlet</servlet–class>
<init–param>
<param–identify>mapping</param–identify>
<param–worth>/WEB–INF/mappings.properties</param–worth>
</init–param>
<load–on–startup>1</load–on–startup>
</servlet>
<servlet–mapping>
<servlet–identify>motion</servlet–identify>
<url–sample>*.zoo</url–sample>
</servlet–mapping>
<welcome–file–checklist>
<welcome–file>index.jsp</welcome–file>
</welcome–file–checklist>
</net–app>
|
and ….
#Mapping file include key worth pair
login=com.xpert.controller.LoginController
register=com.xpert.controller.RegistrationController
Now it’s time to create a easy controller, and examine to check…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package deal com.xpert.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.techzoo.aqsa.Controller;
import org.techzoo.aqsa.View;
public class LoginController implements Controller{
@Override
public View execute(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new View(“/LoginUser.jsp”);
}
}
|
Supply techzoo.org