1 / 14

OOSD Using Java

OOSD Using Java. CBTS Framework. 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.

jaunie
Download Presentation

OOSD Using Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOSD Using Java CBTS Framework

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

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

  4. Processing HTTP Requests - II Web Server Http Request Application Server 1 Web container 3 Model layer classes 2 6 servlets Browser 3 4 10 6 2 4 View Http Response 8 JSPs 7 9 5 5 JSP can be used to further separate Javacode with controllogic and HTML tags. Controller Database Model CBTS

  5. The HttpServlet API CBTS

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

  7. Login.jsp <html> <head><title>Login Page</title></head> <body> <jsp:useBean id="formObj" scope="session" class="form.LoginForm" /> <% if (formObj.getError() != null) { %> <p><%= formObj.getError() %></p> <% } %> <form method="post" action="LoginAction"> <br>Username <input name="username" value='<%= formObj.getUsername() %>'><br> <br>Password <input name="password" type="password" value='<%= formObj.getPassword() %>'><br> <br> <input type="submit" name="Submit" value="Login"> <input type="reset" value="Reset"> </form> </body> </html> CBTS

  8. 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); } } } CBTS

  9. 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); } CBTS

  10. 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) {} } CBTS

  11. 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”; } } CBTS

  12. 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; } } CBTS

  13. 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; } } CBTS

  14. 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; } } CBTS

More Related