I'm working on a webapp with JAVA servlet backend and JSP frontend. This is my problem: in jsp page i have three select form, the options of this form are taken from the DB, so when i load the page the servlet take the info from DB and set attribute in the request and i fill the options of the form. What i would to do is, when i select an item in the first form, the option in the other two form change depending on the selected value. To do this my idea was to take the value of the first form make a POST request with javascript to the servlet, elaborate data, set the request attributes inside the servlet and forwarding to the same jsp. At this point i don't understand how to retrive the attributes i set, cause i don't really understand if the servlet is reloaded or not. I tried using javascritp with no result, i don't think this is strange case but i don't find an answer.
When the first select change this code is triggered:
function postJSON(data){
try {
const response = fetch("<%=contextPath%>/Dispatcher?controllerAction=RegistrationManagement.matchComuni",{
method: "POST",
body: data,
}).then((response)=>{
console.log(response.status);
});
} catch (error) {
console.error("Error: ", error);
}
}
This is part of method that elaborate data:
public static void matchComuni(HttpServletRequest request, HttpServletResponse response){
DAOFactory daoFactory= null;
Logger logger = LogService.getApplicationLogger();
List<Regione> regioni = new ArrayList<Regione>();
List<Citta> listaCitta = new ArrayList<Citta>();
List<Provincia> province = new ArrayList<Provincia>();
String selectedRegioneJSON;
String regioniJSON;
String listaCittaJSON;
String provinceJSON;
try {
daoFactory=DAOFactory.getDAOFactory(Configuration.DAO_IMPL, null);
daoFactory.beginTransaction();
RegioneDAO regioneDAO = daoFactory.getRegioneDAO();
regioni = regioneDAO.findRegioni();
ObjectMapper mapper = new ObjectMapper();
regioniJSON = mapper.writeValueAsString(regioni);
selectedRegioneJSON = request.getParameter("selectedRegione");
Regione selctedRegione = mapper.readValue(selectedRegioneJSON, Regione.class);
CittaDAO cittaDAO = daoFactory.getCittaDAO();
listaCitta = cittaDAO.findCittaByRegione(selctedRegione.getIdRegione());
listaCittaJSON = mapper.writeValueAsString(listaCitta);
ProvinciaDAO provinciaDAO = daoFactory.getProvinciaDAO();
province = provinciaDAO.findProvinceByRegione(selctedRegione);
provinceJSON = mapper.writeValueAsString(province);
request.setAttribute("regioniJSON", regioniJSON);
request.setAttribute("listaCittaJSON", listaCittaJSON);
request.setAttribute("provinceJSON", provinceJSON);
request.setAttribute("viewUrl", "/registration/registration");
at this point in the servlet i received the correct data and all the elaboration are fine so the data are send to the jsp with request.setAttribute(), but i don't find how to read the new data. I don't understand if at this point the jsp is completely reloaded or partial reloaded or the same as before the POST request.
I tried to read the new data in javascript with:
let var = '<%(String)request.getAttribute(provinceJSON)%>';
but return null
let urlParam = new URLSearchParams(window.location.search);
let paramJSON = urlParam.get("provinceJSON");
return null
let urlParam = new URLSearchParams(response.url);
let paramJSON = urlParam.get("provinceJSON");
return null
As i said before i don't think this is a strange scenario, but i don't find a solution, i hope in yours help.
P.S. I'm sorry for my bad english it isn't my first language.
Consider returning a JSON response from the servlet instead, which can be parsed after fetching. The jackson-dataformat-xml Maven dependency can be used to convert a Java object to JSON.
In the servlet:
var objectMapper = new ObjectMapper(); // from Jackson
response.setContentType("application/json");
var writer = response.getWriter();
var result = Map.of(
"regioni", regioni
// and anything else to send back
);
writer.print(objectMapper.writeValueAsString(result));
writer.flush();
On the frontend:
fetch('/Dispatcher?controllerAction=RegistrationManagement.matchComuni', {
method: 'POST',
body: data
}).then(res => res.json()).then(data => {
console.log(data);
// access the values, e.g. data.regioni
});