request.getParameter() says its null when not - java

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

Related

How to fetch default value from an input into a servlet?

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

Passing Data from JSP to Servlets from HTML page dynamically

I have a JSP page which reads data from HTML and has CSS,Jquery code in it .
Now my webpage in jsp has two text labels and a add button next to them.
User can enter any no of values in the text field.
Now my requirement is that every time user enters the alue in these fields and clicks on add then that data should be passed on to my servlet. Servlet will basically do some validation and return a boolean variable.
Based on the value of this boolean, I shall change the appearance of my text boxes.
This is required to be done for every time user clicks on Add button.
How can I achieve this ?
My HTML code :
<div id="id1" name="id1" style="display: none;">Add a node: </br>
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP"> <input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="button" value="Add" name="addnodebutton" id="addnodebutton"/>
</div>
The value in ipaddress and port shall be passed on to my servlet and depending on return parameter, their appearance should change.
Can anyone enlighten me how this is actually going to work ?
TIA :)
For passing data to and from a servlet, you have options.
Option 1- You can wrap your html in a form tag and set the action/method properties for your servlet/http method like below:
<form method="POST" action="servletname">
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP">
<input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="submit" value="Add" name="addnodebutton" id="addnodebutton"/>
</form>
The submit would send a request with the input to your servlet. You would then need to handle your request parameters in your servlet, set your values/flags in your response object and forward to the user or jsp/html page of your choice.
Option 2- You can make an ajax call from your jsp, process your input and return a response to your page asynchronously. Example below:
A Simple AJAX with JSP example

JSP pass hidden input value to servlet when the page load

I should be using getRemoteUser functionality to get the logged in user. Until the authentication part get created I am trying to hard code the user in the jsp page and pass that in the my servlet. But when I try to print out the value its null:
<input type="hidden" name="userId" id="userId" value="123456789" />
Here is how I tried to get the user:
String fakeUser = request.getParameter("userId");
PrintWriter out = response.getWriter();
out.println(fakeUser);
System.out.println(fakeUser)
I also tried the solution mentioned following Stackoverflow post but that didn't work either.
passing value from jsp to servlet
As you are trying to use hidden-form field I assume that you are trying to do some sort of state management.
try something like this
<form action="urlOfYourServlet" method="post">
Enter your name : <input type ="text" name = "name">
<input type="hidden" name="hidden" value="Welcome">
<input type="submit" value="submit">
</form>
In servlet
String getHiddenValue=request.getParameter("hidden");
String name=request.getParameter("name");
System.out.println(name+" Hidden field Value is :"+getHiddenValue);
Disadvantage :
Only textual information can be persisted between request.
This method works only when the request is submitted through input form
Instead try url-redirecting or HttpSession

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

How to send label parameter from JSP to servlet?

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

Categories