Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOSD Using Java CBTS Framework. 11/2/04CBTS2 Servlet  A servlet is a Java program that can extends Web server’s functionality.  Servlets interact with.

Similar presentations


Presentation on theme: "OOSD Using Java CBTS Framework. 11/2/04CBTS2 Servlet  A servlet is a Java program that can extends Web server’s functionality.  Servlets interact with."— Presentation transcript:

1 OOSD Using Java CBTS Framework

2 11/2/04CBTS2 Servlet  A servlet is a Java program that can extends Web server’s functionality.  Servlets interact with a Web client in a request- response mechanism that is based on HTTP.  As a counterpart to Applet, a servlet runs in a Web container on the host of a Web server.  A servlet can invoke the appropriate logic in other Java classes to fulfill clients’ requests, and return the results to clients through the Web server.

3 11/2/04CBTS3 Processing HTTP Requests - I Browser Web Server Application Server Web container Database 1 2 2 2 3 3 4 Http Request Http Response servlets Other classes View Controller Model The real power of servlets come from their ability to serve s controllers in the MVC pattern.

4 11/2/04CBTS4 Web container Processing HTTP Requests - II Browser Web Server Application Server Database 1 2 2 3 7 10 Http Request Http Response servlets Model layer classes View Controller Model JSP can be used to further separate Java code with control logic and HTML tags. JSPs 6 3 6 4 5 5 4 8 9

5 11/2/04CBTS5 The HttpServlet API

6 Ctbs Form Login Action Servlet Login Form Login.jsp Select Exam.jsp User Mgr User Dao User Ctbs Form Select Exam Form 1.1 load 2. login 2.1 login 3. create 1. post 2.2 sql 4. redirect Login Use Case – object interaction

7 11/2/04CBTS7 Login.jsp Login Page Username <input name="username" value=' '> Password <input name="password" type="password" value=' '>

8 11/2/04CBTS8 The LoginAction Servlet public class LoginAction extends ActionServlet { public CtbsForm execute(CbtsForm prevPage) { LoginForm page = (LoginForm) prevPage; String username = page.getUsername(); String password = page.getPassword(); UserVo user = null; try { user = UserMgr.getUserInstance().login(username, password); } catch (ManagerException me) { prevPage.addError("Server Problem. Please try again!"); return prevPage; } if (user == null) { prevPage.addError("Wrong login info. Please try again!"); return prevPage; } else { //hard-coded for now String[] exams = {"Computer Ethics", "Object-Oriented Analysis & Design"}; return new SelectExamForm(exams); }

9 11/2/04CBTS9 The Base Servlet – the template public abstract class ActionServlet extends HttpServlet { private CbtsForm prevPage; private CbtsForm nextPage; public void init(ServletConfig config) throws ServletException { super.init(config); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); loadPrevPage(request); nextPage = execute(prevPage); request.getSession().setAttribute("formObj", nextPage); response.sendRedirect(getFormName(nextPage)); } public abstract BaseForm execute(BaseForm prevPage); }

10 11/2/04CBTS10 The Base Servlet – from the prev. page protected void loadPrevPage(HttpServletRequest request) { prevPage = (CtbsForm)request.getSession().getAttribute("formObj"); BeanInfo pageInfo = null; //get the page attributes by class reflection try { pageInfo = Introspector.getBeanInfo(prevPage.getClass()); }catch (IntrospectionException ise) { } PropertyDescriptor[] pds = pageInfo.getPropertyDescriptors(); try { for (int i=0; i<pds.length; i++) { Method m = pds[i].getWriteMethod(); Object[] args = {request.getParameter(pds[i].getName())}; m.invoke(prevPage, args); } } catch (Exception iae) {} }

11 11/2/04CBTS11 The Base Servlet – to the next page protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { loadPrevPage(request); nextPage = execute(prevPage); request.getSession().setAttribute("formObj", nextPage); response.sendRedirect(getPageName(nextPage)); } private String getPageName(CbtsForm form) { String name = form.getClass().getName(); java.util.StringTokenizer tok = new java.util.StringTokenizer(name, "."); for (int i=0; i<tok.countTokens()-1; i++) { System.out.println(tok.nextToken()); } String formName = tok.nextToken(); formName = formName.substring(0, formName.length()-4); return formName + “.jsp”; } }

12 11/2/04CBTS12 The Base Form – a JavaBean public abstract class CbtsForm implements java.io.Serializable { private String errorMessage = ""; /** Creates a new instance of CbtsForm */ public CbtsForm() { } public void addError(String msg) { errorMessage += msg; } public String getError() { return errorMessage; }

13 11/2/04CBTS13 The LoginForm Page Bean public class LoginForm extends CbtsForm { private String username = ""; private String password; public LoginForm() { } public LoginForm(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public String getPassword() { return password; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } }

14 11/2/04CBTS14 The User Manager public class UserMgr { private DaoFactory dbFactory = DaoFactory.getDaoFactory(); private UserMgr() { } public static UserMgr getUserInstance() { return new UserMgr(); } public UserVo login(String uname, String pwd) throws ManagerException { UserVo user = null; try { user = dbFactory.getUserDao().login(uname, pwd); } catch (DatabaseException e) { } return user; }


Download ppt "OOSD Using Java CBTS Framework. 11/2/04CBTS2 Servlet  A servlet is a Java program that can extends Web server’s functionality.  Servlets interact with."

Similar presentations


Ads by Google