This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
i get a request from a JSP on a servlet.
I wann to set to set the values for that JSP-HTML elements on the servlet.
then i will forward to that jsp. and would like to see those set values for those particular elemnts.
it's easy to set it through jsp using java script. but i wann to set values for those attributes on servlet.
please give me an example.
is it possible?
please suggest me all probable solutions.
Hi Vijitha Kumara,
yes i want to reset value for an already present HTML element on the JSP.
i hav a text box named "fName" on the JSP page " first.jsp".
i get the value of "fName" as "ABC",now i wann to set its value as "PQR" and wann to forward to the same jsp page and display "fName" value as "PQR".
i don't have to use java expression tags or java script.
To preset the values of forms elements you need to format the generated HTML such that the elements possess value attributes that define the predefined value. For example:
This is something that you will need to do inside the JSP. The servlet controller can set the values to be used as scoped variables, but the work to create the markup must be performed by the JSP.
Abhishekshri Shrivastava wrote:please, i need it urgently...
Please read EaseUp. Saying things like this make me want to just ignore this post.
Let's say that your servlet puts a bean named formValues into request scope that captures the values for form elements in its properties (you could also use a Map).
On the JSP page:
Joined: Jan 13, 2011
you want me to use JSTL concept.
yes we can do the things in the suggested way....
but for that we will have to put the value part for all html control...
i want a generic code which will work for all html elements....
let me make the scenario more clear:
i have around 500 already existing JSPs. there is one servlet working as a controller for it. there is a genecric code in servlet part which works for all jsps. using jericho parser i get all the html element names, their types and values. i have few validations on servlet.
here i want to set the values for few of the html elements and want to redirect / forward to the same jsp. suggest me the possible way to set the value for those html elements here.
i have also added such a related post on JSP forum; if there's any way to set those vaues through JSP scriptlet part. but the coding must be generic.as the application is already existing one, so the constraint is that i can't go to all existing html elements and put some coding part there. i wann a generic coding which will work in all conditions and for all jsps.
at each JSP request to servlet i have the html control name and its value. is there any way like setElementValue(htmlControlName. value) type of thing.
i have been also allowed to use any plugin or any external jar to do so.
Abhishekshri Shrivastava wrote:you want me to use JSTL concept.
Nothing related to JSTL has been discussed yet. What Bear has pointed out is using the 'value' attribute of HTML elements with EL (expression language) to get the actual value from the request scope.
Abhishekshri Shrivastava wrote:but for that we will have to put the value part for all html control...
i want a generic code which will work for all html elements....
Yes, and generic code?? The 'value' attribute is the only way which you can use to preset the values of the input elements.
Abhishekshri Shrivastava wrote:I have around 500 already existing JSPs. there is one servlet working as a controller for it. there is a genecric code in servlet part which works for all jsps.
No idea of what your exact requirements are; but I guess that you need a Servlet Filter here, instead of a Servlet.
Abhishekshri Shrivastava wrote:using jericho parser i get all the html element names, their types and values. i have few validations on servlet.
I didn't get that. Jericho parse is for parsing HTML DOM elements when it is present. Are you parsing some external HTML content from outside your application? If it isn't, what's the use of parsing the HTML elements only to get the attribute names and values?
Abhishekshri Shrivastava wrote:here i want to set the values for few of the html elements and want to redirect / forward to the same jsp.
All the elements, which are the part of the initial request, are stored as request parameters. For the rest of the elements, you need to code your servlet to set request scope attributes and then dispatch the request to your JSP. From that JSP, you can use EL to get those values and assign them to the 'value' attribute of the input elements as in Bear's example.
Abhishekshri Shrivastava wrote:If there's any way to set those vaues through JSP scriptlet part.
Let me make the scenario more clear to you using a sample approach:=>
I have a number of jsps as : one.jsp, two.jsp, three.jsp,four.jsp …etc…….. and one common servlet for all those jsps. I get a request to servlet, find url for the request use Jericho parser to tokenize the jsp page and getting all html elements name, their type and value in following way:
method "displayAllElements" has been called from servlet and it's common for all httprequest from jsp (even for all jsps, like a generic approach)
i have posted a sample code example here. suppose i have all html elements <input type="text"...> on jsps
and i want to set value "DEFAULT" if the textbox value is blank.
now suggest me the approach.
parameter value "sourceUrlString" will having value from "request.getRequestURI()"
i know i had to do some sort of operation with the "request.getRequestURI()" value to get the exact url. so don't bother for that. i will take care that.
you, please help me to set those html value from servlet. agin i will redirect o the same url and would like to see the new reset value as "DEFAULT" for those blank textboxes.
i can't expose the whole project or r&d stuff, as it would be compicated to understan. so i have shared a easy sample.
First, please UseCodeTags when you post a code on the ranch. I've edited your post to add code tags for you.
Abhishekshri Shrivastava wrote:
This creates another HTTP Request to that resource. If this "http://localhost:7001"+sourceUrlString URL is mapped to the same Servlet as above, the same servlet gets run over and over can will end up with a self-initiated DDOS attack.
Don't use URL calls and invoke the same resource. What you need to have is a Servlet filter, which fine tunes the response. If you need something like filling the value attributes to "DEFAULT" or something, you can have that logic called from the filter filtering the response. From that filter, you can get the output stream from HttpServletResponse object, and use a DOM parser library to edit the response body as you need.