• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing data to and from a jsp page

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to javaranch. we are Happy to Have you Here
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Gary Monk
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



They are not. Not even close.
 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please accept my apology for mixing initParameters and parameters in line 2...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic