3种方法:
各种input的传递name和value的情况:
前提是,只要没有name属性,就不传递
方法1:(form内1)
--------type=submit,点击后就会自动触发去提交所在form的信息
<form action = "提交的地址"> <input type="submit" value="提交"> </form> 方法2:(form内2)
-----------type=button,点击onclick事件,触发“”内的方法,除了自带的submit外,也可以在JavaScript写方法去调用 <form action = "提交的地址"> <input type="button" value="提交" οnclick="this.form.submit()"> </form>
javascript中onclick调用方法的定义:
function button1_click() { document.form1.action = “check.jsp”;document.form1.submit();}点击button按钮不传递button的name和value值
方法3:(form外)
<form id="form1" action="提交的地址"> </form> <input type="button" value="提交" οnclick="document.getElementById('form1').submit();"> 其中最后一种比较灵活,比如说出发事件的可以不是一个按钮,而是一个超链接或者图片之类的元素。 <a οnclick="document.getElementById('form1').submit();">提交</a>
而在接受数据的jsp中,直接使用
<%=request.getParameter("控件名")%>
即可使用获得的数据
下面为方法一的代码示例:
摘自:http://www.cnblogs.com/modou/articles/1261727.html
t1.htm
<
html
>
< head >
< title > test </ title >
</ head >
< body >
< form name = " f1 " id = " f1 " action = " t1.jsp " method = " post " >
< table border = " 0 " >
< tr >
< td > 内容: </ td >
< td >< input type = " text " name = " field1 " ></ td >
</ tr >
< tr >
< td colspan = " 2 " align = " center " >< input type = " submit " value = " submit " ></ td >
</ tr >
</ table >
</ form >
</ body >
</ html >
< head >
< title > test </ title >
</ head >
< body >
< form name = " f1 " id = " f1 " action = " t1.jsp " method = " post " >
< table border = " 0 " >
< tr >
< td > 内容: </ td >
< td >< input type = " text " name = " field1 " ></ td >
</ tr >
< tr >
< td colspan = " 2 " align = " center " >< input type = " submit " value = " submit " ></ td >
</ tr >
</ table >
</ form >
</ body >
</ html >
t1.jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB2312
"
pageEncoding
=
"
GB2312
"
%>
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=GB2312 " >
< title > test </ title >
</ head >
< body >
<%= request.getParameter( " field1 " ) %>
</ body >
</ html >
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=GB2312 " >
< title > test </ title >
</ head >
< body >
<%= request.getParameter( " field1 " ) %>
</ body >
</ html >
提交表单使用 post 和 get 方式,或者通过网址传递的参数,都可以通过request.getParameter(name)来获取表单的数据。
要想在一个普通类中使用request,可以把PageContext以参数的形式引进去,代码如下:
public
void
MyFun(PageContext pageContext)
throws
Exception
{
PrintWriter out = pageContext.getResponse().getWriter();
ServletRequest request = pageContext.getRequest();
out.print(request.getParameter( " field1 " ));
}
{
PrintWriter out = pageContext.getResponse().getWriter();
ServletRequest request = pageContext.getRequest();
out.print(request.getParameter( " field1 " ));
}
在页面中这样使用:
<%
Class1 class1 = new Class1();
class1.MyFun(pageContext);
%>
Class1 class1 = new Class1();
class1.MyFun(pageContext);
%>