Web tier when a user inputs http: // localhost: 8080 / petstore / customer.do, MainServlet receiving the Request, to doProcess () function: private void doProcess (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {// set the locale of the user to default if not set if (. request.getSession () getAttribute (WebKeys.LOCALE) == null) {request.getSession () setAttribute (WebKeys.LOCALE, defaultLocale);.} try {getRequestProcessor () .processRequest (request); // transduction operation (forward) getScreenFlowManager () forwardToNextScreen (request, response);.} catch (Throwable ex) {String className = ex.getClass () getName ();. String nextScreen = getScreenFlowManager () .gextExceptionscreen (ex); // put the exception in The Request Request.setttribute ("javax.servlet.jsp.jspexception", ex); if (nextscreen == null) {// send to General Error Screen EX. PRINTSTACKTRACE (); throw new servletException ("MainServlet: unknown exception: " className);} context.getRequestDispatcher (nextScreen) .forward (request, response);}} turn Petstore_home / src / waf / src / controller / com / sun / j2ee / blueprints / waf / controller / web /flow/ScreenFlowManager.java, can be found at about 112 forwardToNextScreen () function: public void forwardToNextScreen (HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, FlowHandlerException, javax.servlet.ServletException {// set the presious screen String Fullurl = Request.getRequesturi (); // Get the screen name string selectedURL = defaultscreen; int lastpathsepare = fullurl.lastindexof ("/")
1; if (lastpathsepaare! = -1) {selectedURL = fullurl.substring (lastpathseparetor, fullurl.Length ());} // Please join the reconnaissance code, in this example, SELECTEDURL = Customer.do system.out.println ( "selectURL =" selectedURL); String currentScreen = ""; uRLMapping urlMapping = getURLMapping (selectedURL); if (urlMapping = null!) {if {currentScreen = urlMapping.getScreen () (urlMapping.useFlowHandler ()!); / / Join surveillance code, the present embodiment is, currentScreen = customer.screen System.out.println ( "currentScreen =" currentScreen);} else {// load the flow handler FlowHandler handler = null; String flowHandlerString = urlMapping. getFlowHandler (); try {handler = (FlowHandler) getClass () getClassLoader () .loadClass (flowHandlerString) .newInstance ();. // invoke the processFlow (HttpServletRequest) handler.doStart (reque st); String flowResult = handler.processFlow (request); handler.doEnd (request); currentScreen = urlMapping.getResultScreen (flowResult); // if there were no screens by the id then assume that the result was // the screen itself If (currentscreen == null) currentscreen = flowresult;} catch (Exception ex) {system.err.println ("ScreenflowManager Caught Loading Handler:" EX);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
null) {System.err.println ( "ScreenFlowManager: Screen not found for" selectedURL); throw new RuntimeException ( "Screen not found for" selectedURL);} // transduced operation (forward) System.out.println ("forward to" currentscreen; context.getRequestDispatcher ("/" / "/" / "/" / "/" / "/" / "/"} we know that it will transduction to Customer.Screen, from ScreenDefinitions_en_us.xml, you can find the corresponding picture area Block: screen> Most of them are the same as main.screen, the only difference part is the main block (body), open Customer.jsp 它, Location in PetStore_Home / Src / Apps / PetStore / Src / Docroot, at about 50 columns:
Contact Information P> td> Tr>
first name b> td>
< C: out value = "$ {Customer.account.contactinfo.givenname}" /> td> tr>
Below is omitted ... The above program code is displayed to display the user name, which uses JSTL Tags to print it, but the problem is coming, how is the information? The answer is one of the four invisible roles mentioned in front of this article - Signonnotifier Attribute Listener.
Signonnotifier, please recall the front. When the user identity is successful, Signonfilter will set the sign: hREQ.GetSession (). Setttribute (signed_on_user, new boolean (TRUE) At this time, SignonNotifier will be triggered, please open: petstore_home / src / apps / petstore / src / com / suline / controller / Web / Signonnotifier.java, at about 89 columns: / ** * Process AN after the new attribute added * attribute * / public void attributeAdded (HttpSessionBindingEvent se) {processEvent (se);} / ** * Process the update * rear cover attribute * / public void attributeReplaced (HttpSessionBindingEvent se) {processEvent (se);} private void processEvent (HttpSessionBindingEvent se) {HttpSession session = se.getSession (); String name = se.getName (); / * check if the value matches the signon attribute * if a macth fire off an event to the ejb tier that the User * HAS SIGNED On and Load The Account for the user * / / / / {The new or overlay attribute is processed for "SIGNED_ON_USER", otherwise IF (Name.Equals (Signonfilter.Signed_on_user) {boolean asignon = (((( Boolean) SE.GetValue ()). BooleanValue (); if (asignon) { String userName = (String) session.getAttribute (SignOnFilter.USER_NAME); // Join reconnaissance code System.out.println ( "SignOnNotifier () userName =" userName); // look up the model manager and webclient controller PetstoreComponentManager sl = (PetstoreComponentManager) session.getAttribute (PetstoreKeys.COMPONENT_MANAGER); WebController wc = sl.getWebController (session); SignOnEvent soe = new SignOnEvent (userName); // set the EJBAction on the Event EventMapping em = getEventMapping (session.getServletContext () , SOE); if (em! =
NULL) {soe.sejbActionclassName (em.getejbactionclassname ()); system.out.println ("ejbactionclassname =" em.getejbactionclassname ());} Try {// Update information is used, in this case is to be read, there is no effect wc.handleEvent (soe, session);} catch (EventException e) {System.err.println ( "SignOnNotifier Error handling event" e);} // made Customer EJB Local Interface Reference CustomerLocal customer = sl.getCustomer (session); // ensure the customer object is put in the session // into the Customer EJB Local Interface Session if (session.getAttribute (PetstoreKeys.CUSTOMER) == null) {session.setAttribute (PetstoreKeys. CUSTOMER, customer);} // set the language to the preferred language and other preferences ProfileLocal profile = sl.getCustomer (session) .getProfile (); Locale locale = I18nUtil.getLocaleFromString (profile.getPreferredLanguage ()); session. setAttribute (PetstoreKeys.LOCALE, locale);}}} The following slightly ... SignOnNotifier ShoppingClientFacade reference will be made through PetstoreComponentManager, ShoppingClientFacade is Petstore agent interface in the EJB tier, Web tier through it all Request required to access required According to information, the benefit of this Design Pattern is to reduce the coercion between EJB Tier and Web Tier, and control the business logic to control in EJB Tier.
PetstoreComponentManager, source code D: /petstore1.3.1/src/apps/petstore/src/com/sun/j2ee/blueprints/petstore/controller/web/ PetstoreComponentManager.java, from about 110: public CustomerLocal getCustomer (HttpSession session) {ShoppingControllerLocal scEjb = getShoppingController (session); try {// obtain ShoppingClientFacade reference ShoppingClientFacadeLocal scf = scEjb.getShoppingClientFacade (); //scf.setUserId(userId); // obtain CustomerLocal reference return scf.getCustomer ();} catch (FinderException e) {System.err.println ( "PetstoreComponentManager finder error:" e);} catch (Exception e) {System.err.println ( "PetstoreComponentManager error:" e);} return null;} ShoppingClientFacadeLocalEJB as Session Bean, source In D: /PETSTORE 1.3.1/src/apps/petstore/src/com/sun/j2ee/blueprints/petstore/controller/ejb/ ShoppingClientFacadelocalejb.java, about 101 columns: / ** Asume That The Customer Userid Has Been set * / public customerlocal getcustomer () throws FinderException {// please join the reconnaissance code system .out.println ( "ShoppingClientFacadeLocalEJB.getCustomer ()"); if (userId == null) {throw new GeneralFailureException ( "ShoppingClientFacade: failed to look up name of customer: userId is not set");} try {ServiceLocator sl = new ServiceLocator (); CustomerLocalHome home = (CustomerLocalHome) sl.getLocalHome (JNDINames.CUSTOMER_EJBHOME); customer = home.findByPrimaryKey (userId);} catch (ServiceLocatorException slx) {throw new GeneralFailureException ( "ShoppingClientFacade: failed to look up name of customer : caught " slx);} Return Customer;
} CustomerEJB for the Entity Bean, it has a one to one relationship with AccountEJB, AccountEJB also have one to one relationship with ContactInfoEJB, the three are all Entity Bean, directly correspond to the database table CustomerEJBTable, AccountEJBTable, ContactInfoEJBTable, source code D: /petstore1.3.1 / src / components / customer / src / com / sun / j2ee / blueprints / customer / directory, so no need for Entity Beans, no longer explain. Figure 24 Entity Beans Relationships So
13. Accountejb Entity Bean has a one-on-one relationship with ContactInfoEJB Entity Bean, and ContactInfoeJB represents the ContactInfoeJbTable data sheet. 14. Signonfilter After the 7th step is verified, REQUEST (Customer.do) is released. 15.Request (Customer.do) is received by MainServlet, and the mainservlet is responsible for processing * .do's request. 16.MAINSERVLET reads mappings.xml related settings, finds the Web Action Class corresponding to Customer.do (in Web Tier wants to perform, in this case, only browsing, so there is no corresponding work) and screen Render screen). 17. MAINSERVLET handed ScreenFlowManager to ScreenflowManager, which is responsible for the transfer. 18. The corresponding value of Screen is Customer.Screen, so transduction to Customer.Screen. Writing here The author is full! The first time I wrote such a long article, but I still have to fight my spirit to be an endding. I will discuss the PetStore architecture by these three phases. A complete J2EE Framework is already shaped, and most of the technologies mentioned in J2EE are almost used. It also saw how various technologies should be integrated. This is the so-called Design Pattern, and maybe the reader will think that such a schema can be used directly on the project we actually develop? Of course, it is possible, but in the architecture of the web tier has more complete, more set of Framework appears, is Struts, the URL in http://jakarta.apache.org/struts/index.html, it is a one under apache jakarta Sub-project (Jakarta's most famous child project is Tomcat), a free and continuous upgrade Framework, is currently a red fried chicken, its entire architecture is similar to PetStore, just it ranked only in Web Tier, the author suggests that Struts can be used To develop our projects, when we have learned the architecture of PetStore, learn Struts must have a half-level effort, and can make up the Struts Lack of Framework in EJB Tier. If the reader has any questions or opinions, please discuss with the author, e-mail: Senshaw@ms4.hinet.net. Note 1: PetStore_home represents your PetStore installation directory. Note 2: DEPLOYTOOL ON mode and use the Pestore.ear to load deploytool, please refer to the first article of this series. Note 3: The DAO generation process is similar to the second CatalogDAO of this series, and the author will not repeat it. Note 4: Readers If you want to observe the relevant information table in the Cloudscape database, the author provides a method to take advantage of JBuilder Database Pilot to observe: a. Please open JBuilder, click "Tools"> "Enterprise Setup" in Menu Bar, Please select the "Database Drivers" page, press the "Add" button, add a Library to the Cloudscape related JAR file, the location in the J2EE installation directory / lib / cloudscape, then select "OK" button, JBuilder You will ask you to reactivate jbuiler and close JBuilder.