-----------编写注册页面,点击注册时先进行Ajax请求后台校验,然后校验通过则跳转到成功页面,否则当前页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册页面</title>
<script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#username").blur(function(){
if($("#username").val()==null || $("#username").val()==""){
alert("请输入用户名!");
return;
}
});
$("#password").blur(function(){
if($("#password").val()==null || $("#password").val()==""){
alert("请输入密码!");
return;
}
});
$("#confirmpassword").blur(function(){
if($("#confirmpassword").val()==null || $("#confirmpassword").val()==""){
alert("请输入确认密码!");
return;
}
});
$("#piccode").blur(function(){
if($("#piccode").val()==null || $("#piccode").val()==""){
alert("请输入验证码!");
return;
}
});
//校验函数封装
function validate(){
if($("#username").val()==null || $("#username").val()==""){
alert("请输入用户名!");
return false;
}
if($("#password").val()==null || $("#password").val()==""){
alert("请输入密码!");
return false;
}
if($("#confirmpassword").val()==null || $("#confirmpassword").val()==""){
alert("请输入确认密码!");
return false;
}
if($("#piccode").val()==null || $("#piccode").val()==""){
alert("请输入验证码!");
return false;
}
return true;
}
//验证码图片点击后切换更新
$("#img_piccode").click(function(){
var date=new Date();
$("#img_piccode").attr('src',"<%=request.getContextPath()%>/servlet/ImageServlet?date"+date);
});
//做最后一道校验,点击注册时
$("#register").click(function(){
if(!validate()){
return;
}else{
if(($("#username").val()!=null && $("#username").val()!="") && ($("#password").val()!=null && $("#password").val()!="")
&& ($("#confirmpassword").val()!=null && $("#confirmpassword").val()!="")){
var data={"username":$("#username").val(),"password":$("#password").val()};
var jsonArrayFinal = JSON.stringify(data);//string类型
$.ajax({
url:"servlet/LoginServlet",
type:"GET",
dataType:"java",
data:{"user":jsonArrayFinal},
success:function(data){
if(data=="0"){
alert("验证通过,请继续执行");
$("#form1").submit();
}else{
alert("验证失败,请重新输入账号密码!");
return;
}
},
error:function(){
alert("失败了!");
}
});
}
// $("#form1")[0].submit();
}
});
//点击重置按钮重新清空form表单
$("#rollback").click(function(){
$("#form1")[0].reset();
// window.location.reload();
});
});
</script>
<style type="text/css">
#register,#rollback{
background-color:red;
cursor:pointer;
}
</style>
</head>
<body>
<form id="form1" action="login.jsp">
<table>
<tr>
<td>用户名:</td>
<!-- 发送异步请求来进行校验 -->
<td><input type="text" name="username" id="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="confirmpassword" id="confirmpassword"></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="piccode" id="piccode"> <img alt="验证码" src="<%=request.getContextPath()%>/servlet/ImageServlet" id="img_piccode"></td>
</tr>
<tr>
<td><span id="register">注册</span></td>
<td><span id="rollback">重置</span></td>
</tr>
</table>
</form>
</body>
</html>
--------编写后台验证码图片代码编写
package com.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageServlet extends HttpServlet{
private static final Integer WIDTH=64;
private static final Integer HEIGHT=30;
private static final Integer BK_COLOR1=220;
private static final Integer BK_COLOR2=250;
private static final Integer BK_COLOR3=250;
//生成4位数的验证码数组
private static final Integer SIZE=4;
public static final String[] DATAS=new String[]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
//设置字体大小
private static final int FONTSIZE=30;
//输出图片格式
private static final String FORMATNAME="png";
//干扰线条数
private static final Integer LINESIZE=10;
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);//获取图片对象 指定宽高
Graphics graphics=image.getGraphics();//获取画笔对象
graphics.setColor(new Color(BK_COLOR1, BK_COLOR2, BK_COLOR3));//指定背景颜色
graphics.fillRect(0, 0, WIDTH, HEIGHT);//画边框
Random random=new Random();//获取随机数对象
StringBuffer sBuffer=new StringBuffer();
for(int i=0;i<SIZE;i++){
int whichOne=random.nextInt(DATAS.length);
String data=DATAS[whichOne];
graphics.setColor(new Color(random.nextInt(DATAS.length)*i+100, random.nextInt(DATAS.length)*i+100, random.nextInt(DATAS.length)*i+100));//继续用画笔画背景颜色
graphics.setFont(new Font("宋体", Font.BOLD,FONTSIZE));
//开始将字符绘制到图片上
graphics.drawString(data, 13*i+8, 23);
for(int j=0;j<LINESIZE;j++){
int x=random.nextInt(WIDTH);
int y1=random.nextInt(HEIGHT);
int x1=random.nextInt(WIDTH)+x;
int y=random.nextInt(HEIGHT)+y1;
graphics.setColor(new Color(192, 192, 192));
graphics.drawLine(x, y, x1,y1);
}
sBuffer.append(data);
}
req.setAttribute("piccode", sBuffer.toString());
ImageIO.write(image, FORMATNAME, res.getOutputStream());
}
}
--------------编写Ajax发送过来的json数据,返回java类型对象,0表示成功,1表示失败
package com.servlet;
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 net.sf.json.JSONObject;
public class LoginServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//后台获取Json数据
String userJson=req.getParameter("user");
PrintWriter pWriter=res.getWriter();
JSONObject jsonObject=JSONObject.fromObject(userJson);
String userName=(String) jsonObject.get("username");
String password=(String) jsonObject.get("password");
System.out.println("用户名为:"+userName+",密码为:"+password);
if(userName.equals("itcast") && password.equals("123456")){
pWriter.print("0");
}else{
pWriter.print("1");
}
pWriter.flush();
pWriter.close();
}
}
---------------编写登录成功页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>校验页面</title>
</head>
<body>
<%
String userName=request.getParameter("username");
String password=request.getParameter("password");
%>
</body>
<div>欢迎用户<%=userName %>登录!</div>
<div>账号:<%=userName %>;密码:<%=password %></div>
</html>