In the previous blog article "deep learning Struts Framework (I): a simple example of MVC pattern code", I briefly described the differences between the implementation of MVC pattern code and the MVC pattern and the three-tier architecture, and leave a question: how to remove the IF-else statement block from testservlet.
Because the IF-else statement block appears in testservlet, it makes the program no longer flexible and makes it clumsy to cope with changes in demand. Therefore, we will undertake the previous article to reconstruct the testservlet code, mainly by using the inherited peptide to further reconstruct the testservlet. Although this article does not completely remove the IF-Else, it is more flexible than the code in the previous article, and it also paves the way for the next article to completely remove the IF-Else.
The reconstruction phase is as follows:
Let's take a look at the testservlet code of the previous article:
Package COM. cjq. servlet; </P> <p> Import Java. io. ioexception; <br/> Import Java. util. list; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> public class testservlet extends httpservlet {</P> <p> protected void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {</P> <p> string requesturi = request. getrequesturi (); <br/> system. out. println ("request =" + requesturi); <br/> string Path = requesturi. substring (requesturi. indexof ("/", 1), requesturi. indexof (". "); <br/> system. out. println ("Path =" + path); </P> <p> string username = request. getparameter ("username"); <br/> usermanager = new usermanager (); <br/> // usermanager. adduser (username); <br/> string forward = ""; <br/> If ("/servlet/deluser ". equals (PATH) {<br/> usermanager. deluser (username); <br/> forward = "/del_success.jsp"; <br/>} else if ("/servlet/adduser ". equals (PATH) {<br/> usermanager. adduser (username); <br/> forward = "/add_success.jsp"; <br/>} else if ("/servlet/modifyuser ". equals (PATH) {<br/> usermanager. modifyuser (username); <br/> forward = "/modify_success.jsp"; <br/>} else if ("/servlet/queryuser ". equals (PATH) {<br/> List userlist = usermanager. queryuser (username); <br/> request. setattribute ("userlist", userlist); <br/> forward = "/query_success.jsp "; <br/>}else {<br/> throw new runtimeexception ("request failed"); <br/>}< br/> request. getrequestdispatcher (forward ). forward (request, response); <br/>}< br/>}
First, we can see that a value is assigned to forward in each statement block, that is, the page Jump path is assigned, and the jump path is assigned for each request path judgment. In addition, each if-else statement block has business processing. We need to put these business processing in the class separately, so that the responsibility is more uniform, which is more in line with the object-oriented thinking.
From here we start to refactor. We can encapsulate the jump path and business logic.
Since it is encapsulated, We can abstract an excuse to mainly complete a method. The main function of this method is to complete the business logic encapsulation and the return of the path jump. Then four classes are created to implement the corresponding jump path return after adding, deleting, modifying, and querying the business processing and processing.
The Code is as follows:
Interface action:
Package COM. cjq. servlet; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Public interface action {</P> <p> Public String execute (httpservletrequest request, httpservletresponse response) <br/> throws exception; <br/>}
Add, delete, modify, and query implementation classes:
Add User implementation class:
<PRE name = "code" class = "Java"> package COM. cjq. servlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class adduseraction implements action {Public String execute (httpservletrequest request, httpservletresponse response) throws exception {string username = request. getparameter ("username"); usermanager = new usermanager (); usermanager. adduser (username); Return "/add_success.jsp" ;}}</PRE> <p>
Delete a user implementation class:
Package COM. cjq. servlet; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> public class deluseraction implements action {</P> <p> Public String execute (httpservletrequest request, <br/> httpservletresponse response) throws exception {<br/> string username = request. getparameter ("username"); <br/> usermanager = new usermanager (); <br/> usermanager. deluser (username); <br/> return "/del_success.jsp"; <br/>}</P> <p>}
Update user implementation class:
Package COM. cjq. servlet; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> public class modifyuseraction implements action {</P> <p> @ override <br/> Public String execute (httpservletrequest request, <br/> httpservletresponse response) throws exception {<br/> string username = request. getparameter ("username"); <br/> usermanager = new usermanager (); <br/> usermanager. modifyuser (username); <br/> return "/modify_success.jsp"; <br/>}</P> <p>}
Query user implementation class:
Package COM. cjq. servlet; </P> <p> Import Java. util. list; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> public class queryuseraction implements action {</P> <p> @ override <br/> Public String execute (httpservletrequest request, <br/> httpservletresponse response) throws exception {<br/> string username = request. getparameter ("username"); <br/> usermanager = new usermanager (); <br/> List userlist = usermanager. queryuser (username); <br/> request. setattribute ("userlist", userlist); <br/> return "/query_success.jsp"; <br/>}</P> <p>}
The reconstruction of the testservlet class is as follows:
Package COM. cjq. servlet; </P> <p> Import Java. io. ioexception; <br/> Import Java. util. list; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> public class testservlet extends httpservlet {</P> <p> protected void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {</P> <p> string requesturi = request. getrequesturi (); <br/> system. out. println ("request =" + requesturi); <br/> string Path = requesturi. substring (requesturi. indexof ("/", 1), requesturi. indexof (". "); <br/> system. out. println ("Path =" + path); </P> <p> action Action = NULL; <br/> If ("/servlet/deluser ". equals (PATH) {<br/> action = new deluseraction (); <br/>}else if ("/servlet/adduser ". equals (PATH) {<br/> action = new adduseraction (); <br/>}else if ("/servlet/modifyuser ". equals (PATH) {<br/> action = new modifyuseraction (); <br/>} else if ("/servlet/queryuser ". equals (PATH) {<br/> action = new queryuseraction (); <br/>}else {<br/> throw new runtimeexception ("request failed "); <br/>}< br/> string forward = NULL; <br/> try {<br/> forward?action.exe cute (request, response ); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> request. getrequestdispatcher (forward ). forward (request, response); <br/>}< br/>}
Running result:
In this way, although the testservlet class does not completely remove the IF-Else, the Code becomes more concise, and the service logic processing and path jump return are implemented using the peptide. Clearer responsibilities make maintenance easier.
Issues left behind:
If-else statement blocks are not completely removed, and too many strings still appear in the program, so the program is still not flexible, and too many strings increase the debugging complexity. Therefore, the next article removes the IF-else statement block and places the string in the configuration file for dynamic reading using dom4j.
We are getting closer and closer to the Struts framework. When we remove the IF-Else and string, the prototype of the Struts framework will appear. We look forward to the next article.