-2

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?

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
tracer608
  • 1
  • 1
  • 2
    You must be looking for [`ServletRequest#getParameterMap()`](http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getParameterMap()). – sp00m Dec 23 '14 at 09:46
  • 2
    There is big difference between parameter and attribute. Read [here](http://stackoverflow.com/questions/5243754/difference-between-getattribute-and-getparameter) and [here](http://www.xyzws.com/servletfaq/what-is-the-difference-between-the-request-attribute-and-request-parameter/1) – Braj Dec 23 '14 at 09:57

2 Answers2

1

You should use getParameterNames() instead of getAttributeNames.

Enumeration parameterNames = request.getParameterNames();

For more information to know difference between Attributes and Parameters see this question.

Community
  • 1
  • 1
Ali Sepehri.Kh
  • 2,468
  • 2
  • 18
  • 27
1

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().

sumit kumar
  • 602
  • 3
  • 11
  • 26