Filling the html form fields through servlets - java

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.

Related

Pass data from one jsp to another and display that jsp view

Hey i am a php developer and this is my first go with jsp. Now i retrieved a Json string from my class and converted it into GSON. I display a field in my result.jsp for eg:- ID and on clicking the id it should go to details.jsp and show more info about that ID
Currently my result.jsp is as follows:-
<html>
<body>
<div class="list-group">
<%
String json = (String)request.getAttribute("jsonstring");
Gson gson = new Gson();
ConCom diff = new ConCom();
diff = gson.fromJson(json, ConCom.class);
List<ComparisonResultDTOarr> ls = diff.getComparisonResultDTOarr();
for(int i = 0;i<ls.size();i++)
{
List<AuditItemLogsDTOArr> lsinner = ls.get(i).getAuditItemLogsDTOArr();
%><a href="#" class="list-group-item">
<%out.println(lsinner.get(0).getKeyAsString());%></a><%
}
%>
</div>
</body>
</html>
I read around SO and googled it and understood that that i could make a hidden form. Now I create a form with the following two fields and using the anchor tag i submit the form. But the values in the form need to be posted according to the ID clicked, how can i make that dynamic?
So if my form is as follows:-
<form action="details.jsp" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="phone">
</form>
And my details.jsp will be like:-
<%= request.getParameter("firstname") %>
<%= request.getParameter("phone") %>
I want the firstname and phone to be set according to the ID clicked and the form to be submitted. I can obtain the String/Integer value from my diff object in this page. Would i need to use JQuery? Any help?
Also i know i should be using JSTL. And i will get to that soon. Thank you.
You could call a javascript function during the onclick event of your anchor tag:
<a href="#" onclick="submitHiddenForm("<%=lsinner.getFirstName()%>", "<%=lsinner.getPhone()%>");">...
Your JS function would like like:
function submitHiddenForm(firstName, phone) {
document.getElementById("firstname").value = firstName;
document.getElementById("phone").value = phone;
// attach a name attribute to your form tag
// submit the form
document.myForm.submit();
}
I hope this helps.
EDIT: changed diff to lsinner, since that's the var used in the loop.
On click of the ID call a javascript function passing the values inside the function you can dynamically set the values of the form by getting each element like document.getElementById("firstname").value=value passed similarly set the other fields and in the end document.myform.submit();
Note since we are fetching HTML elements by Id you can use
<form action="details.jsp" method="post" name="myform">
First name: <input type="hidden" name="firstname" id="firstname"><br>
Last name: <input type="hidden" name="phone" id="phone">
</form>
type="hidden" will hide the elements.
check these for better understanding.
How to submit a form using javascript?
If you have the diff object in details.jsp as well, it should be enough to pass only the ID as a parameter, in a normal link.
details
You'd probably do the same in PHP.
JQuery is client side JavaScript and not required to solve your problem.

How to pass value of more than one variable in jsp?

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

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

Sending values from one page to multiple pages JSP

In 1.jsp I have this input boxes
<input type="hidden" id="muncId" name="muncId"/>
<input type="hidden" id="muncDesc" name="muncDesc"/>
I want to send the values in 2.jsp, 3.jsp, 4.jsp etc.
The form submit is for one page only. How should I do that in multiple pages?
the other alterante for hidden form field is to use session.Kepp the values in session
In 1.jsp keep the values in session like this
session.setAttribute("value","hi");
now in other pages 2.jsp,3.jsp,4.jsp retrive the values from session like this
session.getAttribute("value");
In your 1.jsp page include one form for each target page containing the same hidden fields.
Add a little JavaScript to your submit button to submit all forms on click.

how to add values into dropdown in java script

I saw a sample code in this website to dynamically populate second dropdown men based on a selection of first dropdown. I have account number dropdown and based on the selection, I need to populate second dropdown with corresponding email-id.
code is here :Populating Child Dropdown (second option using java script).
But can someone let me know how to populate the values inthe second dropdown ?
<script>
var dd2options = ${dd2optionsAsJSObject};
var dd3options = ${dd3optionsAsJSObject};
function dd1change(dd1) {
// Fill dd2 options based on selected dd1 value.
var selected = dd1.options[dd1.selectedIndex].value; ... }
</script>
My code is as below- how to change the code to do this ?
<td>
1. Member Account Number
<span class="bodyCopy">
<font color="#ff0000"> * </font>
</span>:
<html:select
name="DataForm"
property="Member.accountNumber"
styleClass="formContent"
style="width:80px">
<html:options collection="<%= WorkConstants.RENewDropdowns.PACCT %>"
property="value"
labelProperty="label"
styleClass="formContent"/>
</html:select>
</td>
My second dropdown is as below:
<td>
3. Member <br>E-mail Address:<br />
<span class="bodyCopy"></span>
<html:select
name="DataForm"
property="Member.emailAddress.emailAddress"
style = "width:150px"
styleClass="formContent">
<html:options collection="<%= WorkConstants.RENewDropdowns.PEMAIL %>"
property="value"
labelProperty="label"
styleClass="formContent"/>
</html:select>
</td>
Appreciate your help on this as I'm new to java script.
Problem can be solved in following ways
Using onchange event of select element, on first box submit form and populate data in second select box.
Use ajax to fetch data in the same way instead of submitting form
Fetch all the main and related data, store them in arrays. Call them according to key. You can use JSON to solve

Categories