订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
※Ajax※

Ajax初學者入門---登陸(ログイン)

今天閑的沒事,看了看ajax,寫個初學者適用的登陸例子,寫得比較爛,希望大家多多指教。
想想,大部分我們初學時不是HelloWorld,就是Login,俺剛開始學Ajax時也想找個Login例子,結果找了半天也沒找到,唉~~~只能自己痛苦的啃書,哈哈......
login.jsp頁面如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<%@ page language="java" contentType="text/html; charset=GB2312"
	pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
		<title>login</title>
		
		<script type="text/javascript">
			var xmlHttp = false;
			
			function createRequest()
			{
				try
				{
					xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e)
				{
					try
					{
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e2)
					{
						xmlHttp = false;
					}
				}
				
				if(!xmlHttp && typeof XMLHttpRequest != 'undefined')
				{
					xmlHttp = new XMLHttpRequest();
				}
				
				if(!xmlHttp)
				{
					alert("Error initialing XMLHttpRequest!");
				}
			}
			
			function callServerGET()
			{
				createRequest();
				
				var username = document.getElementById("username").value;
				var password = document.getElementById("password").value;
				
				if((username == null) || (username == ""))
				{
					return;
				}
				
				if((password == null) || (password == ""))
				{
					return;
				}
				
				var url = "test?username=" + escape(username) + "&password=" + escape(password);
				
				xmlHttp.open("GET", url, true);
				xmlHttp.onreadystatechange = updatePageGET;
				xmlHttp.send(null);
			}
			
			function updatePageGET()
			{
				if(xmlHttp.readyState == 4)
				{
					if(xmlHttp.status == 200)
					{
						var response = xmlHttp.responseText;
						document.getElementById("result").innerHTML = response;
					}
					else if(xmlHttp.status == 403)
					{
						alert("Access denied.");
					}
					else if(xmlHttp.status == 404)
					{
						alert("Request URL does not exist");
					}
					else
					{
						alert("Error: status code is " + xmlHttp.status);
					}
				}
			}
			
			function callServerPOST()
			{
				createRequest();
				
				var username = document.getElementById("username").value;
				var password = document.getElementById("password").value;
				
				if((username == null) || (username == ""))
				{
					return;
				}
				
				if((password == null) || (password == ""))
				{
					return;
				}
				
				var xml = "<loginInfo><username>" + escape(username) + "<\/username><password>" + escape(password) + "<\/password><\/loginInfo>";
				var url = "test?timestamp=" + new Date().getTime();
				
				xmlHttp.open("POST", url, true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = updatePagePOST;
				xmlHttp.send(xml);
			}
			
			function updatePagePOST()
			{
				if(xmlHttp.readyState == 4)
				{
					if(xmlHttp.status == 200)
					{
						var response = xmlHttp.responseXML;
						var resultStr = response.getElementsByTagName("resultStr").value;
						document.getElementById("result").innerHTML = resultStr;
					}
					else if(xmlHttp.status == 403)
					{
						alert("Access denied.");
					}
					else if(xmlHttp.status == 404)
					{
						alert("Request URL does not exist");
					}
					else
					{
						alert("Error: status code is " + xmlHttp.status);
					}
				}
			}
		</script>
		
	</head>
	
	<body>
		<div id="result"></div>
		<form action="#">
			username:<input type="text" id="username" /><br>
			password:<input type="password" id="password" /><br>
			<input type="button" value="submitByGET" onclick="callServerGET();"/>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="button" value="submitByPOST" onclick="callServerPOST();"/>
		</form>
	</body>
</html>

Login.java源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package ajax;
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
 * The <code>Login</code> class is an ajax example.
 * 
 * @author Warren CC <warrenwuxj@sohu.com>
 * @version Login.java,v 1.1 2007-4-17 下午01:54:14 
 */
public class Login extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
 
		String resultStr = null;
		if ("warren".equals(username) && "123456".equals(password)) {
			resultStr = "success";
		} else {
			resultStr = "fail";
		}
 
		response.getWriter().write(resultStr);
	}
 
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		StringBuffer strBuffer = new StringBuffer();
		boolean tag = false;
		String username = null;
		String password = null;
 
		BufferedReader reader = request.getReader();
		String readerLine = null;
 
		while ((readerLine = reader.readLine()) != null) {
			strBuffer.append(readerLine);
			tag = true;
		}
 
		if (tag) {
			String xmlStr = strBuffer.toString();
			Document xmlDoc = null;
 
			try {
				xmlDoc = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder().parse(
								new ByteArrayInputStream(xmlStr.getBytes()));
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}
 
			username = xmlDoc.getElementsByTagName("username").item(0)
					.getFirstChild().getNodeValue();
			password = xmlDoc.getElementsByTagName("password").item(0)
					.getFirstChild().getNodeValue();
		}
 
		StringBuffer resultXMLStr = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		resultXMLStr.append("<results>");
		resultXMLStr.append("<resultStr>");
		
		if ("warren".equals(username) && "123456".equals(password)) {
			resultXMLStr.append("success");
		} else {
			resultXMLStr.append("fail");
		}
		
		resultXMLStr.append("</resultStr>");
		resultXMLStr.append("</results>");
		
		response.setContentType("text/xml; charset=utf-8");
		
		response.getWriter().write(resultXMLStr.toString());
	}
}

