listCurrencies thinks that it is a string in jsp code but request attribute has it as an ArrayList Type... the foreach therefore doesn't work. Help!
Servlet Method
protected void listCurrency(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
currencies = new Currencies();
ArrayList<String> c = (ArrayList<String>) currencies.getCurrencyList();
request.setAttribute("listCurrencies", c);
RequestDispatcher dispatcher = request.getRequestDispatcher("/BrokerIndex.jsp");
dispatcher.include(request, response);
}
JSP Code
<form action="brokerServlet" method = "Post">
<select name="currency">
<c:forEach items="${listCurrencies}" var="cur">
<option value="${cur}"
<c:if test="${cur eq selectedCurId}">Currency</c:if>
>
${cur} </option>
</c:forEach>
</select>
<input type="submit" value="Submit" />
Related
How to assign an arraylist to select an option in jsp? I found an example(how to assign arraylist to select option in jsp) but still get empty options.
IndexServlet.java
#WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<String> databaseArrayList = new ArrayList();
databaseArrayList.add("1");
databaseArrayList.add("2");
databaseArrayList.add("3");
request.setAttribute("databaseList", databaseArrayList);
request.getRequestDispatcher("AddManager.jsp").forward(request, response);
System.out.println("in index servlet");
}
}
JspCode
<tr>
<td>Subordinate employees:</td>
<td>
<select name="database1">
<c:forEach items="${databaseList}" var="databaseValue">
<option value="${databaseValue}">
${databaseValue}
</option>
</c:forEach>
</select>
</td>
</tr>
You are not forwarding the request..
RequestDispatcher rd=request.getRequestDispatcher("/AddManager.jsp");
rd.forward(request, response);
I have a JSP that has a form that looks like this:
<form method="GET" action="ManagerLogicServlet?action=salesreport" >
<select name="monthList">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input type="submit" value="Submit">
</form>
I am trying to send over a query string with attribute action = salesreport which will be a condition that will return a sales report for the selected month (don't mind the missing default value). I submit the form over to the ManagerLogicServlet which has this code snippet:
..String action = request.getParameter("action");
if (action.equalsIgnoreCase("salesreport")){
forward = SALES_REPORT;
int month = Integer.parseInt(request.getParameter("monthList"));
String monthString = new DateFormatSymbols().getMonths()[month-1];
request.setAttribute("monthString", monthString);
request.setAttribute("salesReport", salesDAO.getSalesReport(month));
} else if..
But the action attribute is set to null. Why is this?
Because your form is using the GET method, the parameters from the action attribute are being discarded. If you insist on using GET, then you can include an <input> tag containing the parameter you wish to pass on to the servlet. Try doing this:
<form method="GET" action="ManagerLogicServlet?action=salesreport" >
<input type="hidden" name="action" value="salesreport">
<select name="monthList">
<option value="1">January</option>
...
</select>
<input type="submit" value="Submit">
</form>
The alternative would be for you to leave your code as is, but change the form's method to POST.
Its working fine.
I tried this
HTML
<form action="AnyServlet?action=salesreport" method="post">
<input type="submit" value="Submit Data" />
</form>
AnyServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
System.out.println("action=="+action);
}
Output
action==salesreport
UPDATE
When i changed from "post" to "get",I am getting issue too.You can use hidden input field if you want go with "get".
I am trying to print checked values of checkbox list from a JSP page but nothing appears even if some selections are there.
<form action="process" method="POST">
<c:forEach var="item" items="${list.items}">
<input type="checkbox" name="chkSkills" value="${$item.Id}">${item.name}
</c:forEach>
<input type="submit" name="Getvalue" value="Get value" />
</form>
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String[] Answers = request.getParameterValues("chkSkills");
PrintWriter out = response.getWriter();
String ButClicked = request.getParameter("Getvalue");
if (ButClicked != null) {
for (String Answer : Answers) {
out.print(Answer + "<br>");
}
}
//processRequest(request, response);
}
Correct your value attribute to
value="${item.Id}"
Notice, there's no need to put a $ inside the {} again.
Am trying to retrieve the data submitted from the HTML from in a servlet.Am using netbeans and am trying to get the enumeration returned by getAttributes().
The html is
<html>
<body>
<center>
<form name="Form1" action="http://localhost:8080/DemoWeb/TwoParServlet">
<B>Color:</B>
<select name="color" size="1">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
<br>
<br>
<B>Country:</B>
<select name="country" size="1">
<option value="India">India</option>
<option value="Srilanka">Srilanka</option>
<option value="Chinae">China</option>
</select>
<br>
<br>
<input type=submit value="Submit">
</form>
</center>
</body>
</html>
The servlet is
public class TwoParServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String color = request.getParameter("color");
String country = request.getParameter("country");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<p>The selected color is: ");
pw.println(color);
pw.println("<p>The selected country is: ");
pw.println(country);
Enumeration names;
names = request.getAttributeNames();
pw.println("<p> First value received = " + names.nextElement());
//pw.println("<p> First value received = " + names.nextElement());
pw.close();
}
}
When I run the project am getting this error
Exception report message description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.util.NoSuchElementException
java.util.HashMap$HashIterator.nextEntry(HashMap.java:929)
java.util.HashMap$KeyIterator.next(HashMap.java:960)
java.util.Collections$2.nextElement(Collections.java:3665)
p1.TwoParServlet.doGet(TwoParServlet.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
What might have gone wrong?
You Just check the size of names, it will be zero.
so it means there is nothing returned from request.getAttributeNames().
use request.getParameterNames() instead of request.getAttributeNames().
You should use getParameterNames() instead of getAttributeNames.
Enumeration parameterNames = request.getParameterNames();
For more information to know difference between Attributes and Parameters see this question.
I'm doing a small JEE5 application similar to an online shop and I need to show the shop products, it can be seen more clearly in the code:
<c:forEach var="product" items="${productsBean.products}">
<div class ="product">
<table>
<td>
<c:out value="${product.id}"/>
</td>
<td>
<c:out value="${product.name}"/>
</td>
<td>
<c:out value="${product.price}"/>
</td>
<td>
<c:out value="${product.description}"/>
</td>
<td>
<form method="post" action="/servlets-war/AddToCart">
<input type="hidden" name="id" value="${product.id}"></input>
<input type="submit" value="add to cart"/>
</form>
</td>
</table>
</div>
</c:forEach>
The problem is that the hidden input value in the form returns null instead of the ${product.id} when the form is submitted.
The associated servlet code is the next:
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
ShoppingCart shoppingCart = (ShoppingCart)request.getSession()
.getAttribute("shoppingCart");
if(shoppingCart == null){
shoppingCart = new ShoppingCart();
}
ProductsBean store =(ProductsBean) request
.getSession().getAttribute("productsBean");
int id = Integer.parseInt((String)request.getAttribute("id"));
Product temp = store.getProduct(id);
shoppingCart.getProducts().add(temp);
request.getSession().setAttribute("shoppingCart", shoppingCart);
this.getServletContext().getRequestDispatcher("/shop.jsp")
.forward(request, response);
}
You are retrieving the id as a request attribute.
int id = Integer.parseInt((String)request.getAttribute("id"));
Form input is sent to the servlet as a request parameter. Change your code appropriately:
int id = Integer.parseInt((String)request.getParameter("id"));