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

时间:2012-02-02 14:28:53

标签: html jsp servlets web

我在这个问题上溺水了好几个小时....我的html表单显示但是当我填写用户名并点击提交按钮时,它不会重定向到servlet。但是当我输入这个的URL时http:// localhost:201 / Practices / FirstPath?userName = achini ,将Html表单方法更改为“get”..它运行良好... plz帮助我

这是我的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>

1 个答案:

答案 0 :(得分:2)

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

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