File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes to set JSP HTML element values on servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "to set JSP HTML element values on servlet" Watch "to set JSP HTML element values on servlet" New topic
Author

to set JSP HTML element values on servlet

Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
HI,

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.

regards,
abhishek shrivastava
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3829

...I wann to set to set the values for that JSP-HTML elements on the servlet...

You want to set previously selected items in the JSP in the response to a request from the same or....?

And is this about the same?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
Hi Vijitha Kumara,
yes i want to reset value for an already present HTML element on the JSP.
e.g.
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.

I can use java scriptlets.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
Bear Bibeault, can you please put an example here.... or a link where i can get the referenced material.
please, i need it urgently... i am in a r&d work now.. my further step relies on it....
thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

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:
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
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.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3090
    
  40

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.

Scriptlets should NOT be the way to go.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
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.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3090
    
  40

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.
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

As stated by Devaka there is no other way for doing this.
Also as you say you have hundreads of jsps then its much better to add el in value tags the make single filter and write hundreads of ifs.

Also, making a response wrapper in filter will again be tricky and complicated thing.
I would say, you should no make shortcuts to every thing else it will be unmaintainable.


SCJP 91%, SCWCD 97%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: to set JSP HTML element values on servlet