JavaScript学习4---ajax的使用
function getXMLHttpRequest(){
//IE浏览器
var client = getXMLHttpRequestFromIE();
if(client == null){
//非IE浏览器
client = new XMLHttpRequest();
}
return client;
}
function getXMLHttpRequestFromIE(){
var namePrefixes = ["Msxml3", "Msxml2", "Msxml", "Microsoft"];
for(var i = 0; i < namePrefixes.length; i++){
var name = namePrefixes[i] + ".XMLHTTP";
try{
return new ActiveXObject(name);
}catch(e){
}
}
return null;
}
jsp文件:
<%@ page pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/style.css"></link>
<style type="text/css">
#content div{
margin: 10px 0px;
}
div.submit{
text-align:center;
}
label{
width:80px;
display:block;
float:left;
}
fieldset{
width:300px;
margin:auto;
margin-top:20px;
display:block;
}
fieldset select{
width:120px;
}
fieldset input{
width:120px;
}
</style>
<title>登录</title>
<script type="text/javas
<script type="text/javas
var client = getXMLHttpRequest();
function login(form){
var username = form.username.value;
var password = form.password.value;
var requestURL = "${pageContext.request.contextPath}/login.do?username=" + username + "&password=" + password;
requestURL += "&time=" + new Date().getTime();
client.open("GET", requestURL, true);
client.on
if(client.readyState == 4){
if(client.status == 200){
var result = parseInt(client.responseText);
if(result == 0){
document.getElementById("message").innerHTML = "用户不存在或密码不正确!" ;
}else{
window.location = "${pageContext.request.contextPath}/memberCenter.jsp";
}
}
}
}
client.send(null);
}
function clearMessage(){
document.getElementById("message").innerHTML = "";
}
</script>
</head>
<body>
<div id="container">
<div id="header">
<ul>
<li><a href="#">帮助</a></li>
</ul>
</div>
<div id="menu">
<ul>
<li><a href="index.html">首页</a></li>
<li><a href="roomPrice.html">设施与价格</a></li>
<li><a href="memberCenter.html">会员中心</a></li>
</ul>
</div>
<div id="content">
<form>
<fieldset>
<legend>登录信息</legend>
<div>
<label for="username">用户名</label>
<input type="text" name="username" id="username" on
</div>
<div>
<label for="password">密码</label>
<input type="text" name="password" id="password" on
</div>
</fieldset>
<div id="message"></div>
<div class="submit">
<input type="button" value="登录" on
<p>如果您还没有注册,请<a href="${pageContext.request.contextPath}/register.jsp">点击这里注册</a>成为会员。</p>
</div>
</form>
</div>
<hr />
<div id="footer">
<p>本系统做为Java Web教学设计使用。未经允许,不得做为它用,版权所有Allan<br />联系方式:javamedia@live.cn</p>
</div>
</div>
</body>
</html>
servlet 代码
package act
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
public class ActionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getServletPath();
path = path.substring(0, path.indexOf("."));
if (path.equals("/register")) {
register(request, response);
} else if (path.equals("/login")) {
login(request, response);
} else if (path.equals("/validateusername")) {
validateUsername(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void register(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Member member = new Member();
RequestUtil.setProperties(request, member);
MemberBiz memberBiz = new MemberBiz();
Member m = memberBiz.find(member.getUsername());
if (m != null) {
request.setAttribute("message", "username " + m.getUsername()
+ " has already existed.");
getServletContext().getRequestDispatcher("/register.jsp").forward(
request, response);
} else {
memberBiz.add(member);
getServletContext().getRequestDispatcher("/success.jsp").forward(
request, response);
}
}
public void validateUsername(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain;charset=gbk");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
MemberBiz memberBiz = new MemberBiz();
Member m = memberBiz.find(username);
if (m == null) {
out.println(0);
} else {
out.println(1);
}
out.close();
}
public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain;charset=gbk");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
MemberBiz memberBiz = new MemberBiz();
Member m = memberBiz.find(username, password);
if(m == null){
out.println(0);
}else{
HttpSession session = request.getSession();
session.setAttribute("user", m);
out.println(1);
}
out.close();
}
}