I have made a jsp page, and on the change of 1st dropdown, the values of the second dropdown must get changed accordingly.
Values of both the dropdown is fetched from the database.
Values of the 2nd dropdown is getting fetched as the value of the 1st dropdown is the foreign key in that table.
Using onchange event of dropdown 1 I am calling js function and passing it to the servlet to carry out the function.
I am very new to this concept and I am sure that I am making mistakes in my servlet code. Also I used firefox inspect, and according to it, the facno, is being transferred to the servlet, but there is no response from there and thats why the 2nd dropdown gets empty as soon as the value of 1st dropdown changes. Please help me to resolve the same.
JAVASCRIPT CODE:
function Fill() {
var facno = document.getElementById("facname").value;
var xhttp = new XMLHttpRequest();
xhttp.overrideMimeType("text/html;charset=UTF-8");
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
document.getElementById("subname").innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", "DropFill?facnoajax=" + facno, true);
xhttp.send();
}
JSP CODE:
DROPDOWN 1:
<%
Connection con = null;
con = DBConnection.createConnection();
PreparedStatement ps = null;
try {
String sql = "SELECT fac_no, fac_name FROM faculty_info";
ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
%>
<div class="form-group row">
<label class="col-form-label col-md-3" for="facname">Faculty:</label>
<div class="col-md-4">
<select name="facname" id="facname" class="form-control"
onchange="Fill()" required>
<option value="" selected>Choose any:</option>
<%
while (rs.next()) {
String facname = rs.getString("fac_name");
String facno = rs.getString("fac_no");
%>
<option value="<%=facno%>"><%=facname%></option>
<%
}
%>
</select>
</div>
</div>
<%
} catch (SQLException sqe) {
out.println(sqe);
}
%>
DROPDOWN 2:
<div class="form-group row" id="dd2">
<label class="col-form-label col-md-3" for="subname">Subject:</label>
<div class="col-md-4">
<select id="subname" name="subname" class="form-control" required>
<option value="" selected>Choose any:</option>
</select>
</div>
</div>
SERVLET CODE:
public class DropFill extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DropFill() {
super();
// TODO Auto-generated constructor stub
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
ArrayList<String> subname = new ArrayList<String>();
ArrayList<String> subno = new ArrayList<String>();
Connection con = null;
con = DBConnection.createConnection();
PreparedStatement ps = null;
String facno = request.getParameter("facnoajax");
try {
String sql = "SELECT sub_name FROM subject_info WHERE fac_no='" + facno + "'";
ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
subname.add(rs.getString("sub_name"));
subno.add(rs.getString("sub_no"));
}
for (int i = 0; i < subname.size(); i++) {
for (int j = 0; j <= subno.size(); j++)
response.getWriter().write("<select> <option value=" + subno.get(j) + ">" + subname.get(i)
+ "</option> </select>");
}
}
catch (SQLException sqe) {
out.println(sqe);
}
}
}
}