login.jsp
<title>Insert title here</title>
<script type="text/javascript">
function changeImage(){
document.getElementById("myimage").src = "/d_session/checkimage?"+new Date().getTime();
}
</script>
</head>
<body>
<h3>XX网站的登录页面</h3>
<font color="red" size="5">${message }</font>
<form action="/d_session/login" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
验证码:<input type="text" name="checkcode">
<img src="/d_session/checkimage" style="cursor: pointer" onclick="changeImage();" id="myimage"><br/>
<input type="submit" value="登录">
</form>
</body>
CheckImageServlet doGet
public class CheckImageServlet extends HttpServlet {
private final int WIDTH = 120;
private final int HEIGHT = 30;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage bf = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) bf.getGraphics();//--Graphics2D is Graphics's child
Color color = new Color(203, 222, 225);
graphics.setColor(color);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
String base ="ABCDEFGHIJKLMN";
Random random = new Random();
graphics.setColor(Color.RED);
graphics.setFont(new Font("楷体",Font.BOLD,18));
int m = 13;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = random.nextInt(base.length());
char charAt = base.charAt(index);
int jiaodu = random.nextInt(60)-30;
double theta = jiaodu*Math.PI/180;
graphics.rotate(theta, m, 15);
graphics.drawString(charAt+"", m, 19);
sb.append(charAt);
graphics.rotate(-theta, m, 15);
m+=20;
}
request.getSession().setAttribute("checkcode_session", sb.toString());
graphics.setColor(Color.BLUE);
for (int i = 0; i < 4; i++) {
int x1 = random.nextInt(WIDTH);
int x2 = random.nextInt(WIDTH);
int y1 = random.nextInt(HEIGHT);
int y2 = random.nextInt(HEIGHT);
graphics.drawLine(x1, y1, x2, y2);
}
graphics.dispose();
ImageIO.write(bf, "png", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
LoginServlet doGet
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkcode = request.getParameter("checkcode");
String checkcode_session = (String) request.getSession().getAttribute("checkcode_session");
if(checkcode==null||!checkcode.equalsIgnoreCase(checkcode_session)){
request.setAttribute("message", "对不起, 您输入验证码信息不正确..."); request.getRequestDispatcher("/login.jsp").forward(request, response);
return ;
}
// 做用户名和密码的校验
// admin
if("admin".equals(username)&&"admin".equals(password)){
request.getSession().setAttribute("loginUser", "admin"); response.sendRedirect(request.getContextPath()+"/index.jsp"); }else{
request.setAttribute("message", "对不起, 您 是用户名或密码不正确...");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
1770

被折叠的 条评论
为什么被折叠?



