【问题标题】:Html button click doesn't submit form dataHtml按钮点击不提交表单数据
【发布时间】:2012-02-02 14:28:52
【问题描述】:

我被这件事淹没了好几个小时......我的 html 表单显示,但是当我填写用户名并点击提交按钮时,它不会重定向到 servlet。但是当我输入 URL 作为这个 http://localhost:201/Practices/FirstPath?userName=achini ,将Html表单方法改为“get”..效果很好...请帮助我

这是我的 achHtml.html 代码

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

</head>
<body>
<form method="post" action="FirstPath">
    <input type="text" name="userName"/>
    <input type="button" value="Submit here"/>
</form>
</body>
</html>

这是我的名为 First.java 的 servlet

package com.achini.practice;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class First
 */
public class First extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String userName=request.getParameter("userName");
        out.println("Hello from the get method.!!!"+userName);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String userName=request.getParameter("userName");
        out.println("hello fom the post method"+userName);
    }

}

这是 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Practices</display-name>

  <servlet>
    <description></description>
    <display-name>First</display-name>
    <servlet-name>First</servlet-name>
    <servlet-class>com.achini.practice.First</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>First</servlet-name>
    <url-pattern>/FirstPath</url-pattern>
  </servlet-mapping>
</web-app>

【问题讨论】:

    标签: html jsp servlets web


    【解决方案1】:

    您的输入类型必须是提交而不是按钮。像这样的:

    <form method="post" action="FirstPath">
        <input type="text" name="userName"/>
        <input type="submit" value="Submit here"/> <!-- NOTE THE CHANGE HERE -->
    </form>
    

    【讨论】:

    • 但出现错误...当我将 post 方法保留在 html 文件中时...它给出了正确的声明..“来自 post 方法的你好 ach[我输入的用户名]”...但是当我将 html 文件更改为“get”时,它仍然显示相同的语句....'hello from the post method ###'
    • 1.您确定您的浏览器没有缓存旧表单吗? 2.如果您使用的是chrome,请按F12检查请求是否将数据作为GET数据发送。
    • mmm...实际上我正在使用eclipse..我认为它使用的浏览器是internt xplorer..hw 来检查它缓存旧表单的天气?
    • 右键->查看源代码
    猜你喜欢
    • 2013-10-18
    • 2017-04-26
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多