aspose file tools*
The moose likes Servlets and the fly likes setting the value to a form of html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "setting the value to a form of html" Watch "setting the value to a form of html" New topic
Author

setting the value to a form of html

suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Hi.,
I have a small question regarding servlets and html.I have a text field in the form like this.
<form method="GET" action="ChoosenName.do">
<input type="text" name="firstName">
<input type="submit">
</form>
I am submitting this form. when i press the submit button,i need to return to the same html page with the value what i entered at first time.I have done this with RequestDispatcher in my servlet as follows.
RequestDispatcher view= request.getRequestDispatcher("form.html");
view.forward(request,response);
But i dont know how to set the value for text box.I have tried with request.setattribute().But i identified that html doesnt know any thing regarding request object.The page is loading with empty text box.
help me...
And Thanks for any suggestion,solution.
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Why not you use jsp instead of form.html ?

You can use request object in the jsp page for setting teh text box value while loading html form(i.e using onload event you can do it)

Regards
Baseet Ahmed

*******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
*******************
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Hi Baseet.,
Thanks for your reply.As you said we can use JSP instead of HTML.But I need to use HTML only.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Originally posted by suresh sai:
we can use JSP instead of HTML.But I need to use HTML only.

Why?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Because i am using only html pages.If i choose to JSP then i can able to write scriptlets....To avoid that scriptlets i am displaying through HTML.
Can anyone tell me .,Is it possible to set a field value of a html form by using servlets while loading the page?
Sunny Dyal
Greenhorn

Joined: May 27, 2008
Posts: 1
HTML is used for static pages. If you want to make it dynamic then you have to use some server script. In HTML page, server script is possible by AJAX or Java script. Try to use java script. It will help you.

Javawebparts is a good AJAX framework.
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Because i am using only html pages.If i choose to JSP then i can able to
write scriptlets....To avoid that scriptlets i am displaying through HTML
.


If this the only reason,then you can rename your filename to form.jsp.
Because writing scriptlets is not mandate in Jsp.

The advantage you will get is, you can call the Javascript function while loading form(i.e onload event in Body tag).And in that function use this:
<%=request.getAttribute("whatevername")%> to get the needful data from your servlet.

Hope this helps.

Regards
Baseet Ahmed

*******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum
*******************
cortin flaneir
Greenhorn

Joined: May 26, 2008
Posts: 13
if you want to make your page dynamic you must use any server side language.if you dont want to use it then use java script or Ajax.for this you must have Know about java and javascript comminication.i suggest you that use jsp page.okkk

try it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

You can avoid scriptlets through use of modern techniques like the JSTL and EL. I haven't placed a scriptlet in a JSP page in years.

If you use HTML, you cannot add any dynamic data to it.

A word of advice. In the future, the answer to "why are you doing things this way" shouldn't be "Because I want to!". Rather, explaining why you want to do something can help find the proper ways to achieve your goals.
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Hi All.,

I am very happy by looking at your suggestions and solutions. I am very thankful to you for providing your valuble time.As you said i have used the javascript. After changing that my code is as follows...

HTML page:
---------------
function setValues(){
theUrl_Index=document.URL.indexOf('?');
if(theUrl_Index != -1){
theUrl=document.URL.substring(theUrl_Index+1,document.URL.length);
var theUrl_String = new Array();
theUrl_String = theUrl.split('&');
for(index=0;index<theUrl_String.length;index++){
var name_Value = new Array();
name_Value = theUrl_String[index].split('=');
document.forms[0].incrVar.value =name_Value[1];
}
}
else{
alert('There are no values to set...');
}
}
<h1 align="center">Increament Example</h1>
<form method="GET" action="Increament.do">
Enter the Number for which you need the increament..::
<input type="text" name="incrVar">
<input type="submit" value="Increase..">
</form>

And my Servlet code is as follows::
----------------------------------------
String varValue = req.getParameter("incrVar");
res.sendRedirect("main.html?incrVar="+varValue );

Here,the URL is coming in Address bar...But main.html is not opening...Browser is showing Blank(White) Screen.
Please tell me what is wrong with the above code...
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
sorry to mention one thing...I am calling the java script function onload.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

So let me get this straight. You want to avoid using scriptlets (good idea), so you don't want to use JSP at all (not so good idea), and rather, you will populate the fields with JavaScript via info passed on the URL (really poor idea)?

Why would you want to do this in such a weird and inefficient way rather than use the industry-standard, best-practice method of using scriptless JSP with JSTL and EL?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting the value to a form of html