I would like to ask how can I use the same submit for 2 the html forms in one page. I would like to have the same action for both of the forms and thus the same servlet to handle the reuqest.
<form action = "add">
Enter first number: <input type = "text" name = "num1"><br>
Enter second number: <input type = "text" name ="num2"><br>
</form>
<br>
<br>
<form action = "add">
Enter Third: <input type = "text" name = "num1"><br>
Enter fourth: <input type = "text" name ="num2"><br>
<input type = "submit">
</form>
</body>
1 You can to enter one hidden parameter like
<input type="hidden" name="fieldHidden" value="formOne/formTwo">
in the jsp file for each form.
On servlet you will pass value of parameter into new variable
String form = request.getParameter("fieldHidden");
if(form.equals("formOne"){
//process create logic for form 1
}
else if(form.equals("formTwo")) {
//process create logic for form 2
}
for example :
in your jsp file :
<form action = "add">
<input type="hidden" name="fieldHidden" value="formOne">
Enter first number: <input type = "text" name = "num1"><br>
Enter second number: <input type = "text" name ="num2"><br>
<input type = "submit">
</form>
<br>
<br>
<form action = "add">
<input type="hidden" name="fieldHidden" value="formTwo">
Enter Third: <input type = "text" name = "num1"><br>
Enter fourth: <input type = "text" name ="num2"><br>
<input type = "submit">
</form>
On servlet you will pass value of parameter into new variable
String form = request.getParameter("fieldHidden");
if(form.equals("formOne"){
//process create logic for form 1
}
else if(form.equals("formTwo")) {
//process create logic for form 2
}
Hope this helps :)
Related
I know the title might be misleading but here my question: In Thymeleaf we set request params with the input (in HTML). Is it possible to have an input field that sets the path variable. For example I have an method like this:
#PostMapping("/house/{id}/rent")
public String rentHouse(#RequestParam Date startDate, #PathVariable("id") long id, Model model) {
House h = new House();
h.setId(id);
r.setStartDate(startDate);
Rents rents = rentsService.createNewRent(h, id);
model.addAttribute("rent", rents);
return "House";
}
And in House.html I want something like this:
<form th:action="#{/house/${id}/rent/}" method="post">
<label for="startDate">start Date:</label><br>
<input type="datetime-local" id="startDate" th:name="startDate" placeholder="startDate"><br>
<label for="id">house id:</label><br>
<input type="number" id="id" th:name="id" placeholder="id"><br>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
So that when I input something then the result url should be looking like this (I know start Date has false format):
localhost:8080/House/12/rents?startDate=02.21.22
And is it also possible to pass request body in Thymeleaf, I searched for similar questions but they all solved it by manually putting the path variable in the url.
Thanks in advance
I have dropdown with options and the values. I can get the option value by the dropdown name in servlet but how can i get the dropdown "value" in servlet. In screenshot, temporarily i concatenated the with options but i want to store value in variable in servlet.
Please help:
HTML:
<input type="text" name="taxiDropdown" id= "taxiDropdown" placeholder="Search taxi...">
</div>
<div class="scrolling menu">
<%
List eList = (ArrayList) session.getAttribute("taxiInfo");
%>
<%
for (int i = 0; i < eList.size(); i++) {
%>
<div class="item" data-value="<%=((TaxiInfo) eList.get(i)).getID()%>">
<div class="ui green empty circular label"></div>
<%=((TaxiInfo) eList.get(i)).getTaxiPlate() +" "+ ((TaxiInfo) eList.get(i)).getID() %>
</div>
<%
}
%>
</div>
</div>
Servlet:
String val = request.getParameter("taxiDropdown");
(in "val", I want to store the value of the dropdown not the option text)
In JSP you should have something like that:
<form method="post">
<select name="taxiDropdown" id="taxiDropdown">
<%
List<TaxiInfo> eList = (List<TaxiInfo>) request.getAttribute("taxiInfo");
for (TaxiInfo taxiInfo : eList) {
%>
<option name="<%=taxiInfo.getTaxiPlate()%>" value="<%=taxiInfo.getID()%>"><%=taxiInfo.getTaxiPlate()%></option>
<%
}
%>
</select>
<input type="submit" />
</form>
Then in controller/servlet you will receive the id of TaxiInfo:
String val = request.getParameter("taxiDropdown");
System.out.println(val);
Or in your case you should set a hidden input with javascript with desired value.
added this code in html:
Move value of the dropdown selection to hidden textbox
<script type='text/javascript'>
$(function() {
$('#driverdp').change(function() { <-- this is my dropdown -->
var x = $(this).val();
$('#driverid').val(x); <-- this is my textbox -->
});
});
</script>
Servlet:
get the value of hidden text in servlet
String text= request.getParameter("driverId");
hope it will help someone
I have a HTML page with dinamycally changing number of select elements.
<script>
function getValues() {
var selects = document.getElementsByTagName('select'),
arr = Array.prototype.slice.call(selects),
selectValues = arr.map(function (select) {
return select.value;
});
return selectValues;
}
</script>
<script type='text/javascript'>
function moreSelect() {
for (i = 0; i < number; i++) {
// Append a node with a random text
container.appendChild(document.createTextNode("Name " + (i+1) + ": "));
// Create an <input> element, set its type and name attributes
var input = document.createElement("select");
input.name = "name" + (i+1);
container.appendChild(input);
// Append a line break
container.appendChild(document.createElement("br"));
}
</script>
<form action="action"method="POST" onsubmit="return getValues;">
More selects (max. 9):<br>
<p>
<input type="number" id="name" name="name" value="0"
min="0" max="9"><br />
<button type="button" onclick="moreSelect()">Add</button>
<br>
<p>
<br>
<div id="container" /></div>
<p>
<br> <br> <input type="submit" value="Go">
</form>
I want to collect this values to a List or an Array before the POST method and give this parameter list to my Java controller like this:
#RequestParam("allValues") List<String> allValues
Edit: I edited it, but doesn't works.
Get all selects, transform them to a real Array by Array.prototype.slice. Now you can use map to get all values. getElementsByTagName returns a HTMLCollection, that does not support map(), etc.
var selects = document.getElementsByTagName('select'),
arr = Array.prototype.slice.call(selects),
selectValues = arr.map(function (select) {
return select.value;
});
Now selectValues is an Array of the select values.
You can add one hidden form parameter say with name "allValues" and using javascript before posting Form, you can add all select values in that parameter.
I need to pass my array of dynamically created objects from jsp to java action class , Meanwhile i am tring following code to set array objects as a request parameter for action form . But while fetching in action class it produce null result ,as array object are not passed on to action form. Kindly provide me a right way to pass array list to action class. Thanks in advance ////
<script>
var i;
var arraya = new Array();
var arrayb = new Array();
var arrayc = new Array();
var idCount = 1;
function arr()
{
for (var j=0;j<idCount;j++)
{
arraya[j]= document.getElementsByName("a"+j)[0].value;
arrayb[j]= document.getElementsByName("b"+j)[0].value;
arrayc[j]= document.getElementsByName("c"+j)[0].value;
}
var one=arraya.valueOf();
var two=arrayb.valueOf();
var three=arrayc.valueOf();
}
</script>
<input type="text" name="a0">
<input type="text" name="b0">
<input type="text" name="c0">
<input type="button" onclick="addDiv();" value="Add"/>
<input type="hidden" name="one" value="<%= request.getParameter("one") %>" />
<input type="hidden" name="two" value="<%= request.getParameter("two") %>" />
<input type="hidden" name="three" value="<%= request.getParameter("three") %>" />
You can achieve it by doing this -
Simply have input type like this. Notice no index used.
<input type="text" name="a"/>
<input type="text" name="b"/>
<input type="text" name="c"/>
In your action class you can then do this. The values will be in the order.
String[] a = request.getParameterValues("a");
String[] b = request.getParameterValues("b");
String[] c = request.getParameterValues("c");
Although since you are using Struts you should actually create an object having properties a, b and c. Use the object in array fashion.
I have a simple form
form action="email.jsp" method="post"
<label for="firstname">Your Name: </label>
input type="text" id="name"<br/>
<label for="email">Your Email: </label>
input type="text" id="address"<br/>
<label for="message">Message: </label>
textarea size="30" rows="4" class="expand" id="comments"</textarea<br/>
input type="submit" value="Send" input type="reset"
/form
and am posting to a email.jsp page running in tomcat 5.5, working for another website i use that is in flash
this email.jsp is coming up with null values every time i post data to it - code below
can anyone see what i'm doing wrong?
<%# page import="sun.net.smtp.SmtpClient, java.io.*, javax.servlet.http.HttpServletResponse, javax.servlet.jsp.PageContext" %>
<%
String name = request.getParameter("name");
out.print(name);
String address = request.getParameter("address");
String comments = request.getParameter("comments");
String from="test#there.com.au";
String to="test#where.com";
try{
SmtpClient client = new SmtpClient("localhost");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("From: " + from);
message.println("To: " + to);
message.println();
message.println("Enquiry :-) from " + name + " at " + address);
message.println();
message.println("Details: "+ comments);
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
out.print("Email Sent Correctly");
%>
Your HTML is a bit garbled, but it looks like you're not specifying the "name" attribute for your inputs. The "id" attribute is good for referencing your field from a <label> or for accessing your inputs from Javascript, but the browser will not use it in the POST request.
The solution is simple, add the "name" attribute to your input elements, like this:
<input type="text" id="name" name="name" />