1.下面代碼為伺服器端擷取請求資訊Servlet,真正用的時候可能就是struts等
package com.adu;
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;
public class AjaxServer extends HttpServlet {
private static final long serialVersionUID = -1391282338046351379L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
商賬追收
//擷取頁面jquery的url提交的資料,進行處理
// String userName = request.getParameter("name");
// String passWord = request.getParameter("password");
String name = request.getParameter("name");
String password = request.getParameter("password");
//轉換字元集編碼,避免中文亂碼問題
String userName = URLDecoder.decode(name,"UTF-8");
String passWord = URLDecoder.decode(password,"UTF-8");
//進行邏輯判斷,看輸入是否合法
if(userName.length() != 0 && userName != null && passWord.length() != 0 && passWord != null){
if(userName.equals("mawanli") && passWord.equals("123")){
//out.print("Welcome* "+userName+" *visit my web!");
out.print("歡迎* "+userName+" *訪問!");
out.flush();
out.close();
}else{
out.print("sorry please login again!");
// out.print("請輸入正確的使用者名稱和密碼!");
}
}
else{
out.print("please input right name and password!");
//out.print("請重新登入!");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
this.doGet(req, resp);
}
}
2.以下是頁面測試的代碼:
<%@ 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>測試ajax驗證登入</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.6.min.js"></script></head>
<script type="text/javascript">
//新增時間戳記,以防每次測試為不同的url,解決瀏覽器緩衝問題
function convertUrl(url) {
var timestamp = (new Date()).valueOf();
if (url.indexOf("?") >= 0) {
url = url + "&t=" + timestamp;
} else { 髮型123
url = url + "?t=" + timestamp;
}
return url;
}
//用encodeURI可以解決頁面傳值時的中文亂碼問題
function validate(){
var name = encodeURI(encodeURI($("#userName").val()));
var password = encodeURI(encodeURI($("#passWord").val()));
var url = "AjaxServer?name="+name+"&password="+password;
url = convertUrl(url);
$.get(url,null,function(data){
$("#result").html(data);
});
}
</script>
<body>
<div align="center">
<div >
<form action="#" method="get">
<table>
<tr><td>使用者名稱:</td><td><input type="text" name="userName" id="userName"/></td></tr>
<tr><td>密 碼:</td><td><input type="password" id="passWord"/></td></tr>
<tr align="right"><td colspan="2"><input type="button" value="登入" onclick="validate()"/>
<input type="reset" value="取消"/></td></tr>
</table>
</form>
<div id="result"></div>
</div>
<hr width="600px" align="center" size="3" color="green"/>
</div>
</body>
</html>
<pre name="code" class="java"></pre>[align=left][/align]