**发一篇有关javaWeb的,有关转发,重定向,session,request域的综合小案例:验证码。
需求是:
1. 访问有验证码的登陆页面login.jsp
2. 用户输入用户名,密码以及验证码。
* 如果用户名和密码输入有误,跳转登录页面,提示:用户名或密码错误
* 如果验证码输入错误,跳转登录页面,提示:验证码错误
* 如果全部输入正确,则跳转到主页success.jsp,显示:用户名,欢迎您。
以下是代码:
首先是登录页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
<script>
window.onload=function(){
document.getElementById("img").onclick=function () {
this.src="/Cookie&&Session&&jsp/checkCodeServlet?time="+new Date().getTime();
}
}
</script>
<style>
div {
color: #f00;
}
</style>
</head>
<body>
<form action="/Cookie&&Session&&jsp/loginServlet" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="checkCode"></td>
</tr>
<tr>
<td colspan="2"><img id="img" src="/Cookie&&Session&&jsp/checkCodeServlet"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table>
<div><%=request.getAttribute("login_error")==null?"":request.getAttribute("login_error")%></div>
<div><%=request.getAttribute("cc_error")==null?"":request.getAttribute("cc_error")%></div>
</form>
</body>
</html>
以下是servlet代码:
package com.slxy.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkCode = request.getParameter("checkCode");
HttpSession session = request.getSession();
String checkCode_session = (String) session.getAttribute("checkCode_session");
//清除session
session.removeAttribute("checkCode_session");
//判断验证码
if (checkCode_session != null && checkCode_session.equalsIgnoreCase(checkCode)) {
//判断用户名和密码是否一致
if ("admin".equals(username) && "admin".equals(password)) { //登录成功(此处调用查询数据库的方法)
//存储信息
session.setAttribute("username", username);
//重定向
response.sendRedirect(request.getContextPath() + "/success.jsp");
} else { //登陆失败
request.setAttribute("login_error", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} else {//验证码不一致
request.setAttribute("cc_error", "验证码错误");
//转发到登录页面
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
验证码的代码:
package com.slxy.servlet;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int width = 100;
int height = 50;
//1.创建对象,在内存中画图(验证码的图片对象)
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2.美化图片
//2.1 填充背景色
Graphics g = bi.getGraphics(); //画笔对象
g.setColor(Color.pink); //设置画笔颜色
g.fillRect(0, 0, width, height);
//2.2画边框
g.setColor(Color.BLUE);
g.drawRect(0, 0, width - 1, height - 1); //边框
String str = "ABCDEFGHIJKLMNOPQRSTWVUXYZabcdefghijklmnopqrstuvwxyz0123456789";
//生成随机角标
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 4; i++) {
int index = random.nextInt(str.length());
//获取字符
char c = str.charAt(index);//随机字符
sb.append(c);
//2.3写验证码
g.drawString("" + c, width / 5 * i, height / 2);
}
String checkCode_session = sb.toString();
//将验证码存到session中
req.getSession().setAttribute("checkCode_session", checkCode_session);
//2.4 画干扰线
g.setColor(Color.GREEN);
for (int i = 0; i < 6; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}
//3.将图片输出到页面展
ImageIO.write(bi, "jpg", resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
以下是效果图: