Passing Data from JSP to Servlets from HTML page dynamically - java

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

Related

get value of attribute outside form tag from jsp to servlet

i have one view where i m showing many records and now i m adding functionality for user to download those records which he will choose date from search textbox. As my search textbox in jsp outside of form tag and thus i m not getting the value of that parameter in my servlet..is there any way to get that value from outside of form tag? Here is my jsp
<div id="divOfDateTable">
Search:<input type="text" name="dropdown" id="datedropdown">
<button id="dateButton" name="dateSearch">Search</button>
</div>
<form action="Download_Servlet" class="download" method="post">
<input type="submit" id="downloadRecords" value="Download Order-records">
</form>
In my Servlet i want to get that parameter value.i.e;Date put by user.As per the requirement only the searched records by the user needs to be downoaded. So please guys help out..

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

How to get form name value in JSP

Is there a way to get the name of the form element itself using JSP? I searched google and did not find any examples which show how to get the name of the form value specified in the JSP file.
For example, I have the below form,
<html>
<form name="register" action="servregister" method="POST"
onsubmit="return validate();">
</form>
</html>
I would like to get the string "register" to a string variable in JSP. Is there a way to do this?
No, a <form> is not submitted with the request. Instead create a hidden input element which holds that information.
<input type="hidden" name="name of form" value="value" />
or possibly the submit element
<input type="submit" name="distinguishing name" value="submit" />
Either of these in a form with be sent as a url-encoded parameter.
There is probably a better solution to what you are trying to do if you explain your goals.
Consider looking into different patterns for achieving your goals.

form values not submitted to the servlet

I am trying to submit the text field value and print it using the servlet. The index.jsp is my main page and I am using jsp:include to include the form which reside in another page which is login.html.
here is the code i have for login.html
<form id="f1" action="ControllerServlet" method="GET">
<p>username
<input class ="text-input" type="text" id="txtusername" />
</p>
<p>
<input type="submit" value="submit" />
</p>
the index.jsp
<div id="col3_content" class="clearfix">
<h1>H1 Heading</h1>
<jsp:include page="login.html"></jsp:include>
</div>
the controller servlet
String usrname = request.getParameter("txtusername").toString();
out.print(usrname);
The problem is this is throwing a null pointer exception. what am I doing wrong here ? any help appreciated. thanks
Please use name not id
<input class ="text-input" type="text" name="txtusername" />
The id is not used to identify the name of the input parameter. The right attribute for the parameter is name, currently you are using an input without a name. So use
<input class ="text-input" type="text" name="txtusername" id="txtusername" />
You need to define name attribute of input tag to get it in Servlet by name.
<input class ="text-input" type="text" id="txtusername" name="txtusername" />
Also make sure you are writing code in doGet or service method of servlet as you have GET as action in form tag.
Code for Login.html
<form action="ControllerServlet" method="GET">
<p>username :
<input type ="text" name="txtusername" /></p>
<p><input type="submit" value="submit" /> </p>
</form>
ControllerServlet.java
public void service(ServletRequest request, ServletResponse response)
{
String username = request.getParameter("txtusername");
PrintWriter out = response.getWriter();
out.println("User Name " + username)
I faced a similar situation, when I checked front end, the form seems to have all the value populated correctly. However, after form.submit, from server side using request.getParameter("the parameter") does not return the value populated. After tuning on the network traffic tab in browser, I see the parameter was there, but there was a typo.
Hopefully could save you some time if same thing happens to you.

Categories