Trying to get value of an option field in Java - java

I'm having a bit of an issue with getting the value of a select in java.
This is the code inside of my java controller:
String topicName = request.getParameter("select_topic");
This is the code inside of my JSP file:
<select class="topic-form" name="select_topic" style="height:40px; " id="select_topic">
<option>Select topic</option>
</select>
I'm generating options for the select dynamically and then trying to get the selected option inside of java. The problem is I tried to see what the code in java returns and it's a "null" value. I've also made the jsp return the values inside the console to check if the values are right and they are. So all I can think of is that the value somehow doesn't get taken by topicName. I have also checked to see if the function gets called and it does.

Lets say your JSP page with the form is called "topic.jsp" :
Make sure that your <form> tag has an action param (ex. action="topic.jsp")
Make sure that each of the <option> tags have value param. (ex <option value="-1">Select</option>
Example:
<form action="topic.jsp" method="get">
<select class="topic-form" name="select_topic" id="select_topic">
<option value="-1">Select topic</option>
<option value="1">Topic 1</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
Debugging :
It is helpful to set form method to GET so the request parameters will be included in the address bar.
HttpRequest object has a couple useful methods:
request.getQueryString(); // if (request.getQueryString() != null) System.out.println("QS: " + request.getQueryString());
request.getParameterMap() // Map<String,String[]> (Key = form object name, Value[] = respective values)

Related

Use value of a local variable as global in thymeleaf

I have a variable that is obtain inside each block in thymeleaf. I want to send that variable to a certain method in my controller. The limitation is that the variable is obtain inside a block which makes it local, not accessible global.Hence I get an error while trying to use it. How can I move inside the scope to get the variable so as I can use it global in thymeleaf.
<form th:action="#{/masomo/somo(date=${dateMpya.date})}" method="POST">
<select id="date" name="date" required="true">
<option value="none" selected disabled hidden >
Select a Date
</option>
<th:block th:each="somoChagua : ${masomoChagua}">
<option th:each="dateMpya: ${somoChagua}" th:value="${dateMpya}" th:text="${dateMpya.date}" ></option>
</th:block>
</select>
<button type="submit"><i class="fa fa-search"></i> </button>
</form>
There can be many different "dateMpya" objects for each "somoChagua".
But there is only one submit button.
So which "dateMpya" should be used for the submit button value?
I think what you are actually trying to do here is get the value of the "dateMpya" which the user selected in the drop-down. Is that right?
If that's the case there is no need to add any attributes to the submit button. You would access that value by using the name of the select element, which is "date".
EDIT: For the same reason you also need to remove the (date=${dateMpya.date}) part of the form action as well. The value selected in the drop-down will automatically be submitted under the name of the select element "date", it does not need to be specified.

Filling the html form fields through servlets

So the question is like we manipulate the values of the form fields through servlets by using request.getParameter() can we do the other way round i.e set the form feild values fromthe servlet itself?
Basically what I am trying to do is to create a dropdown and some textboxes. the dropdown gets the ids from the database , when user selects a particular id, the text boxes should get filled with the other values from database for that id so the user can either edit them or leave unchanged accordingly.
For ex: The html code is:
<form action="GetValues">
<select name="ids"><option>1</option><option>2</option></select>
<input type="submit" value="Edit">
</form>
<form action="Save">
Product name:<input type="text" name="name" id="tb1"/></br>
Price:<input type="text" name="price" id="tb1"/>
<input type="submit" value="Save">
</form>
The Getvalues servlet establishes the database connection and gets the values of name and price from the datbase which I can do, but how to display those values in the two textboxes?
I can than make the Save.java servlet to get the values from textfields and update into the database.
I am not at all comfortable using JSP scriptlets. I want to do this using servlets only.
I know I could have created textboxes using the servlet itself but that won't work for me because that makes my jquery on the form die.
Like we do in javascript:
var x="hii";
document.getElementById("tb1").value=x;
Is there anything like this in Java too?
I know you requested no scriptlets, but IMO this is the easiest way to do it.
In your servlet, set a request attribute as such:
request.setAttribute("attributeName", attributeValue);
Then in your jsp, you can access the attributeValue like this:
<%= request.getAttribute("attributeName") >
Edit: For the followup question in the comment, here is how you can show the IDs in the select box using scriptlets:
First, set the list of IDs in the servlet:
List<String> idList = ...;
request.setAttribute("idList", list);
Then in your JSP, construct the select field as follows:
<select name="ids">
<%
List<String> idList = request.getAttribute("idList");
for(String id : idList) {
%>
<option><%=id></option>
<%
}
%>
</select>
. the dropdown gets the ids from the database , when user selects a particular id, the text boxes should get filled with the other values from database for that id so the user can either edit them or leave unchanged accordingly.
That is the perfect place to use a AJAX call. Please fire a AJAX request and get the values from Database.

unable to retrieve the passed value from jsp to controller using url via a button

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

Passing multiple values from one page to another

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' />

request.getParameter() says its null when not

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">

Categories