?
Solved

JSP to Servlet hidden text passing

Posted on 2003-03-31
5
Medium Priority
?
780 Views
Last Modified: 2013-11-24
I currently have a web application that takes user input from a jsp page and processes that information within a servlet.  I need to check user input, and if an error is detected forward back to the user input jsp.  I also need to indicate the errors that they made.  I thought that this would be relatively simple to do.  I tried checking for errors in the servlet and then setting the text of a hidden input to indicate that an error has occured.

The problem that I'm having is that the hidden text box seems to be reset whenever a forward command is sent to it.  Whenever the jsp checks for the hidden text it finds an empty string.  I have logging setup, and while in the servlet I can tell that the hidden text was correctly set.  However as soon as its forwarded the hidden text is reset.

How can I check for errors in the servlet and pass a value to the jsp to indicate that some input was not correctly entered?

Thanks in advance,
Michael Crutcher
0
Comment
Question by:mcrutcher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8239858
It is often better to validate client-side using JavaScript, as this saves a trip to the server.
0
 

Author Comment

by:mcrutcher
ID: 8239944
I understand that there are some instances where verifying on the client side is better.  However the jsp page is maintained by someone else and they don't know java or java script.  I don't want to polute the jsp with a bunch of code.  Its only a jsp so that I can dynamically fill list boxes with a bean call.

I think I figured it out, but its pretty wierd.  I've been calling getParameter(String) on the request object to get the value.  This works for all of the text and list boxes but returns a blank string on a hidden input element.  The method getAttribute(String) on the request object returns the correct value.  So I fixed it, but its still pretty wierd.  Can anyone tell me the difference between request.getAttribute(String) and request.getParameter(String)?

Michael Crutcher
0
 
LVL 2

Accepted Solution

by:
amit_chauhan earned 750 total points
ID: 8240874
request.getParameter () returns the parameters that make the query string of an http request. These are the parameters that are passed from a jsp/html page by tags like <input type="text">.
You cannot set parameters programatically.

However, java provides methods setAttribute () and getAttribute () to set your own parameters (attributes) in the request, which can be accessed by another servlet (jsp) where the request is forwarded to.

But as a new request object is created for each http request, these attributes have the life time of the request.

They will be available to a forwarded jsp/servlet as the request object is the same, but if you are redirecting to a jsp/servlet, then its a new request, and those attributes and parameters will be lost.


Hope that helps
Thanks
Amit
0
 

Author Comment

by:mcrutcher
ID: 8240910
That works as an answer to me, but I have a couple of questions about your response if you don't mind.

You cannot set parameters programatically.
what is setParameter(String name, String value) for?

So an attribute has no direct relation to input from the user?  I mean I don't need to create a hidden box, I can just make up the attribute in code?
0
 
LVL 2

Expert Comment

by:amit_chauhan
ID: 8241128
I dont know where are you seeing the method setParameter (). I dont see that anywhere in the javadocs.

Second, yes, you are right, attribute has no relation to input from the user. So you dont need to create a hidden text box for it.

Hope that helps
Thanks
Amit
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question