Download private CbtsForm prevPage

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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 requestresponse 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.
11/2/04
CBTS
2
Processing HTTP Requests - I
Application Server
Web container
Http Request
1
2
Other
classes
Web
Server
2
Browser
View
3
4
Http Response
3
2
servlets
Controller
The real power of servlets
come from their ability to
serve s controllers in the
MVC pattern.
11/2/04
Model
Database
CBTS
3
Processing HTTP Requests - II
Http Request
Application Server
1
Web container
2
Browser
View
Model
layer
classes
6
servlets
Web
10 Server
2
Http Response
3
3
4
6
4
8
JSPs
9
7
5
Controller
JSP can be used to
further separate Java
code with control
logic and HTML tags.
11/2/04
5
Model
Database
CBTS
4
The HttpServlet API
11/2/04
CBTS
5
Login Use Case – object interaction
Login
.jsp
1. post
Action
Servlet
4. redirect
1.1 load
2. login
Login
Action
3. create
Select
Exam
.jsp
Ctbs
Form
Ctbs
Form
Login
Form
User
Mgr
2.1 login
User
Dao
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>
11/2/04
CBTS
7
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);
}
}
}
11/2/04
CBTS
8
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);
}
11/2/04
CBTS
9
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/2/04
CBTS
10
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”;
}
}
11/2/04
CBTS
11
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;
}
}
11/2/04
CBTS
12
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) {
public void setPassword(String password) {
}
this.username = username;
this.password = password;
}
}
}
11/2/04
CBTS
13
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;
}
}
11/2/04
CBTS
14
Related documents