A small web project implementation method discussion Nova http://www.jdon.com Oct 14, 2004 10:51 AM Reply Recently, it is recently maintained for someone else 's web projects. Seeing this implementation: 1. Only one Controller's servlet class 2. A Service interface 3. Some instances of the class Controller class that implements the service interface is responsible for controlling, dynamically generating business logic classes instance (all classes need to implement the service interface), then pass HttpServletRequest.setttribute ("UserList" , userList); assigned to the WEB terminal, specific reference may be portions of 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 insta ? Nce Class serviceClass = Class.forName (serviceName); Service service = (Service) serviceClass.newInstance (); service.execute (request, response, servletcontext);} catch (ClassNotFoundException classnotfoundexception) {throw new ServletException (classnotfoundexception.getMessage ( ));} catch (IllegalAccessException 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;} implement a service class (service class corresponding to) 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);}} JSP page file < ?% String userName = (String) request.getAttribute ( "USERNAME"); List userList = (List) request.getAttribute ( "USERLIST"); when%> visit: /service.Controller service = StartService & target = / StartPage.jsp I want to know now 1. How is this implementation? Why do you do this, what is the benefit 2. Is there any need to save more than HashMap to avoid more objects // Todo: Hashmap to reduction the generated instance? Class serviceclass = class.Forname (serviceName); service Service = (service) serviceclass.newinstance (); service.execute (request, response, servletContext); 3. Do you have a good similar solution (just for small web projects, don't say it with Framework) Thank you! Re: A small web project implementation method discussion Time: OCT 19, 2004 9:51 AM