HttpServletRequest没有参数?

时间:2014-07-09 09:42:38

标签: forms servlets java-ee-6

我无法使用表单在HttpServletRequest对象中获取任何参数。

我的项目设置为使用Java6,并且是一个动态的Web项目2.5。

html页面的鳕鱼:

<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Thesaurus Builder</title>
    <link type="text/css" rel="stylesheet" href="<c:url value="/form.css"/>" />
</head>
<body>
    <form action="<c:url value='/' />" method="post" accept-charset="UTF-8"> <!-- accept-charset="UTF-8" -->
        <fieldset>
            <legend>Analyse de texte</legend>

            <label for="file">Fichier à analyser</label>
            <textarea rows="7" cols="100" name="file" id="file" ></textarea>
            <br />
            <br />

            <label for="encoding">Encodage</label>
            <input type="text" id="encoding" name="encoding" value="UTF-8" />
            <br />
            <br />


            <br />
            <label for="debug">Mode Debug</label>
            <input type="checkbox" id="debug" name="debug"/>
            <br />
            <br />

            <input type="submit" value="Envoyer" class="sansLabel" />
            <br />                
        </fieldset>
    </form>



</body>
</html>

servlet:

    package test2;

    import java.io.IOException;
    import java.util.Map;
    import java.util.Map.Entry;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class Tester extends HttpServlet{

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

            this.getServletContext().getRequestDispatcher( "/WEB-INF/input.jsp" ).forward( request, response );



    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        Map<String,String> map = request.getParameterMap();
        for(Entry<String, String> s : map.entrySet()){
            System.out.println(s.getKey() + " - "+ s.getValue());
        }
        this.getServletContext().getRequestDispatcher( "/WEB-INF/input.jsp" ).forward( request, response );

    }
  }

和web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>test2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
        <servlet-name>Tester</servlet-name>
        <servlet-class>test2.Tester</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Tester</servlet-name>
        <url-pattern>/test</url-pattern>
        <url-pattern>/default.html</url-pattern>
        <url-pattern>/default.htm</url-pattern>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>

</web-app>

servlet中的函数不会打印任何内容。 我完全被困在这里。谁知道该怎么办?

1 个答案:

答案 0 :(得分:1)

您的servlet未映射到url /。您的servlet代码未被调用。要么在web.xml中有<url-pattern>/</url-pattern>,要么在JSP中有"<c:url value='/test' />"