aspose file tools*
The moose likes Servlets and the fly likes Passing data to and from a jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing data to and from a jsp page" Watch "Passing data to and from a jsp page" New topic
Author

Passing data to and from a jsp page

Gary Monk
Greenhorn

Joined: Jan 14, 2009
Posts: 4
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!!


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to javaranch. we are Happy to Have you Here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Coming to your question, please use code tag to post your question,it makes the code clear .

and i guess you want to implement pagination into your jsp .if yes,please look Paging or Pagination

if any clarification ,Let us know
Ashwin Pai
Ranch Hand

Joined: May 20, 2008
Posts: 90
As Seetharaman said, if you are looking for the Paging/Pagination concept, look for in the forums..
However I wanted tell you about the incorrect sysntax you are trying to do here..
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.


You are trying to mix client code & server code. I suppose what you are intending to do by is to set attribute "Direction" to value"Prev" in session (server session). What you need to realize here is once the page is rendered(server sends HTML) to client you have enetred the client context and cannot access the server object session. If you need to access values from session, you would have to store them in hidden input elements. Now on submitting the page you could retrieve these from the request object(server side).
Make sure you understand what objects are available in the server & client contexts.

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.

Did you check the server logs ? Can 'classContent.do" resovled ? There could be many reasons for this.

HTH
Ashwin


The only way to make your dream come true is to wake up.
Gary Monk
Greenhorn

Joined: Jan 14, 2009
Posts: 4
Thank you for your help!!

I have both problems fixed. Both of them had to do with an attribute.

I do have another question. What is the difference between getAttribute and getParameter? Acutally, I guess the question is what is the difference between the attributes and parameters? I ask because in the servet they seem to be interchangeable and there isn't a setParamter.

Thanks again!
muktesh tripathi
Ranch Hand

Joined: Nov 20, 2007
Posts: 30
What is the difference between getAttribute and getParameter?


Well attribute and parameter are quite different in nature.
1. Attribute return type is object while parameter has string as return type.
2. Parameters are declared in DD while attributes can be set using setAttribute("a","b");
3. We get request parameters from request (i.e posted data), while servlet init parameters are declared in DD.
4. Value of attributes can be changed using setAttribute("a","c"); while parameters are 'you get what you submit or set in DD'.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

muktesh tripathi wrote:
2. Parameters are declared in DD while attributes can be set using setAttribute("a","b");
Those are init parameters and have nothing to do with getParameter().

"Attributes", also termed scoped variables, are Java objects set into (and retrieved from) the various contexts (page, request, session and application) by server-side code.

"Parameters" are the results of request submissions from the client.

"Init parameters" are a completely separate concept that has nothing to do with either of the methods getAttribute() or getParameter().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Gary Monk wrote:I ask because in the servet they seem to be interchangeable ...


They are not. Not even close.
muktesh tripathi
Ranch Hand

Joined: Nov 20, 2007
Posts: 30
Please accept my apology for mixing initParameters and parameters in line 2...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing data to and from a jsp page