@catServlet注释与tomcat 6

时间:2014-06-07 06:06:32

标签: servlets servlet-filters servlet-3.0

我尝试使用servlet编写一个简单的Web应用程序。当我尝试执行frist页面时,它会使用url" // localhost:8080 / PassingParameter / ParamHtml.html"执行。正确。当我点击下一个按钮时,网址也会改变" localhost:8080 / ReadParamUrl / *" 在我的servlet代码中    protected void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出

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);
    }

}

xml代码是

<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>

html代码是

<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>

我希望我已经正确地提供了网址,但它无效。 请帮帮我..

1 个答案:

答案 0 :(得分:1)

如果要使用servlet-3.0和@WebServlet,则必须使用Tomcat 7或更高版本。