I have an application where I want to display multiple pages. Each page has a prev and next button. The
servlet code that invokes the
jsp page is...
currentSession = request.getSession(true);
String className = request.getParameter("classname");
String direction = (String)currentSession.getAttribute("Direction");
System.out.println("Direction = " + direction);
Integer pageNumber = (Integer)currentSession.getAttribute("currentPage");
if(pageNumber == null)
pageNumber = new Integer(1);
if(direction != null) {
System.out.println("Direction = " + direction);
if(direction.equals("Next")) {
if(pageNumber < 99) {
pageNumber++;
}
}
else {
if(direction.equals("Prev")) {
if(pageNumber > 1) {
pageNumber--;
}
}
}
}
System.out.println("Page Number = " + pageNumber);
String pageContent = cm.getPage(className, pageNumber);
if (pageContent.length() > 0) {
currentSession.setAttribute("currentPage", pageNumber);
request.setAttribute("pageNumber", pageNumber);
request.setAttribute("className", className);
System.out.println("Class name = " + className);
request.setAttribute("pageContent", pageContent);
RequestDispatcher view = request.getRequestDispatcher("CEUClassContent.jsp");
view.forward(request, response);
}
The jsp code is
<% String className = (String)request.getAttribute("className");
String content = (String)request.getAttribute("pageContent");
Integer pageNumber = (Integer)request.getAttribute("pageNumber");
out.print("<h1> Class Name: " + className + "</h1>");
out.print("<p></p>");
out.print("<h2>Content</h2>");
out.print("<p>" + content + "</p>");
out.print("<br /><br />");
out.print("<form action='classContent.do' method='post' />");
if(pageNumber > 1) out.print("<input type='submit' value='Prev' onclick='session.setAttribute('Direction', 'Prev') />");
out.print("<input type='submit' value='Next' onclick='session.setAttribute('Direction', 'Next') />");
out.print("</form>");
%>
I have two issues. The first is, I can't figure out a good way to set the diection. As you can see I have two buttons defined and I try to do a session.setAttribute in the onclick event, but it never seems to execute. I have looked for solutions to this, and there are some on the internet, but there aren't any examples and I can't seem to get them to work.
The second issue is the first page displays correctly, but when I click either button a blank page displays. When I go back and refresh the page it now displays the second page. In case your wondering, I hard code the direction just to see if the rest of the servet/jsp works.
Thank you very much for your help!!