I have an input type which has a default value. I want to fetch its value inside of a servlet. So, which method will work in this case?
I tried using request.getParameter("FLC1"), but that didn't work as well.
Following is the input code:-
<input type="text" name="FLC1" id="FLC1" value="Floral Lavender Candle" disabled>
Thank you
Since your input element has a disabled attribute so it will not be submitted on form submit, instead make it a readonly and it should work.
<input type="text" name="FLC1" id="FLC1" value="Floral Lavender Candle" readonly>
Alternatively you can put a hidden filed in the form with same name attribute.
<input type="hidden" name="FLC1" value="Floral Lavender Candle">
You can:
Make the value server side an populate it in the jsp using el:
value="${myvariable}"
Store the value in a hidden field, it will be accessible then
Related
I've got a form where the inputted data gets mapped to the correct variable by using th:field. But when the page loads, I want the inputfield to show a default value. So that when the user does not enter any data, the default value gets mapped to the variable.
th:field overrides th:value, therefore I added:
th:attr="value=${field.placeholder}", which I found out about in another thread (link in comment), but this doesn't seem to work. When I inspect the element, it shows value as: value="".
I know field.placeholder isn't empty, because th:placeholder="${field.placeholder}" does show the value.
<form th:action="#{/generateData}" th:object="${stepObject}" method="post" class="form-group">
<div th:each="field, iterStat : ${stepEntry.value}">
<label th:text="${field.name}"></label><input type="hidden" th:field="*{name}" th:attr="value=${field.name}"/>
<input th:type="${field.type}" class="form-control" th:maxLength="${field.length}"
th:field="*{value}" th:attr="value=${field.placeholder}" th:placeholder="${field.placeholder}"/>
</div>
<button type="submit" class="btn btn-primary" id="generateButton">Generate away!</button>
</form>
So here in the hidden input it should have the value ${field.name}, but value is "".
in the other input it should have the value ${field.placeholder}, but this value also is "".
Any suggestions on why this is and how I could get this to work?
When I enter data in the inputfields, they get mapped to the value variable of stepObject. But it's important that the user can also just press the button without entering anything.
Thanks in advance!
I gave up on using the th:field and ended up just using the #RequestParam("form-name-attribute") in my controller method to get the (default) value and manually set it to the correct object.
I would've liked to do it with thymeleaf functionality though.
So if anyone still finds a solution for it, please let me know so I can learn! :-)
I want to pass values of two variables when a link is clicked to another page I am using query parameter but I am only able to send one variable through it. I know about session.setAttribute() but don't know how can I use it based upon links...Foreg:
<p> < </p>
<a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value);%>" class="classname1" > > </a>
This is my code I know its wrong..I just want is If I click on first link than value1 should be passed and If I click on 2nd link value should be passed.P.S.:I have already passes search variable through query parameter but now If I try to pass second parameter through session only the final value i.e second initialized value only counts? what to do?
EDIT:
suppose my code is this:
<form class="navbar-form navbar-right" action="Search.jsp" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
Here one variable search is passes through form How can I pass another variable value?should it be like:
<form class="navbar-form navbar-right" action="Search.jsp?item1=<%=value%>" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
You can send multiple parameters like,
href="Search.jsp?item=<%=search%>&item2=value2&item3=value3.."
Also to add <%session.setAttribute("val",value1);%> will be executed at server side irrespective of the click of the hyperlink.
For the form you can add another input parameter in the form,
<input type="text" name="item1" value="<%=value%>">
You need to add some separator in between two values e.g.#
And while reading at server side you can split those values based on that separator
You may try using this example to send multiple values:
With same name
With diff name
Separate the two values by using &:
<a href="Search.jsp?item=<%=search%>&item2=<%=value2%>">
On your search.jsp fetch values as :
request.getParameter("item");
request.getParameter("item2");
You need to call session.getAttribute in the place where you are calling session.setAttribute and session.setAttribute should be called in controller or in the jsp before the link tag to set the value. Please separate values like Search.jsp?item1=value1&item2=value2
I am assigning href to a button and passing a value to the controller
JSP page
<td><input type="button" name="remove" value="DELETE"/>
</td>
In controller, I am trying to get the value when the button is clicked using:
if(request.getParameter("remove") !=null)
{
int cart_id=(Integer)request.getSession(false).getAttribute("cartid");
b_id = (String) request.getParameter("book_id");
int bid=Integer.parseInt(b_id);
System.out.println(bid);
}
This prints a null value though I have passed book_id value in the URL.
I want to know how can I get the value passed in the URL via the button.
You can't combine [a] tag with an [input] tag like that.
Try using a form instead with hidden inputs:
<form action=viewController>
<input type=hidden name=remove value=delete>
<input type=hidden name=book_id value=<%=vo.getBookId()%>>
<input type=submit value='Delete'>
</form>
The resulting url will be : viewController?remove=delete&book_id=...
When submit button is pressed, the entire form will be sent. You can select where data will be sent by using attribute action:
<form action="demo_form.jsp">
<!--inputs here-->
<input type="submit">Send me now</input>
</form>
In that case form will be sent to demo_form.jsp. In HTML5 you can use formaction attribute if you want use different servlets for different buttons
Any way, you shouldn't use links for sending forms.
It is possible to use links without button:
Remove
I have a jQuery dialogue box which contains values as checkboxes. On selecting the checkboxes I am storing the selected values into label. Next I have to send these values from label as parameter through form to servlet but I don't know how to complete it.
Here is my code:
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date" size="4">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/>
Select Reporting Level
<label class="button2" style="display:none" id="depart"> Department</label>
</form>
I am retrieving these parameters in my Servlet as:
String reportname=request.getParameter("depart");
System.out.println(reportname);
But it is returning null values. Please help me.
Thanks in advance.
You have to use hidden input field:
<input type="hidden" name="depart" />
You need to understand what gets passed on form submission and what is not. In a nutshell, only values of the input fields get sent to the server. You have several ways to solve your problem:
Write value to a hidden input field
Modify the query string (what gets sent after ? in your GET request) during form submission (using java script):
?...&depart=xxx
Hi i have a servlet which get a parameter form a jsp on a submit button. One of the parameters is reporting to be null though. However this is not the case. The text input in question is filled automatically by a session variable and is definitely not null and can be seen in the text box on the page. But when inside the servlet the java console indicates that the variable is null? below is the code that populates the box and reads the parameter.
<input type="text" id="cID" value="<%= session.getAttribute("cID")%>" readonly="readonly">
reading the parameter:
String cID = request.getParameter("cID");
On printing cID to the console in netbeans it is reportedly null?
Add the name attribute to the input tag
<input type="text" id="cID" name="cID" value="<%= session.getAttribute("cID")%>" readonly="readonly">
It's the name attribute, not the id attribute, that defines the name of the parameter that's sent to the server. id is purely a client-side thing.
I think you need to write:
<input type="text" id="cID" value="<%= session.getAttribute(\"cID\")%>" readonly="readonly">