模擬jQuery ajax服務(wù)器端與客戶端通信的代碼
功能如下:
如果用戶名為空提示“用戶名不能為空 ”
如果用戶名存在提示“用戶名[xxxxxx]已經(jīng)存在,請使用其他用戶名, 4 ”
如果用戶名不存在提示“用戶名[xxxxxx]尚未存在,可以使用該用戶名注冊, 5”
運(yùn)行效果如下:




package com.ljq.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class AjaxServer extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
//設(shè)置頁面utf-8編碼
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Integer total = (Integer) request.getSession().getAttribute("total");
int temp = 0;
if (total == null) {
temp = 1;
} else {
temp = total.intValue() + 1;
}
request.getSession().setAttribute("total", temp);
// 1.取參數(shù)
String param = request.getParameter("name");
String name = URLDecoder.decode(param, "UTF-8");
// 2、檢查參數(shù)是否有效
if (param == null || param.length() == 0) {
out.println("用戶名不能為空");
} else {
// 3、校驗(yàn)操作
if (name.equals("linjiqin")) {
// 4、返回結(jié)果數(shù)據(jù)
out.println("用戶名[" + name + "]已經(jīng)存在,請使用其他用戶名, " + temp);
} else {
out.println("用戶名[" + name + "]尚未存在,可以使用該用戶名注冊, " + temp);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>AjaxServer</servlet-name>
<servlet-class>com.ljq.test.AjaxServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxServer</servlet-name>
<url-pattern>/servlet/ajaxServer</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.3.1.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/validate.js"></script>
</head>
<body>
<!--html5的標(biāo)準(zhǔn): 首先標(biāo)簽名要小寫,其次標(biāo)簽必須關(guān)閉,第三屬性名遵循駱駝命名法,第四屬性值必須位于雙引號(hào)中-->
請輸入用戶名:<br/>
<input id="userName"/>
<input type="button" value=" 校 驗(yàn) " onclick="verify();"/>
<div id="result"></div>
</body>
</html>
validate.js
function verify() {
// 解決中文亂碼方法一: 頁面端發(fā)出的數(shù)據(jù)作一次encodeURI,服務(wù)器段使用new String(name.getBytes("iso8859-1"),"UTF-8");
// 解決中文亂碼方法二: 頁面端發(fā)出的數(shù)據(jù)作兩次encodeURI,服務(wù)器段使用URLDecoder.decode(name,"UTF-8")
var url = "servlet/ajaxServer?name=" + encodeURI(encodeURI($("#userName").val()));
//注意url前不要加"/",否則無法訪問url
//var url = "/servlet/ajaxServer?name=" + encodeURI(encodeURI($("#userName").val())); //錯(cuò)誤
url = convertURL(url);
$.get(url, null, function(data) {
$("#result").html(data);
});
}
// 給url地址增加時(shí)間戳,騙過瀏覽器,不讀取緩存
function convertURL(url) {
// 獲取時(shí)間戳
var timstamp = (new Date()).valueOf();
// 將時(shí)間戳信息拼接到url上
if (url.indexOf("?") >= 0) {
url = url + "&t=" + timstamp;
} else {
url = url + "?t=" + timstamp;
}
return url;
}
相關(guān)文章
使用jQuery.Pin垂直滾動(dòng)時(shí)固定導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了使用jQuery.Pin垂直滾動(dòng)時(shí)固定導(dǎo)航的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
jQuery實(shí)現(xiàn)彈出帶遮罩層的居中浮動(dòng)窗口效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)彈出帶遮罩層的居中浮動(dòng)窗口效果,涉及jQuery針對頁面元素的運(yùn)算與樣式的動(dòng)態(tài)修改相關(guān)技巧,需要的朋友可以參考下2016-09-09
jquery 學(xué)習(xí)之二 屬性(html()與html(val))
取得第一個(gè)匹配元素的html內(nèi)容。這個(gè)函數(shù)不能用于XML文檔。但可以用于XHTML文檔。2010-11-11
用jquery實(shí)現(xiàn)自定義風(fēng)格的滑動(dòng)條實(shí)現(xiàn)代碼
用jquery實(shí)現(xiàn)自定義風(fēng)格的滑動(dòng)條的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-04-04
Jquery實(shí)現(xiàn)彈出層分享微博插件具備動(dòng)畫效果
此Jquery插件是一款非常實(shí)用的特效,不但有分享功能,還具備了動(dòng)畫效果,提高了用戶體驗(yàn),感興趣的朋友可以參考下哈2013-04-04
jQuery實(shí)現(xiàn)響應(yīng)鼠標(biāo)背景變化的動(dòng)態(tài)菜單效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)響應(yīng)鼠標(biāo)背景變化的動(dòng)態(tài)菜單效果代碼,涉及jquery鼠標(biāo)mouseover事件操作頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
使用jQuery Mobile框架開發(fā)移動(dòng)端Web App的入門教程
jQuery Mobile是移動(dòng)端的基于jQuery的Web前端開發(fā)框架,移動(dòng)設(shè)備上的瀏覽器對HTML5的支持普遍較強(qiáng),因而jQuery Mobile的UI設(shè)計(jì)主要針對HTML5,下面就來詳細(xì)看一下使用jQuery Mobile框架開發(fā)移動(dòng)端Web App的入門教程2016-05-05

