• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

to set JSP HTML element values on servlet

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...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?
 
Abhishekshri Shrivastava
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Abhishekshri Shrivastava
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 88
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic