Getting List values in jsp as request.getParameter("") from another jsp
a.jsp
List<String> abc =
(ArrayList<String>)request.getAttribute("studentsrollno");// got
studentsrollno from servlet by request.setAttribute();
<form action="b.jsp" method="post">
<input type="hidden" name="rollno" value="<%=abc%>"/>
<input type="submit">
</form>
b.jsp
<%
List<String> a2 = (ArrayList<String>) request.getParameter("rollno");//
getting error string cannot be converted to ArrayList<String> // How can I
display data rollno here ?
// Iterator<String> itr = a2.iterator();
%>
Many thanks in advance
You can retrieve the values as an array:
String[] vals = request.getParameterValues("rollno");
The values should be emitted as separate form parameters:
<c:foreach items="${abc}" var="item">
<input type="hidden" name="rollno" value="${fn:escapeXml(item)}" />
</c:foreach>
Related
I'm doing a java web application using jsp and servlets.
I have a form and I retrieve different values from my db to populate an option. This is my form:
<form class="form" action ="<%=request.getContextPath()%>/aggiungiLibro" method ="post">
<div class="form__group">
<input type="text" placeholder="Titolo" class="form__input" name = "titolo"/>
</div>
<div class="form__group">
<input type="text" placeholder="Quantità disponibile" class="form__input" name="quantita" />
</div>
<div class="form__group">
<select class="form__input" name="autore">
<c:forEach items="${listaAutori}" var = "autore" >
<option value="${autore.idAutore}"><c:out value="${autore.nome} ${autore.cognome}" /></option>
</c:forEach>
</select>
</div>
<button class="btn btn-light" type="submit">Inserisci nuovo libro</button>
</form>
In the servlet I retrive my data with
String titolo = request.getParameter("titolo");
int quantita = Integer.parseInt(request.getParameter("quantita"));
int autore = Integer.parseInt(request.getParameter("autore"));
I get right values for 'titolo' and 'quantita' but I get the string '${autore.nome}' for 'autore'.
It seems like it doesn't replace the string with the actual value.
The field idAutore is spelt right.
(In the jsp page I see the correct values for ${autore.nome} ${autore.cognome})
Anyone can help me please? What I'm doing wrong?
I solved replacing this:
<option value="${autore.idAutore}">
with this:
<option value="<c:out value='${autore.idAutore}' />">
Getting List values in jsp as request.getParameter("") from another jsp
a.jsp
List<String> abc =
(ArrayList<String>)request.getAttribute("studentsrollno");// got
studentsrollno from servlet by request.setAttribute();
<form action="b.jsp" method="post">
<input type="hidden" name="rollno" value="<%=abc%>"/>
<input type="submit">
</form>
b.jsp
<%
List<String> a2 = (ArrayList<String>) request.getParameter("rollno");//
getting error string cannot be converted to ArrayList<String> // How can I
display data rollno here ?
// Iterator<String> itr = a2.iterator();
%>
Many thanks in advance
You can retrieve the values as an array:
String[] vals = request.getParameterValues("rollno");
The values should be emitted as separate form parameters:
<c:foreach items="${abc}" var="item">
<input type="hidden" name="rollno" value="${fn:escapeXml(item)}" />
</c:foreach>
I am trying to call a HashMap value in jsp page but got error there and error says
There was an unexpected error (type=Internal Server Error, status=500).
For input string: "res"
My code;
<c:forEach items="${onlineExamList}" var="item"
varStatus="loop">
<div>
<b>${item.question1}</b><br>
<div class="radio">
<label><input type="radio" value="a"
name="answers[${loop.index}]">${item.option1}</label>
</div>
<div class="radio">
<label><input type="radio" value="b"
name="answers[${loop.index}]">${item.option2}</label>
</div>
<div class="radio">
<label><input type="radio" value="c"
name="answers[${loop.index}]">${item.option3}</label>
</div>
<div class="radio">
<label><input type="radio" value="d"
name="answers[${loop.index}]">${item.option4}</label>
</div>
<input type="text" name="rightAnswer"
value="${item.rightAnswer}">
//problem in this line
<c:if test="${result != null}">
<br>
<br>
<b>Your answer: ${result.get("res"+loop.index).get(1)}</b>
<br>
</c:if>
</div>
<hr />
</c:forEach>
And this is how i have set hashmap from controller
Map<String, List<String>> mapResult = new HashMap<String, List<String>>();
int totalScore = 0;
for (int i = 0; i < answers.answers.size(); i++) {
List<String> result = new ArrayList<>();
String res = "Wrong";
if (answers.answers.get(i).equals(answers.rightAnswer.get(i))) {
res = "Correct";
totalScore+=10;
}
result.add(res);
result.add(answers.answers.get(i));
result.add(answers.rightAnswer.get(i));
mapResult.put("res" + i, result);
}
ra.addFlashAttribute("result", mapResult);
ra.addFlashAttribute("score", totalScore);
Same things printed on java page
for (int i = 0; i < 10; i++) {
System.out.println(mapResult.get("res" + i).get(0));
System.out.println(mapResult.get("res" + i).get(1));
System.out.println(mapResult.get("res" + i).get(2));
System.out.println("...................................");;
}
How can I print hashmap value in jsp page?
Please check try this.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${currentLoggedInUsersMap}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
I solve the problem by concating the string using concat like this
${result.get("res".concat(loop.index)).get(1)}
The mapResult object is a map containing string arrays
Hashmap<String, List<String>>
you may need to first get the Array from the map object and then iterate over it.
this may help?
<table>
<c:forEach var="results" items="${resultsMap['res' + loop.index]}" varStatus="loop">
<tr>
<c:forEach var="answer" items="${results}" varStatus="status">
<td>
<td>$answer</td>
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
I have a dropdown which consist the language names. I am setting the value and displaying name of the dropdown by using a hashmap.
<form action="TextTranslation" method="post" class="form" role="form" >
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> </option>
<%
System.out.println((String)key);
}
String name = request.getParameter("country");
request.setAttribute("code", name);
%>
</select>
</div>
<input type="submit" class= "btn btn-image" value="Translate">
Search Text
</div>
</div>
</form>
Values are passed correctly to dropbox as it print all the values in console. the set attribute is accessed in the particular servlet. But it gives a null value. Do you have any idea?Thank you in advance
UPDATED
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> /option>
<% System.out.println((String)key);
}
String name = request.getParameter("country");
%>
</select>
<input type="hidden" name="code" value = <%= name%>/> .
In the servlet I used,
request.getParameter("code");
update your jsp likewise,
<form...>
...
<input type="hidden" name="code" value = <%= name%>/>
....
</form>
then get it from your servlet likewise,
request.getParameter("code"); // will return value of code
NOTE :
Remove from your jsp-code if above solution you gonna implement then,
request.setAttribute("code", name);
I am new to programming and this problem is bothering me for 3 days straight...
I have a post form on .jsp site for gathering name, surname, mail,... and all this info is saved in object USER. I want to save users in array and display them on the same site. But everytime I use submit button in form new session is created and on array output info is only one user.
What should I do to solve this problem?
ps: on this stage i can't use sql because it's school projects
<% Uporabnik uporabnik = new Uporabnik(); //user
uporabnik.setIme(request.getParameter("ime"));
uporabnik.setPriimek(request.getParameter("priimek"));
uporabnik.setEmail(request.getParameter("email"));
uporabnik.setKraj(request.getParameter("kraj"));
uporabnik.setPostnaStevilka(request.getParameter("postnaStevilka"));
ArrayList<Uporabnik> seznamUporabnikov = new ArrayList<Uporabnik>(); //array with i want to display
seznamUporabnikov.add(uporabnik);
session.setAttribute("seznamUporabnikov", seznamUporabnikov); %>
<form method="post" action="Registracija.jsp">
Ime: <input type="text" name="ime"/> <br/>
Priimek: <input type="text" name="priimek"/> <br/>
Email: <input type="text" name="email"/> <br/>
Kraj: <input type="text" name="kraj"/> <br/>
Postna stevilka: <input type="text" name="postnaStevilka"/> <br/>
<input type="submit" name="potrdi" value="Vnesi">
<input type="reset" name="tabelaReset" value="Izbrisi iz tabele">
<input type="submit" name="resetiraj" value="Izbrisi podatke">
</form>
<br/> Seja: <%=session.getAttribute("Oseba")%> <hr/>
<% if (request.getParameter("potrdi")!=null) {
session.setAttribute("Oseba", uporabnik);
} %>
<% if (request.getParameter("resetiraj")!=null) {
session.setAttribute("Oseba", null);
} %>
change these lines:
...
ArrayList seznamUporabnikov = new ArrayList(); //array with i want to display
seznamUporabnikov.add(uporabnik);
session.setAttribute("seznamUporabnikov", seznamUporabnikov);
...
to
...
ArrayList seznamUporabnikov=session.getAttribute("seznamUporabnikov");
if(seznamUporabnikov == null) { //check if already in session before creating.
ArrayList seznamUporabnikov = new ArrayList(); //array with i want to display
}
seznamUporabnikov.add(uporabnik);
session.setAttribute("seznamUporabnikov", seznamUporabnikov);
...
create a class and create a static user list under that class and add all users in that list
this list will be available throughout your application lifecycle.
Okay after 4 days this stuff works now!!! I am so happy :) anyway.. thank you guys for getting me on the right track...
ArrayList<Uporabnik> seznamUporabnikov=null;
//check if already in session before creating.
if(session.getAttribute("seznamUporabnikov") == null) {
seznamUporabnikov = new ArrayList<Uporabnik>();
//array which I want to display
session.setAttribute("seznamUporabnikov", seznamUporabnikov);
} else {
seznamUporabnikov =
(ArrayList<Uporabnik>)session.getAttribute("seznamUporabnikov");
}
if (request.getParameter("potrdi") != null) {
seznamUporabnikov.add(uporabnik);
}
session.setAttribute("seznamUporabnikov", seznamUporabnikov);