1.form表单的四种提交方式
方式一、
Js
的提交方式,表单上有
action
<script type="text/javascript">
function regist(){
document.getElementById("form1").submit();
//
for
m
表单
id.
submit();
}
</script>
<body>
<form id="form1" method="post" action="Servletone">
姓名
:<input type="text" name="sname" size="25">
<input type="button" value="
提交
"
οnclick="regist()">
</form>
</body>
方式二、
Js
的提交方式,表单上有
action
<script type="text/javascript">
function regist(){
document.form1.submit(); //
for
m
表单
name
.
submit();
}
</script>
<body>
<form name="form1" method="post" action="Servletone">
姓名
:<input type="text" name="sname" size="25">
<input type="button" value="
提交
"
οnclick="regist()">
</form>
</body>
方式三、
Js
的提交方式,表单上没有
action
<script type="text/javascript">
function regist(){
var form=document.getElementById("form1");
//
for
m
表单
id
form.action="Servletone";
//
因为
form
没有带
action
所以这里要加上提交到哪
form.submit();
}
</script>
<form id=
“
form1
”
method=
“
post
”
>
<!
—没带
action
时
-->
姓名
:<input type="text" name="sname" size="25">
<input type="button" value="
提交
"
οnclick="regist()">
</form>
方式四、
<script type="text/javascript">
function regist(){
var sname=document.getElementById("sname").value;
if(sname == null || sname ==""){
alert("please enter name");
return false;
}
return true;
}
</script>
<form name="form1" id="form1" method="post" action="abc" onSubmit="return regist()">
/
/
当
onSubmit="return regist()"
中的
regist()
返回
true
时,就可以进行提交,提交到
action
=
“
abc
”
name:<input type="text" name="sname" id="sname" size="25">
<input type="submit" value="
提交
" >
</form>
2.JSP与Servlet之间传递数据出现的乱码问题
JSP
设置编码:
设置
JSP
的编码方式:选择
window
–
> Preferences
–
> MyEclipse
–
> Files and Editors
–
> JSP
<
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Servlet
设置编码
public
void
doPost(HttpServletRequest request, HttpServletResponse response){
response.setContentType(
"text/html;charset=UTF-8"
);
//response
是返回给页面的编码。
request.setCharacterEncoding(
"UTF-8"
);
//request
是从页面来的请求编码。
String na=new String(request.getParameter("na").getBytes("ISO-8859-1"), "UTF-8");
//
超级链接(
get
方式)的编码
修改
TOMCAT
:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"redirectPort="8443"
URIEncoding="UTF-8"
/>
修改
MYSQL
有时会用
JDBC
连接
MySQL
数据库,可能会出现乱码,所以要在连接之后加上如下的语句:
useUnicode=true&characterEncoding=UTF-8
3.访问Servlet的三种方式
1.
第一种访问方式:浏览器地址栏访问
---doGet
方式访问
http://localhost:8080/oneWebProject
(项目名)
/Servletone
(
web.xml
中配的
url-pattern
值)
注意:
action
后面写的
url
必须和
url-pattern
一样。
使用超级链接访问,在
href
处指定
Servlet
的
URL
2
第二种访问方式:表单提交
---doGet
和
doPoset
方式访问
<body> <form action="Servletone" method="post"> <input type="submit" value="
提交
">
</form> </body>
3
第三种访问方式:超连接
---doGet
方式访问
<a href="Servletone">
访问
servlet</a>