Html按钮点击不提交表单数据

发布于 2025-01-02 05:06:18 字数 2957 浏览 0 评论 0原文

我在这件事上沉迷了几个小时......我的 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>

I 'm drowning in this matter for hours....my html form displays but when I fill user name and hit submit button,it does nt redirect to the servlet.but when I type the URL as this http://localhost:201/Practices/FirstPath?userName=achini ,changing the Html form method into "get"..it works well...plz help me

this is my achHtml.html code

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

this is my servlet called First.java

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

}

this is web.xml file

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2025-01-09 05:06:18

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

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

Your input type must be submmit not button. Something like this:

<form method="post" action="FirstPath">
    <input type="text" name="userName"/>
    <input type="submit" value="Submit here"/> <!-- NOTE THE CHANGE HERE -->
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文