Home arrow JavaScript arrow Page 4 - J2EE Design Patterns: Getting Started
JAVASCRIPT

J2EE Design Patterns: Getting Started


If you have been looking for a good overview of Design Patterns in J2EE, look no further. In this article you will learn about Design Patterns and how they interact with J2EE's tier-based development. It covers three of the most common Design Patterns, and provides a real-world example of Design Patterns in action.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 16
September 11, 2006
TABLE OF CONTENTS:
  1. · J2EE Design Patterns: Getting Started
  2. · Common Design Patterns at a Glance
  3. · J2EE Design Patterns in the Real World
  4. · View component and DTO

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials