Atti Atti - 3 months ago 19
Java Question

Refreshing Page With A Parameter in JSP

I have a JSP page in my web application and i want to refresh the page with the parameter of option.

<select class="form-control combobox" name="education" >
<option value="" disabled selected hidden>Choose an education</option>

I want to use this parameter for querying my database again. Forexample;


BookDAO bdao = new BookDAO();
for (TblBooks book : bdao.getAllBooks()) {%>
<td><input type='checkbox' name ='chk1' /></td>

<% }%>

I can get the parameter with request.getParameter("education") but how can i refresh page and query again with this parameter?


In Javascript, You can ask a web page to reload itself (the exact same URL) by:

location.href = location.href;

You can ask a web page to load a different location using the same mechanism:

location.href = ''; // or whatever destination url you want

You can get the value of a select control (which needs to have an id attribute):

var ctrl = document.getElementById("idOfSelectControl");
var selectedOption = ctrl.options[ctrl.selectedIndex].value;

So, if you update your JSP to give the control an id:

<select class="form-control combobox" id="education" name="education" >

you should be able to reload the page:

var educationControl = document.getElementById("education");
var selectedOption = educationControl.options[educationControl.selectedIndex].value;
location.href = "http://mysite.example/mypage?education=" + selectedOption;