The idea is easy: if condition is true - to show first JSP-page, otherwise - to show second JSP-page.
First of all i have index.jsp which makes a request to a servlet. Then that servlet uses class Client to keep all incoming information and transfers the instance of this Client class as a session's or request's attribute.
The servlet's main code:
What we see:
Using Scriptlet in the correctAnswer.jsp:
What we see:
Using Java Bean (instead of using servlet) in the wrongAnswer.jsp:
First of all i have index.jsp which makes a request to a servlet. Then that servlet uses class Client to keep all incoming information and transfers the instance of this Client class as a session's or request's attribute.
The servlet's main code:
private void gotoPage(String address,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = getServletContext();
RequestDispatcher dispatcher = sc.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
............
HttpSession session = request.getSession(true);
long accessTime = session.getLastAccessedTime();
String answer = request.getParameter("selection");
String name = request.getParameter("name");
Client cl = new Client();
cl.setCreationTime(accessTime);
cl.setAnswer(answer);
cl.setName(name);
session.setAttribute("client", cl);
String selection = request.getParameter("selection");
//Conditional Dispatch
if (selection.equals("true"))
gotoPage("/answerCorrect.jsp",request, response);
else gotoPage("/answerWrong.jsp",request, response);
What we see:
Using Scriptlet in the correctAnswer.jsp:
<%@ page errorPage="Error.jsp" %>
<%@page import="test.Client"%>
<div style="width:300px;float:left;display:inline-block;
background:#fbdce1;border:5px solid #00ff00;padding:10px;">
<h1>Answer: Correct</h1>
<%
Client cl = (Client)session.getAttribute("client");
out.println("Your name is: " + cl.getName() +"<br />");
out.println("Time: " + cl.getCreationTime() +"<br />");
out.println("Answer: " + cl.getAnswer());
%><br />
<%@ include file="ContactSection.jsp" %>
</div>
What we see:
Using Java Bean (instead of using servlet) in the wrongAnswer.jsp:
<%@ page errorPage="Error.jsp" %>
<%@page import="test.Client"%>
<div style="width:360px;float:left;display:inline-block;
background:#fbdce1;border:5px solid #ff0000;padding:10px;">
<h1>Answer: WRONG</h1>
<jsp:useBean id="client" class="test.Client" scope="session" />
Your name is:
<jsp:getProperty name="client" property="name" /><br />
Creation time:
<jsp:getProperty name="client" property="creationTime" /><br />
Your Answer:
<jsp:getProperty name="client" property="answer" /><br />
<%@ include file="ContactSection.jsp" %>
</div><br />
No comments:
Post a Comment