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. |