150 likes | 264 Views
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.
E N D
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. • 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
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
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
The HttpServlet API CBTS
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
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
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
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
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
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
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
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
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