web.xml添加如下:
1
2
3
4
5
6
7
8
9
<servlet>
		<servlet-name>testLogin</servlet-name>
		<servlet-class>ajax.Login</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>testLogin</servlet-name>
		<url-pattern>/test</url-pattern>
	</servlet-mapping>


OK,完成了,希望對初學的有幫助,也希望和大家共同學習。

平均得分
(0 次评分)





文章来自: 本站原创
标签: java  Ajax Web 
评论: 70 | 查看次数: 1491
  • 共有 70 条评论
  • 1
  • 2
  • 3
  • 4
  • 5
  • |
  • >>
游客 [2008-11-14 16:22:40]
游客 [2008-11-14 14:37:38]
游客 [2008-11-11 10:31:01]
游客 [2008-11-05 15:02:13]
cheap wow power leveling Beyond knowing your limitations. HDRO gold it's important to be able to size up opponents lecteur mp3 at a glance. level wow Knowing their class is the first step, understanding. lord of rings online gold your places in a rock-paper-scissors scenario. lotro gold More important, however, is the estimation .lotro gold of gear. mp3 As much as skill and class
balance . mp3 mp4 player plays a part in the outcome . mp3 mp4 player of combat, gear is a major differentiator that makes up for shortcomings in other areas. mp3 mp4 player In fact, with . mp3 player kaufen the introduction of Resilience, gear more than mp4 ever plays a more substantial part in PvP. power level In an Arena match, the very first thing we . power level scope out is gear. wow level Through quick tab-selection viewing .wow leveling of character portraits, we generally wow lvl have a good idea of the classes we're up against if they keep their helm graphic on. wow lvl If they are wearing Season 3 shoulders, then we know . wow lvl 60 their relative experience. wow lvl 70 This is why the visual i.wow power leveling mpact of Arena shoulders is so important.wow powerlevel It immediately gives you a general idea of how tough the match will be. wow powerlevel Players in full S3 will likely have over 10k hp and over. wow powerleveling 400 Resilience, depending on the class and spec. A full S3 SL/SL Warlock, for example, will easily have. about 12-13k hp and over 400 Resilience. Identifying weapons is slightly more . difficult but will also give a general idea of an enemy's strength. Season 1 and 2 weapons share the same graphics, so it's harder to identify. Season 3 weapons, on the other hand, are distinctive and share models with Black Temple and Mount Hyjal weapons. which 最新免费网络游戏 have relatively the same power. A review of Brutal Gladiator weapons will come in handy because these will be the most common way to identify opponents of relative skill. With the new mechanics. in place for Arenas, Season 4 will more or less weed out the chaff from the grain.
游客 [2008-11-05 14:56:35]
4GB MP3 PLAYER My wife is a shrewd little fox. Bluetooth Headset She knows just how much I love the fact. Bluetooth Headsets that she plays the game with me so sometimes, when we have our little domestic arguments, she makes sure. cell phone accessories to cancel her WoW account just to drive home a point. des po wow Of course, it doesn't mean much since we're both paid up for the next few months, but the message is clear -- "we make up (or you see things my way). digital camcorder or I'm quitting the game!" Of course, we don't reconcile merely because I'll be losing my . digital camcorders favorite playing partner, but I have to confess that it doesn't make . dvd players me happy one bit. free online games For parents, World of Warcraft can be a . free online war games useful bargaining chip for their kids with the parental . gold wow controls feature. gold wow It's easy enough to control WoW time if kids aren't doing their homework, floundering in school, or simply not. mp3 mp4 player doing their chores. mp3 player Conversely, a friend . mp3 player accessories of mine gave his son a WoW subscription when . mp3 player accessory he did well in school. mp3 player kaufen World of Warcraft can be so. online games much fun and addicting that it's . play war games often used as a social tool, and it's often upsetting when our. po wow friends quit playing the game. portable dvd player How many of us have . wow europe had friends whose significant others have "allowed" them to. wow geld play the game after, say, a wonderful date. wow gold verkaufen I'm not sure if it only applies to me, but . wow level service because I play the game with many of my RL friends . wow leveling service and my family, I use the lure of WoW . to full effect. I once had my brother do a specific task for . the promise of an upgrade to The Burning Crusade. A little before he finished what I asked him to do, I secretly upgraded his account. so he could免费网络游戏 finally make his Blood Elf Priest. Kind of manipulative, I know, but we did end up having . a lot of fun leveling our alts together. How about you. How much a part of your life is WoW and has it .最新网游 ever been used as a bargaining . chip in your social life.
游客 [2008-11-05 14:51:07]
mp3 8GB MP3 PLAYER A couple of people . apple ipod have posted about the Shattered Sun Peacekeepers slacking off on their jobs lately. buy cheap wow gold quite possibly thanks . canon digital camera to something Blizzard fed them . cheap world of warcraft gold in PatchIn their reports, they complain about Peacekeepers. digital camera attacking them for no reason, sometimes not even in retaliation for attacking (or defending yourself against). digital cameras a member of the opposing faction. dvd player I can actually empathize with this as. eve isk I encountered the ill-placed wrath of the Peacekeepers myself when I rezzed my wife's toon in front of . ipod the Staging Area. ipod nano Without having done anything other. ipod shuffle than rezz, the Peacekeepers promptly charged . ipod touch and made short work of me. ipods In my experience, I have found 网游 that the Peacekeepers around . mp3 the Shattered Sun Staging Area have . mp3 player been slacking off. mp3 players In fact, my wife's toon was ganked right in front . mp4 of the building and the so-called Peacekeepers . portable dvd players did absolutely nothing. world of warcraft buy gold Sensing a bug, my wife . wow wrote a ticket and got a somewhat rude e-mail response saying that -- you guessed it -- it was working as intended. wow gold An Alliance . wow gold guild on my server seemed to be aware of the fact and exploited it to full effect, killing solo players who could seek no refuge under the apathetic -- or even hostile -- Peacekeepers. wow leveling According to reports, it's a known bug -- one player even . wow powerleveling made a video to document it -- but so far Blizzard . zubehoer mp3 player has turned a blind eye to it. I got a more sympathetic response 网络游戏 from my GM who at least. mentioned he'd look into the situation. An in-game GM even reset the Peacekeepers in order to see if it would change anything (it didn't). I don't mind gankage
游客 [2008-11-05 14:49:47]
2GB MP3 PLAYER Remember the WoW account hacking. 4GB MP3 PLAYER If you're a WoW player. buy wow gold you couldn't have missed . buying gold world of warcraft any of the articles posting this issue. cell phones Well, as it turns out, an article posted by . phones cell grimwell says that someone might have found the problem behind all the . cheap cell phones account hacking, stealing and selling even 5 year's worth items. cheap wow gold A piece of the BBC news article says:"Analysis [. cheap wow gold cheap wow gold cheapest wow gold ] showed that. eve isk it lay dormant on a victims . mp3 players machine until . portable mp3 player they ran World of Warcraft (WoW) at which point it captured login data and sent it to. portable mp3 players the hacking group. sell wow gold The group's enthusiastic use of the cursor flaw suggests it is trying to . world of warcraft gold do the same again. wow The online fantasy game . wow gold now has more than eight million active players around the world. wow gold Research by security firm Symantec . wow gold suggests that the raw value of a WoW account is now higher. wow gold than a credit card and its associated verification data. wow gold "Normally, as a WoW player, you'd know about the risks involved when creating an online account, yet some were . wow gold lazy enough not to check on their accounts every once in a while, thus resulting in their items . wow gold kaufen and WoW currency being stolen. wow gold kaufen Everyone knows that . stuff like that happen, some. even managed to hack the Superbowl website and use it . to host code for spyware, so monitor the hell out of your computer!In an article I wrote yesterday. about hackers seeing 游戏 console users as the new target, there is a comment 魔兽 coming from Stefana Muller.
游客 [2008-11-03 09:42:02]
游客 [2008-10-31 14:19:54]
游客 [2008-10-31 14:19:54]
游客 [2008-10-30 16:03:43]
wow power leveling
wow power leveling
Buy Lotro Gold | Lord Of The Rings Online Gold
Lotro Accounts
| Buy Lotro Accounts
Lord Of The Rings Online Power Leveling | Lord Of The Rings Online PowerLeveling
Lotro Cd Key | Lord Time Card
Lotro Gold | Lotro Gold Instant Delivery
lord of the rings online accounts | lord of the rings online accounts for sale
Lotro Power Leveling | Lotro Powerleveling
Lord Of The Rings Online Cd Key | Lord Of The Rings Online Time Card
Lord of the Rings Online Gold
Buy Lotro Gold
Sell LoTRO Gold
LoTRO CD Key
LoTRO Europe Gold
Cheap LoTRO Accounts
Lord of the Rings Online Power Leveling
Lord of the Rings online CD Key
Cheap Lotro Gold

Air Jordan Shoes
Warhammer Gold
Warhammer Online Gold
Warhammer Accounts
Warhammer Power Leveling
Warhammer Online Key
Warhammer Gold
Warhammer Online Gold
Warhammer Time Card
Warhammer CD Key
WAR gold
warhammer online gold
Buy WAR gold
Buy warhammer gold
Buy warhammer online gold
warhammer gold
WAR Accounts
warhammer Accounts
warhammer online Accounts
Buy WAR Accounts
warhammer Accounts for sale
Warhammer Power Leveling
Warhammer Online Power Leveling
War Power Leveling
Buy Warhammer Power Leveling
Warhammer PowerLeveling
Cheap Warhammer Power Leveling
Cheap Warhammer Online Power Leveling
Buy War Power Leveling
Warhammer EU Power Leveling
Cheap Warhammer Gold
Cheap Warhammer online gold
Buy Cheap Warhammer Gold
Buy WAR Gold
Warhammer EU Gold
Warhammer EU Power Leveling
Warhammer EU CD Key
Warhammer EU Accounts
Warhammer CD Key
Warhammer online CD Key
Warhammer Timecard
Buy Warhammer Time Card
Warhammer 60 days Time Card
Cheap WAR Accounts
Cheap warhammer Accounts
Cheap warhammer online Accounts
Buy Cheap WAR Accounts
buy Warhammer CD Key
buy Warhammer online CD Key
cheap Warhammer CD key
warhammer time card
Warhammer prepaid time card
游客 [2008-10-30 14:30:22]
游客 [2008-10-27 14:54:40]
游客 [2008-10-27 08:44:10]
游客 [2008-10-24 16:45:26]
  • 共有 70 条评论
  • 1
  • 2
  • 3
  • 4
  • 5
  • |
  • >>
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启