1.这个是页面,负责登录操作
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html;charset=GBK" pageEncoding="GB2312"%>
<html:html>
<head>
<title>
AddUser
</title>
<LINK
href="images/css.css" type=text/css rel=stylesheet>
<script>
//设一个变量
var XMLHttpReq=false;
//创建一个XMLHttpRequest对象
function createXMLHttpRequest(){
if(window.XMLHttpRequest){ //Mozilla
XMLHttpReq=new XMLHttpRequest();
}
else if(window.ActiveXObject){
try{
XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
//发送请求函数
function send(url){
createXMLHttpRequest();
XMLHttpReq.open("GET",url,true);
XMLHttpReq.onreadystatechange=proce; //指定响应的函数
XMLHttpReq.send(null); //发送请求
}
function proce(){
if(XMLHttpReq.readyState==4){ //对象状态
if(XMLHttpReq.status==200){//信息已成功返回,开始处理信息
var res=XMLHttpReq.responseXML.getElementsByTagName("content")[0].firstChild.data;
window.alert(res);
}else{
window.alert("所请求的页面有异常");
}
}
}
//身份验证
function check(){
var name=document.getElementById("salesname").value;
if(name==""){
alert("请输入姓名");
return false;
}
else{
send('checkuser?name='+name);
}
}
</script>
</head>
<body>
<p>
<table width="801" border="0" align="center">
<tr>
<th width="408" height="29" background="images/bg1.gif" scope="col"> </th>
</tr>
</table>
<table width="801" border="0" align="center">
<tr>
<th width="795" align="center" background="images/bg1_2.gif" scope="col"> </th>
</tr>
</table>
<h1 align="center"> </h1>
<h4 style="color:red" align="center"><html:errors/></h4>
<html:form action="/adduserAction.do" method="POST">
<table align="center">
<tr>
<td>账号</td>
<td><html:text property="salesname"/></td>
<td align="left" style="color:red">*</td>
<td><input type="button" value="检查用户名" onClick="check()"/></td>
</tr>
<tr>
<td>密码</td>
<td><html:password property="password"/></td>
<td align="left" style="color:red">*</td>
</tr>
<tr>
<td>密码确认</td>
<td><html:password property="repassword"/></td>
<td align="left" style="color:red">*</td>
</tr>
<tr>
<td>真实姓名</td>
<td><html:text property="realname"/></td>
<td align="left" style="color:red">*</td>
</tr>
<tr>
<td>性别</td>
<td><html:radio property="sex" value="男">男
</html:radio> <html:radio property="sex" value="女">女
</html:radio></td>
<td align="left" style="color:red">*</td>
</tr>
<tr>
<td></td>
<td><input type="image" src="images/ok.jpg" name="pic"/></td>
<td><a href="login.jsp"><img src="images/backbutton.gif" width="70" height="20"></a></td>
<td></td>
</tr>
</table>
<br>
<br>
<br>
<br>
<br />
</html:form>
</body>
</html:html>
2.创建Servlet
package com.ajaxservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jdbc.TDB;
public class CheckUser extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收的信息的字符集
//设置接收的信息的字符集
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
//设置输出的信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
//创建输出流
PrintWriter out=response.getWriter();
//查找用户名相关操作
boolean flag=new TDB().ajaxcheckuser(name);
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(flag==true){
out.println("<content>"+"账户已存在,请重新注册"+"</content>");
}else{
out.println("<content>"+"账户不存在,可以注册"+"</content>");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
3.Web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>addaction</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CheckUser</servlet-name>
<servlet-class>com.ajaxservlet.CheckUser</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addaction</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CheckUser</servlet-name>
<url-pattern>/checkuser</url-pattern>
</servlet-mapping>
</web-app>