【问题标题】:@WebServlet annotation with tomcat 6带有tomcat 6的@WebServlet注释
【发布时间】:2014-07-28 11:01:42
【问题描述】:

我尝试使用 servlet 编写一个简单的 Web 应用程序。当我尝试执行第一个页面时,它会使用 url "//localhost:8080/PassingParameter/ParamHtml.html" 正确执行。当我单击下一个按钮时,网址也在更改“localhost:8080/ReadParamUrl/*” 在我的 servlet 代码中是 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);
    }

}

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>

我希望我提供了正确的网址,但它不起作用。 请帮帮我..

【问题讨论】:

    标签: servlets servlet-filters servlet-3.0


    【解决方案1】:

    如果您想使用 servlet-3.0 和 @WebServlet,那么您必须使用 Tomcat 7 或更高版本。

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 1970-01-01
      • 2021-03-15
      • 2011-11-13
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多