JavaScript
  Home arrow JavaScript arrow Page 4 - J2EE Design Patterns: Getting Started
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

J2EE Design Patterns: Getting Started
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2006-09-11

    Table of Contents:
  • J2EE Design Patterns: Getting Started
  • Common Design Patterns at a Glance
  • J2EE Design Patterns in the Real World
  • View component and DTO

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    J2EE Design Patterns: Getting Started - View component and DTO


    (Page 4 of 4 )

    The View component calls the controller component which in turn uses the DAO implementation to get the connection. Then it creates a DTO and passes it to the next View Component if the username and password are correct.

    package loginmodule;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import loginmodule.dao.LoginDAO;
    import loginmodule.dto.LoginDTO;
    public class Login extends HttpServlet {
        private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
       private LoginDAO connection;   

       private ConfigService configService;
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
           try {
             connection=new DBConnection();
                   }
           catch (Exception e) {
            e.printStackTrace();//for now just display the exception   
           }
        }
        public void doGet(HttpServletRequest request, 
                          HttpServletResponse response) throws ServletException,
    IOException {response.setContentType(CONTENT_TYPE);
            PrintWriter out = response.getWriter();
            String flush=null;
            HttpSession session=request.getSession();       

           //implementation of the service class not shown for brevity
            service=new DataService(connection.getStatement());       

            String user=request.getParameter("user_txt");
            String passwd=request.getParameter("password_txt");       

    //check user’s credential and if its ok create a dto and sent to the next
    view
            if(isValidUser(user,passwd)) {           

    //service class takes the user name, populates the dto and
    returns //the dto
                  session.setAttribute("USER",service.getDetails(user));
               

                RequestDispatcher dispatcher=request.getRequestDispatcher
    (“/welcome.jsp”));
                dispactcher.forward(request,response);
            }
            else {
                flushSession(session);//invalidate the crated session on invalid
    login
                RequestDispatcher dispactcher;
                dispactcher = request.getRequestDispatcher
    (configService.getPage(ConfigService.PAGE_ERROR));
                dispactcher.forward(request,response);
            }       

            out.close();
        }
        public void doPost(HttpServletRequest request, 
                          HttpServletResponse response) throws ServletException,
    IOException {
                              doGet(request,response);
                          }   

        private void flushSession(HttpSession session) {
            session.invalidate();
        }   

        public void destroy() {
        connection.close();       
        }
    }

    And here's the DTO.

    package loginmodule.dto;
    public class LoginDTO
    {
      String user;
      String favColor; 

      public LoginDTO()
      {
      }
    //the constructor called by service class to populate the dto
     public LoginDTO(String user, String favColor)
      {
        this.user=user;
        this.favColor=favColor;
      }
      public void setUser(String user)
      {
        this.user = user;
      }
      public String getUser()
      {
        return user;
      }
      public void setFavColor(String favColor)
      {
        this.favColor = favColor;
      }
      public String getFavColor()
      {
        return favColor;
      }
    }

    That’s it. In this part I have given a glimpse of what Design Patterns are and what can be achieved by implementing design patterns in context of J2EE. From the next part onwards I will focus on each Design Pattern available for each of the tiers. Till then…


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Design Patterns are standard answers to recurring problems. In this discussion I...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek