This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes passing values from jsp to servlet new jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing values from jsp to servlet new jsp" Watch "passing values from jsp to servlet new jsp" New topic
Author

passing values from jsp to servlet new jsp

Will Sillmon Jr.
Greenhorn

Joined: Aug 25, 2005
Posts: 14
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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Will Sillmon Jr.
Greenhorn

Joined: Aug 25, 2005
Posts: 14
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

Joined: Dec 11, 2004
Posts: 13410

The easiest way is to pass the context from your servlet to a JSP.

Look up Model, View, Controller or MVC pattern.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Will Sillmon Jr.
Greenhorn

Joined: Aug 25, 2005
Posts: 14
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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 ]

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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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

Joined: Aug 25, 2005
Posts: 14
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Aug 31, 2005
Posts: 479
[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 ]
 
Don't get me started about those stupid light bulbs.
 
subject: passing values from jsp to servlet new jsp
 
Similar Threads
Retrieving data from a servlet then forward to JSP page
character encoding from jsp->servlet
JSP makes me headache
Populating bean from servlet
How to pass Vector to servlet from a jsp page?