register.jsp:
注意:1、contextName就是你的项目名称。2、form中method设置为get.
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<head>
<script>
function UserExist(){
var name= document.form1.username.value
document.form1.action="/contextName/userName.check?username="+name
document.form1.submit()
}
</script>
</head>
<body>
<form name="form1" action="SaveServlet" method="get" onsubmit="return check()">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username" >
<input type="button" name="bn1" value="检测用户名是否存在" onclick="UserExist()">
</td></tr>
<tr><td colspan="2"><input type="submit" name="subm" value="确定"></td></tr>
</table>
</form>
</body>
</html>
web.xml添加下面定义
<servlet>
<servlet-name>CheckUserNameServlet</servlet-name>
<servlet-class>com.company.project.servlet.CheckUserNameServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckUserNameServlet</servlet-name>
<url-pattern>*.check</url-pattern>
</servlet-mapping>
CheckUserNameServlet中doGet()方法最后一句话可以修改成这样:
out.println("<a href='javascript:history.go(-1)'>返回</a>")
您好,out.println("<script language='javascript'>alert('密码错误!')</script>")注意,在这一句后面不能有 sendRedirect("")之类跳转的语句,因为这样的话该servlet还没输出到页面就已经结束生命期了。估计你是用了跳转吧。
使用document.form.action方式
相关源码如下: *.js [javascript] 代码如下: <span style="white-space:pre"></span>document.getElementById("sendPerson").value = SendPerson document.getElementById("currentTime").value = currentTime() document.getElementById("message").value = message document.getElementById("recvPerson").value = recvPerson document.chatform.action = "ToHistoryServlet" document.chatform.submit() *.html [html] 代码如下: <!--the tag below is the params to the userHistory dml@2012.8.27--> <input type="hidden" name="sendPerson" id="sendPerson"><input type="hidden" name="currentTime" id="currentTime"><input type="hidden" name="message" id="message"><input type="hidden" name="recvPerson" id="recvPerson"> 注意的是,input需指定name属性,这么servlet才可以获取到参数值 *.java [java] 代码如下: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { www.2cto.com String sendPerson = request.getParameter("sendPerson") String recvPerson = request.getParameter("recvPerson") String sendTime = request.getParameter("currentTime") String message = request.getParameter("message") Message msg = new Message() msg.setMessage(message) msg.setRecvPerson(recvPerson) msg.setSendPerson(sendPerson) msg.setSendTime(sendTime) HistoryHandle.addMessage(msg) }这个缺点是页面就跳走了,要是希望保持原页面,可以参照方法2 2.jquery调用后台方法 [javascript] 代码如下: $.ajax({ type : "POST", contentType : "application/json", url : "ToHistoryServlet?sendPerson=" + SendPerson + "¤tTime=" + currentTime() + "&message=" + message + "&recvPerson=" + recvPerson, dataType : 'json', success : function(result) { alert(result.d) } })