Html按钮点击不提交表单数据
我在这件事上沉迷了几个小时......我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的输入类型必须是提交而不是按钮。像这样的东西:
Your input type must be submmit not button. Something like this: