Professional Documents
Culture Documents
Architecting
JSP[tm]/Servlet Application
dataBase
Commodore 1541
Capacity180k
details.jsp Cost: 200
Aligened: sometimes
[main][logoff]
footer.jsp
Component design
Page-centric: Component Page
Redirect
Servlet-centric Technology
• Servlet-centric approach:
– Loosens the coupling between the pages and
improves the abstraction between presentation
and application logic.
• Reduces JSPs for pure data display and input
collection activities.
• Eliminate for embedding presentation
information inside the servlets.
• Most of the business logic can be debugged
through the servlet before passed to
JavaBeans and JSP.
– If the flow logic that is hard-coded in the
Servlets changes often, the advantage can be
perceived than actual.
Best Practice
View
search.html
Request 1
response list.jsp
Client
Model
response
forward
JavaBeans
Request 2
Controller
find.jsp
Request 3
redirect
delete.jsp
Servlet-centric Example
View
search.html
Request 1
response list.jsp
Client
Model
response
forward
JavaBeans
Request 2
Controller
Request 3
servlet
Servlet/JSP/EJB Example
View
search.html Model
Request 1
response list.jsp
Client
Request 2
Request 3
servlet
Session EJBs
Controller
EJB
• Application Requirement:
– This portal should be an on-line application.
• The portal should allow lectures and students
into different application.
– A lecture should be able to compose, delete,
view and organize the exam questions .
• An exam is made up of multiple questions.
• Each question should consist 2 parts the
question and the answer.
• Different type of questions can be specified,
i.e. Multiple choice, short answer question and
so on.
• Exam questions can be stored into persistent
data.
Case Study: A Portal for Exams
(Cont.)
• Application requirement:
– A student should be able to display the
question and answer the questions in the
exam.
• Exam questions could be displayed one at a
time.
• A student should be able to go back and forth
through the exam questions.
• The answer of each question can be stored
into persistent data.
Application Modules
• Lectures:
– Compose/Delete/View exam questions.
• Students
– Display/Write Exams.
Choose Appropriate Design
index.jsp memu.jsp
create_exam.jsp
ProcessQuestionServlet
question_entry .jsp
CREATE_EX
ADD_TO_EXAM_FROM_REPOSITOR
question.jsp
exam
ADD
list_question.jsp DISCARD }
Question ADD_ANSWER
body.jsp
body.jsp
SAVE
Question body
Log_OUT
question_repository.jsp
JavaBeans Controller
Index Page: index.jsp
Create or Dispaly: menu.jsp
Create Exams: create_exam.jsp
Define Contants
public interface Constants {
// Servlet init params
public static final String SAVE_DIRECTORY = "saveDirectory";
public static final String QUESTION_TYPE = "questionType";
public static final String QUESTION_TYPE_TEXT =
"questionTypeText";
// ACTIONS from froms
public static final String ACTION = "action";
public static final String ADD = "add";
public static final String SAVE = "save";
public static final String CREATE_EXAM = "create_exam";
public static final String LOGIN = "login";
public static final String LOGOUT = "logout";
public static final String ADD_ANSWER = "add_answer";
...
// Parameters in a form
public static final String ANSWER_TYPE = "answer_type";
public static final String RADIO_ITEM = "radio_item";
public static final String TEXTBOX_ITEM = "textbox_item";
public static final String LECTURER = "lecturer";
public static final String STUDENT = "student";
// Servlet and JSP pages
public static final String INDEX_PAGE = "index.jsp";
public static final String PROCESS_QUESTION_SERVLET =
"process_question";
...
}
JSP: create_exam.jsp
<%@page import="java.util.*, tinc.servlet.*, tinc.core.*"
%>
<html>
<head>
<title>Login</title>
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
</head>
<body>
<%@include file="time.jsp" %>
<form method="POST" action="process_question">
<b>Subject: </b><input type="text" name="subject"
size="65">
<b>Duration: </b><input type="text" name="duration"
value="120" size="5">mins
<hr>
<input type="submit" name="action" value=<%=
Constants.CREATE_EXAM %>>
<input type="submit" name="action" value=<%=
Constants.LOGOUT %>>
<input type="reset" name="action" value="clear">
</form>
</body></html>
WEB.XML
<servlet>
<servlet-name>processQuestion</servlet-name>
<servlet-class>
tinc.servlet.ProcessQuestionServlet
</servlet-class>
...
<init-param>
<param-name>questionTypeText</param-name>
<param-value>
Multiple choice (radio/checkbox):Short answer entry
</param-value>
</init-param>
...
</servlet>
<servlet-mapping>
<servlet-name> processQuestion </servlet-name>
<url-pattern> /process_question </url-pattern>
</servlet-mapping>
ProcessQuestionServlet
public void doPost(HttpServletRequest req, HttpServletResponse
res)
throws ServletException, IOException {
String action = req.getParameter(Constants.ACTION);
String jumpTo = Constants.QUESTION_BODY_PAGE;
if (action.intern() == Constants.ADD) {
doAdd(req, sess);
} else if (action.intern() == Constants.ADD_ANSWER) {
doAddAnswer(req, sess); ...
jumpTo = Constants.QUESTION_ENTRY_PAGE;
} else if (action.intern() == Constants.CREATE_EXAM) {
doCreateExam(req, sess);
jumpTo = Constants.QUESTION_ENTRY_PAGE;
} else if (action.intern() == Constants.SAVE) {
doSaveExam(sess);
jumpTo = Constants.MENU_PAGE;
} else if (action.intern() ==
Constants.ADD_TO_EXAM_FROM_REPOSITORY) {
doAddToExamFromRepository(req, sess);
jumpTo = Constants.QUESTION_ENTRY_PAGE;
} else if (action.intern() == Constants.DISCARD) {
sess.removeAttribute(Constants.CURRENT_QUESTION);
jumpTo = Constants.QUESTION_ENTRY_PAGE;
} else if (action.intern() == Constants.LOGOUT) {
sess.invalidate();
jumpTo = "/" + Constants.INDEX_PAGE; }
RequestDispatcher rd = req.getRequestDispatcher(jumpTo);
rd.forward(req, res); }}
Question Entry: question_entry.jsp
Compose Questions: question.jsp
Compose Mutiple-Choice Questions
Compose Short Questions
Create Different Type of
Questions
Body item
• JavaBeans component:
– Qeustion, QuesionBody, and Exam.
• Exam question are described in Question.
• Each question has two parts: question header is
in Question, and question body is contained in
QuesionBody.
– The body contains possible answers which
describe in BodyItem.
– RadioBodyItem and TextBodyItem handle
different question type and the associated
answer.
• Exam contains information such as exam
duration, subject, and a collection of questions.
List Questions: list_question.jsp
JSP: list_questions.jsp
<%@page info="List question" import="java.util.*,
tinc.servlet.*, tinc.core.*" %>
<jsp:useBean id="current_exam" class="tinc.core.Exam"
scope="session" />
<%! Question[] question; %>
...
<jsp:getProperty name="current_exam" property="subject" />
</h1>
...
<jsp:getProperty name="current_exam" property="duration" />
mins</h1>
<hr>
<% question = current_exam.getQuestions();
if (question.length == 0) {
%>
...
<%= question[i].getQuestionText() %></font>
<% } %>
</ol>
<% } %>
<br>
<h1><a href="question_entry.jsp">Back to questions
entry</a></h1>
JavaBeans Component: Exam
question_repository.jsp
Student Module Design
index.jsp
exam.jsp
memu.jsp
Question JavaBeans
doExamServlet
Start
CurrentQuestionNumber
Iterate
Next
Previous
Logout/complete
?page # = number
Controller
Sit for a Exam
Exam.jsp
DoExamServlet
public class DoExamServletextends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
String jumpTo = Constants.EXAM_PAGE;
String action = req.getParameter(Constants.ACTION);
RequestDispatcher rd = req.getRequestDispatcher(jumpTo);
rd.forward(req, res); } }
Summary