Implementation method in a small Web project (reproduced)

xiaoxiao2021-03-06  48

Recently, it is maintained for one of the other people's web projects. Seeing this implementation: 1. Only a Controller's servlet class 2. A service interface 3. Some of the class Controller classes that implement the service interface are responsible for controlling, dynamically generating business logic classes. The instance (all classes need to implement the service interface), then assign values ​​to the web side by httpservletRequest.setttribute ("UserList"; for the web side, the specific section code: Controller class (Extends httpservlet)

protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// service name, example for packagename.ServiceName String serviceName = request.getParameter (Constant.SERVICE); if (serviceName == null) throw new ServletException ( "There is not service parameter ")! [service =?]; String targetName = request.getParameter (Constant.TARGET); // the targeted file name, example for /fileName.jsp if (targetName == null) throw new ServletException ( "There is not target parameter! [target =?]"); ServletContext servletcontext = getServletContext (); try {// TODO: hashmap to reduce the generated instance Class serviceClass = Class.forName (serviceName); Service service = (? Service) serviceClass.newInstance (); service.execute (request, response, servletcontext);} catch (ClassNotFoundException classnotfoundexception) {throw new ServletException (classnotfoundexception.getMessage ());} catch (IllegalAccess Exception illegalaccessexception) {throw new ServletException (illegalaccessexception.getMessage ());} catch (Exception exception) {throw new ServletException (exception.getMessage ());} forward (request, response, targetName);}

Service interface

public interface Service {public abstract void execute (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, ServletContext servletcontext) throws Exception;} class implements a service (corresponding to traffic class)

public class StartService implements Service {public void execute (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, ServletContext servletcontext) throws Exception {// test data List userList = new ArrayList (); httpservletrequest.setAttribute ( "USERNAME", "TestUser"); httpservletrequest.setAttribute ("UserList", UserList;}}

转载请注明原文地址:https://www.9cbs.com/read-82778.html

New Post(0)