0
<form id='search' method='get'>
<input type='text' name='search' />
<input type='submit' value='Search'/><br/>
</form>

我有这个搜索表。

String search = request.getParameter("search");
    if (search != null) {
                request.setAttribute("search", search);
                RequestDispatcher rd = request.getRequestDispatcher("Content");
                rd.forward(request, response);
}

在这里,我想获取我在输入中输入的内容并将其发送给 Content。我的意见根本不是反应。我的调试器显示我的程序甚至没有进入 request.getParameter("search") 部分。有什么问题?

4

1 回答 1

1

以如下形式指定动作名称:

<form action="/yourServlet/"id='search' method='get'>
<input type='text' name='search' />
<input type='submit' value='Search'/><br/>
</form>

小服务程序代码:

    public class YourServlet extends HttpServlet{

        public void doGet(HttpServletRequest request, HttpServletResponse   
        response) throws IOException{
           String search = request.getParameter("search");
              if (search != null) {
                 request.setAttribute("search", search);
                RequestDispatcher rd =  
                  request.getRequestDispatcher("Content");
                rd.forward(request, response);
    }
        }
    }

以及 web.xml 中的 servlet 映射:

<servlet>
    <servlet-name>yourServlet</servlet-name>
    <servlet-class>test.YourServlet</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>yourServlet</servlet-name>
    <url-pattern>/yourServlet/</url-pattern>
  </servlet-mapping>
于 2015-05-02T12:44:43.153 回答