ajax提交:
第一步:ajax提交给servlet数据,进过相关的处理后
Stringmsg=
"error"
;
response.getWriter().write(msg);
返回信息!(如果是要转跳进行第三部,否则对返回的信息做相应的处理,如本例子的弹出提示框。)
第三部:通过前台中的js来实现页面跳转(如果是放在web-inf中jsp,jsp要在web.xml中部署后,这样的url才会有效,参考:http://blog.csdn.net/wanghaiping1993/article/details/23510411中关于web-inf中jsp如何访问
)
window.location.href=
"${pageContext.request.contextPath}/main.jsp"
;
form提交:
第一步:写好form表单后,向servlet提交信息
request.getRequestDispatcher("/WEB-INF/jsp/***.jsp").forward(request,response);
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
"://"
+request.getServerName()+
":"
+request.getServerPort()+path+
"/"
;
%>
function
ajax_submit(){
var
username=document.getElementsByName(
"username"
)[0].value;
password=document.getElementsByName(
"password"
/* //一定要加入jquery.js */
$.ajax({
url:
'${pageContext.request.contextPath }/Userservlet?method=login'
,
type:
'post'
data:
'username='
+username+
'&password='
+password,239)">success:
(msg) {
if
((msg==
"error"
)) {
}
else if
"success"
))
window.location.href=
"${pageContext.request.contextPath}/main.jsp"
;
}
});
}
<%-- <form action="" method="post">
--%>
<
@H_301_91@tr
>
用户名:
<
@H_301_91@input
type
=
"text"
name
=
"username"
></
@H_301_91@tr
><
@H_301_91@br
>
<
@H_301_91@tr
>
密码:
<
@H_301_91@input
type
=
"password"
name
=
"password"
></
@H_301_91@tr
><
@H_301_91@br
>
<
@H_301_91@tr
>
<
@H_301_91@input
type
=
"submit"
onclick
=
"ajax_submit()"
value
=
"提交"
></
@H_301_91@tr
>
</
div
>
<%-- </form>
--%>