0

Friends please tell how to add list of array values in drop down list using java servlet.

Please check the below code and tell any error in this code..

My Jsp Page.... DropDown.jsp

<body onload="callAction()">
<form>
<script type="text/javascript">
    function callAction() {
        document.location.href="http://localhost:8084/Servlet/DropDown";
    }
</script>

<select name="Day"><option>Day</option>
    <c:forEach items="${Data}" var="d">
        <option value="${d}">${d}</option>
    </c:forEach>      
</select>
</form>
</body>

My Servlet page DropDown.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
int[] day=new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

    request.setAttribute("Data", day);

 response.sendRedirect("DropDown.jsp");
}

2 Answers 2

1

EDIT

servlet

int[] day=new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
request.setAttribute("Data", day);//<-- no loop required !
//response.sendRedirect("DropDown.jsp");//<-- this will make a new request
request.getRequestDispatcher("DropDown.jsp").forward(request, response);//<-- forward it

DropDown.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<select name="Day">
    <option>Day</option>
    <c:forEach var="d" items="${Data}">
      <option value="${d}">${d}</option>
    </c:forEach>
</select>
Sign up to request clarification or add additional context in comments.

5 Comments

@Aravind I am not getting the values.. Plz check the code, now I updated the full code.. Please check and reply for that..
@Aravind.. Currently nothing displaying on the browser.. Please check the javascript and tell any error on this..
@ShajiSS, remove that script tag from jsp, not needed, tell me do you have only this servlet and jsp in your web-app? here your servlet needs to be invoked before jsp
@ShajiSS, jsp cannot render values from the request attributes after page load, still if you need in that way you will need ajax
@ShajiSS, kindly accept the answer, as it worked for you
0
for(int i=1;i<=day.length;i++)
    {
        request.setAttribute("data", day[i]);
    }       

it will override the data attribute and replace a new value. just set attribute with the array.

request.setAttribute("data",day);

then in your select use JSTL and import the jstl tag library

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<select name="Day">
   <c:foreach items="${data}" var="d">
    <option>${d}</option>
    </c:foreach>
</select>

1 Comment

it will not take effect because you are using response.redirect. you must use requestdispatcher

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.