類比jQuery ajax伺服器端與用戶端通訊的代碼_jquery

來源:互聯網
上載者:User

功能如下:

如果使用者名稱為空白提示“使用者名稱不可為空 ”   

如果使用者名稱存在提示“使用者名稱[xxxxxx]已經存在,請使用其他使用者名稱, 4 ”          

如果使用者名稱不存在提示“使用者名稱[xxxxxx]尚未存在,可以使用該使用者名稱註冊, 5”

運行效果如下: 

             
目錄結構:
 
伺服器端AjaxServer
複製代碼 代碼如下:

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 {
//設定頁面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.取參數
String param = request.getParameter("name");
String name = URLDecoder.decode(param, "UTF-8");
// 2、檢查參數是否有效
if (param == null || param.length() == 0) {
out.println("使用者名稱不可為空");
} else {
// 3、校正操作
if (name.equals("linjiqin")) {
// 4、返回結果資料
out.println("使用者名稱[" + name + "]已經存在,請使用其他使用者名稱, " + 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的標準: 首先標籤名要小寫,其次標籤必須關閉,第三屬性名稱遵循駱駝命名法,第四屬性值必須位於雙引號中-->
請輸入使用者名稱:<br/>
<input id="userName"/>
<input type="button" value=" 校 驗 " onclick="verify();"/>
<div id="result"></div>
</body>
</html>

validate.js
複製代碼 代碼如下:

function verify() {
// 解決中文亂碼方法一: 頁面端發出的資料作一次encodeURI,伺服器段使用new String(name.getBytes("iso8859-1"),"UTF-8");
// 解決中文亂碼方法二: 頁面端發出的資料作兩次encodeURI,伺服器段使用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())); //錯誤
url = convertURL(url);
$.get(url, null, function(data) {
$("#result").html(data);
});
}
// 給url地址增加時間戳記,騙過瀏覽器,不讀取緩衝
function convertURL(url) {
// 擷取時間戳記
var timstamp = (new Date()).valueOf();
// 將時間戳記資訊拼接到url上
if (url.indexOf("?") >= 0) {
url = url + "&t=" + timstamp;
} else {
url = url + "?t=" + timstamp;
}
return url;
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.