Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing values from jsp to servlet new jsp

 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really frustrated now. Here's the deal... I have a jsp form that submits two fields to a servlet. The servlet then sets up a socket connection to the mainframe, processing my information and returns a string. I have taken the string and used "substring()" to separate the string into different values.

My issue is that I have to figure out how to display this data in a new jsp inside of text boxes so the user can choose(or not choose) to edit the information. I can write the HTML in the servlet but from all of my research...

I keep getting null when I try the request dispatch to forward the values to a new page...

Will gladly display code if asked...
Help
 
Scott Selikoff
author
Saloon Keeper
Posts: 4009
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code would be nice, I'm not sure exactly what your problem is. You can store objects in session variables (although for passing from servlet-to-jsp I wouldn't recommend it). You can have the servlet submit the data to a JSP as it would any form, and then read the data from the JSP using a JSP:bean or using request.getAttribute() to read the data as from a form.
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess what I'm trying to ask is ... How would I take values from a servlet and display them inside of a form without using println() inside of my servlet. I have a very large application to build and this is my first screen so I am very careful about doing this the right (and most efficient) way...

I send two fields to the servlet from the jsp but I need to return those two PLUS two others that are given from the servlet. One of those new fields needs to be in text box.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way is to pass the context from your servlet to a JSP.

Look up Model, View, Controller or MVC pattern.
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The MVC architecture is what I plan on using. What code would I use inside of "final" form to display the values sent from a Servlet.

Maybe that question will help clear up my problem. This is what I have now.

Here is code from servlet:
String desc = myResponse.substring(184, 233);
String nextJSP = "/jspforward.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);

Here is code from jspforward.jsp:
<p align=left><FONT face=Arial><strong>DescriptionX:</strong></font>
<input name=DESC type=text id=DESC size=50 maxlength=50 value='" + desc + "'>

This just displays " + desc +" inside of the text box when sent back to the browser.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a real simple MVC example on my site that you can look at to see how I do this if you like.
http://simple.souther.us
Look for SimpleMVC.

In short, any values that need to be passed from the servlet to the JSP get loaded into a bean, the bean gets bound to request scope (there are other scoping objects too), then the context gets forward to the JSP.

From within the JSP there are several ways to read the values from the bean.
The easiest way these days is with EL

${beanName.property}



[KLUNK!]
[ December 15, 2005: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, the JSP will have no automatic access to variables defined in the servlet. You must place the values to be referenced from the JSP into request context with the request.setAttribute() method.



Then, on the JSP you must use approriate notation to cause the value to be evaluated. Under a JSP 2.0 container:


[ December 15, 2005: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this point, Ben and I both look like Klingons from bumping our heads so often!



Not so much hair though...
[ December 15, 2005: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Notice that I also fixed up your HTML notation a bit. Leaving off the quotes for attributes leads to nothing but trouble.
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply you guys. I'm checking out the link and I already see it as being a great help. It's one thing to see it in books ... it a total different story when you can see a simplistic example.

Here is the big picture (since I have your attention).... I have to set up a logon jsp that sets different options (other jsp hyperlink accessibility) and diplays them on a "menu" jsp.

From that menu jsp, a user can choose an option (the jsp I'm working on right now for instance).

I'm going to have to keep up with user information, so are beans the best way to do this or do I have any other options...

Forgive the fact that I am still so "wet behind the ears" but I just left the mainframe world so I'm trying the best I know how.. haha
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Will Sillmon Jr.:

From that menu jsp, a user can choose an option (the jsp I'm working on right now for instance). I'm going to have to keep up with user information, so are beans the best way to do this or do I have any other options...


If you put all this menu information into a bean and bind it to session scope, you can look it up, once, when the user logs in and retain it in memory on the server for the duration of their session.
Once their session expires (or if you invalidate the session in response to the user making a logout request), the object is freed for garbage collection.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[removed unrelated post]

Karthik,
You asked this same question in this thread:
http://www.coderanch.com/t/288820/JSP/java/help-one
Read the response there.
[ January 09, 2006: Message edited by: Ben Souther ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic