Get name of the form in servlet - html

<form name="GetAll"
method="post"
action="NewServlet">
<input type="text" name="des">
<input type="submit">
</form>
How I can get name of this form (GetAll) in my NewServlet servlet?

The name attribute of the form is not part of the HTTP request when it is submitted. You could add a hidden field instead to identify multiple forms off same page like
<input type="hidden" name="formName" value="formA">
keeping the name attribute of the hidden input same of course.

Related

Prefill html form with element without ID

I have a form:
<form action="#" method="post" class="aui" id="advanced-settings-form">
<input class="text medium-long-field" type="text" name="6" placeholder="/**/*">
</form>
I know that it is possible to prefill the form via url as: URL?id=text
But in my case there is no id - only a name.
Is it still possible to prefill the form via URL?
Well, for sending data through a form, we use name attribute and not id attribute. What you are looking for is to use value attribute of <input> tag.
Also URL?id=text which you have mentioned, means that the form has already been submitted usingmethod=get. It has no relation with prefilling of the input boxes.
Something like this:-
<form action="#" method="post" class="aui" id="advanced-settings-form">
<input class="text medium-long-field" type="text" name="name_here" placeholder="/**/*" value="text">
</form>

Redirect to selected site with variable which i got

<form action="/subsite/" method="GET">
<input type="text" name="" placeholder="Your Nick">
<input class="button" type="submit" />
</form>
I want to redirect I mean it should looks like
www/subsite/text
What should I use ? POST ?
The method="get" means that the parameters (and values) will be sent in the query string (the stuff after the question mark in the URL). eg.
/subsite?input_field_name=input_field_value
In your case, the input field doesn't have a name, which will cause problems. You probably want something like this:
<input type="text" name="nickname" placeholder="Your Nick">
So if you submit this form:
<form action="/subsite" method="get">
<input type="text" name="nickname" placeholder="Your Nick">
</form>
Then after submitting, the browser will go to:
/subsite?nickname=value_of_nickname_variable
If you use a method="post", then the form data (variables) will be sent along in the request body, not the query string. There are other differences between get/post, but that's one of them :)
If you just want to do a simple redirect when clicking the button, you could use javascript instead, eg.: window.location.href='/my_url_path_here'

How to build a custom URL from a HTML form?

I have a HTML form like:
<form name="input" action="" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
I need to build a URL when the submit link is clicked and send the user to it, using the username from the form in the correct position in the URL.
URL:
http://example.com/htmlchat/init.html?init_room=1&init_user=USERNAME_FROM_FORM_HERE
Can someone help (an example would be great)?
Don't use POST when you want the data to appear in the URL
Do put all the data you want in your URL in the form
Get the names of your fields right
Put the base URI in the action
such:
<form action="init.html">
<input type="hidden" name="init_room" value="1">
<label>
Username:
<input name="init_user">
</label>
<input type="submit" value="Submit">
</form>
This is possible with either Javascript or a server side language such as PHP. With Javascript you want to update the action attribute.
You could use jQuery to do this.

How to change the url sent to an html form?

Suppose we have:
<form action="xxx">
<input type="checkbox" id = "checkbox" name="checked" value="1">
<input type="submit">Submit</input>
</form>"
When click the submit button, the url should be something like "xxx?checked=1". In this case, I want to append another parameter to the url. I know using hidden value is an option, but do we have a more direct way? Please suggest. Thanks
The fact is the url depends on the method you are using to send the form. This is set on the method attribute on the <form> tag.
The URL of the action can be any valid URL so to add extra attributes like something=1 and other=2 you could set the following:
<form action="xxx?something=1&other=2">
<input type="checkbox" id="checkbox" name="checked" value="1">
<input type="submit">Submit</input>
</form>
submitting the form will now send the GET request xxx?something=1&other=2&checked=1

how to pass textbox value to next jsp page by clicking on a different form button without using javascript

here is my code
<body>
<input type=text name=name>
<form action=new3.jsp method=post name="f1">
<input type=text name=name>
<input type=submit value=next>
</form>
<form action=new1.jsp method=post>
<input type=submit value=back>
</form>
</body>
as you can see there are two forms here with two different submit buttons named "next" and "back". The text box is in the form where "next" button is. My question is here how to send the value of this text box to "new1.jsp" clicking "back button" without using javascript. Thanks in advance...
Don't post form to JSPs. Post the to servlets instead, and let the server analyze the parameters and forward to the appropriate JSP depending on which button has been clicked:
<form action="/controllerServlet" method="post" name="f1">
<input type="text" name="name"/>
<input type="submit" name="destination" value="next"/>
<input type="submit" name="destination" value="back"/>
</form>
Using the above form (which BTW, is valid HTML), the servlet can use the value of the "destination" parameter. If it's "next", it should forward to new3.jsp. If it's back, it should forward to "new1.jsp". And if, for example, you want to redisplay the same page because the user didn't enter a valid value in the text field, it can do so.
That's the well-known MVC pattern. You should use it.