一个从html向servlet中提交数据的问题
请问怎么样用submit()方法将html中的数据提交到servlet中去呢请举个简单的例子看看谢谢!!!!!!!!!!!!...
请问怎么样用 submit() 方法将html中的数据提交到servlet中去呢
请举个简单的例子看看 谢谢!!!!!!!!!!!! 展开
请举个简单的例子看看 谢谢!!!!!!!!!!!! 展开
展开全部
在浏览器输入:http://127.0.0.1:8080/test/test.html
点击提交按钮,Tomcat后台输出:
control: aaa's value is : bbb
页面显示结果:
page: aaa's value is : bbb
文件目录结构:
test
│ test.html
│
└—WEB-INF
│ web.xml
└—classes
SubmitServle.java
SubmitServle.class
源代码(test.html):
<form action="submit.do">
<input type="textField" name="aaa" value="bbb" />
<input type="submit" />
</form>
源代码(web.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>submitServle</servlet-name>
<servlet-class>SubmitServle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>submitServle</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
源代码(SubmitServle.java):
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SubmitServle extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String aaa = request.getParameter("aaa");
System.out.println("control: aaa's value is : " + aaa);
response.getOutputStream().println("page: aaa's value is : " + aaa);;
}
}
另:test.html中的提交按钮被点击时,会将表单中的字段aaa以及它的值提交到submit.do去(提交的这一过程也可以用javaScript来写)。服务器接到这个请求,将解析web.xml文件中的内容,将转交给符合这一请求(*.do)的servlet--SubmitServlet处理。处理调用service方法,可以从request中取得参数aaa的值,然后打印出来。
点击提交按钮,Tomcat后台输出:
control: aaa's value is : bbb
页面显示结果:
page: aaa's value is : bbb
文件目录结构:
test
│ test.html
│
└—WEB-INF
│ web.xml
└—classes
SubmitServle.java
SubmitServle.class
源代码(test.html):
<form action="submit.do">
<input type="textField" name="aaa" value="bbb" />
<input type="submit" />
</form>
源代码(web.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>submitServle</servlet-name>
<servlet-class>SubmitServle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>submitServle</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
源代码(SubmitServle.java):
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SubmitServle extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String aaa = request.getParameter("aaa");
System.out.println("control: aaa's value is : " + aaa);
response.getOutputStream().println("page: aaa's value is : " + aaa);;
}
}
另:test.html中的提交按钮被点击时,会将表单中的字段aaa以及它的值提交到submit.do去(提交的这一过程也可以用javaScript来写)。服务器接到这个请求,将解析web.xml文件中的内容,将转交给符合这一请求(*.do)的servlet--SubmitServlet处理。处理调用service方法,可以从request中取得参数aaa的值,然后打印出来。
展开全部
html:
<html>
<body>
<form action="LoginServlet" method="POST">
用户名:<input type="text" name="username">
密码: <input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
action中的值与Servlet文件相同
Servlet:
package org.servlet;//包
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServle extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
}
}
修改web.xml文件
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>org.servlet.LoginServle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServle</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<html>
<body>
<form action="LoginServlet" method="POST">
用户名:<input type="text" name="username">
密码: <input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
action中的值与Servlet文件相同
Servlet:
package org.servlet;//包
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServle extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
}
}
修改web.xml文件
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>org.servlet.LoginServle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServle</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询