0
votes

I want a combo box in a jsp file to display a arraylist pass by a servlet but right now it is displaying the whole arraylist in one line of the combo box. the servlet code

  ArrayList<String> list = new ArrayList<String>();
    list.add("A");
    list.add("B");
    list.add("C");
response.setIntHeader("default", 5);
request.setAttribute("name",":Motorola Solutions TC55");
request.setAttribute("item",list);
request.getServletContext().getRequestDispatcher("/default.jsp").forward(request, response);

the jsp code

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Include Manufacture name</title>
</head>
<body  >
<p id ="L1">Name :- <%=request.getAttribute("name")%></p>
<select>
<c:forEach items="${item}" var="temp">
    <option value='${temp}'>${temp}</option>
</c:forEach>
</select>
<button type="button" >Change Content</button>
</body>
</html>

i added <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> now t The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

2
you should add jstl jar to your buildpath .Santhosh

2 Answers

2
votes

You have mis-typed the name of the arraylist,

<select name='listSelect'>
<c:forEach items="${item}" var="temp">
    <option value='${temp}'>${temp}</option>
</c:forEach>
</select>

To avoid the errors like above , you should follow naming conventions as per standards

-1
votes

The bug is here:

request.setAttribute("item",list);

it should be

request.setAttribute("items",list);

On top of that I think you can't use the ${items} construct on request parameters. You've not done that for the "name" attribute - for that you've read it using request.getAttribute so you'll need to do that in your foreach