Atti Atti - 1 month ago 11
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>
<option>English</option>
<option>French</option>
<option>Dutch</option>
<option>Spanish</option>
</select>


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

<%

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

<% }%>


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

Answer

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 = 'http://www.google.com'; // 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;
Comments