0

I tried to write a simple web application using servlet. when I tried to execute the frist page it get executed with the url "//localhost:8080/PassingParameter/ParamHtml.html" correctly. When I click the next button the url is changing too "localhost:8080/ReadParamUrl/*" In my servlet code is protected void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String title = "Reading All Form Parameters";
          String docType =
          "<!doctype html public \"-//w3c//dtd html 4.0 " +
          "transitional//en\">\n";
          out.println(docType +
            "<html>\n" +
            "<head><title>" + title + "</title></head>\n" +
            "<body bgcolor=\"#f0f0f0\">\n" +
            "<h1 align=\"center\">" + title + "</h1>\n" +
            "<table width=\"100%\" border=\"1\" align=\"center\">\n" +
            "<tr bgcolor=\"#949494\">\n" +
            "<th>Param Name</th><th>Param Value(s)</th>\n"+
            "</tr>\n");
          Enumeration<?> paramNames = request.getParameterNames();

          while(paramNames.hasMoreElements()) {
             String paramName = (String)paramNames.nextElement();
             out.print("<tr><td>" + paramName + "</td>\n<td>");
             String[] paramValues =
                    request.getParameterValues(paramName);
             // Read single valued data
             if (paramValues.length == 1) {
               String paramValue = paramValues[0];
               if (paramValue.length() == 0)
                 out.println("<i>No Value</i>");
               else
                 out.println(paramValue);
             } else {
                 // Read multiple valued data
                 out.println("<ul>");
                 for(int i=0; i < paramValues.length; i++) {
                    out.println("<li>" + paramValues[i]);
                 }
                 out.println("</ul>");
             }
          }
          out.println("</tr>\n</table>\n</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

The xml code is

<welcome-file>ParamHtml.html</welcome-file>
    </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ReadParam</display-name>
    <servlet-name>ReadParam</servlet-name>
    <servlet-class>org.param.ReadParam</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ReadParam</servlet-name>
    <url-pattern>/ReadParamUrl</url-pattern>
  </servlet-mapping>

The html code is

<form action="/ReadParamUrl" method="POST" target="_blank">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics"  /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chemistery
<input type="submit" value="Select Subject" />
</form>

I hope I have given the url correct, but it is not working. Please help me..

jona
  • 21
  • 1
  • 6

1 Answers1

1

If you want to use servlet-3.0 and @WebServlet then you must use Tomcat 7 or later.

Steve C
  • 18,876
  • 5
  • 34
  • 37