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
Related
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
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 the value of a variable that I retrieve from one servlets to another servlets. I don't want to use session. For example when I submit the form I want the value of nickname to pass to the second servlet.
This value Nickname
<li input type="hidden" name="nickname" value="${fn:escapeXml(obj.nickname)}">
Nickname: <c:out value="${obj.nickname}"/>
</li>
I use this link Stack Example
in your form you can set a hidden input which you pass to your second servlet:
<input name="nickname" value="${obj.nickname}" type="hidden"/>
This code is wrong, you can't have the word "input" randomly as a list attribute.. You also cannot have the type, name or value attribute on a list tag..
<li input type="hidden" name="nickname" value="${fn:escapeXml(obj.nickname)}">
Nickname: <c:out value="${obj.nickname}"/>
</li>
Check out the link here below, it shows the legal html tags you can use to send information with a form. You can only use these. So a list won't work.
https://www.w3schools.com/html/html_form_elements.asp
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 the followin question, how will I pass multiple values from one jsp page to another? I have i piece of code here, which works fine, but it only sends one value from one page to another (year):
<form method="post" action="Display data.jsp" name="inputpage" >
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
</select>
For example if I had another value, for example
String str = "value";
Is it possible to send it using form post method as well? I googled it, and the answer I found included loops and too much code, is there short and simple way of doing it? Many thanks!
When you submit the form all values of the form will be passed, they only need to be inside the form. You can read other values normally by using:
request.getParameter(ParamName)
Take a look at this article for more information
You can send as many variable you want by Form Method.
For sending the value of String Str, assign its value to hidden field as:
<input type="hidden" id="hidden1" value=<c:out value="${variableName}" />
where variableName=str.
Could you use a hidden input inside your form to pass other data using the form post?
<input type='hidden' id='myExtraData' value='hello' />