1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 网页登陆注册(jsp实现)验证码

网页登陆注册(jsp实现)验证码

时间:2021-08-06 03:46:33

相关推荐

网页登陆注册(jsp实现)验证码

这是一个登陆页面,有登陆验证和验证码的功能

(1)生成验证码的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.File;

import java.io.IOException;

public class YzmServlet extends HttpServlet {

//设置验证码图片的宽度

private static final int WIDTH = 100;

//设置验证码的高度

private static final int HEIGHT = 80;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//产生一张图片BufferedImage image=new BufferedImage(Width,Height,BufferedImage.TYPE_INT_RGB);response.setHeader("Pragram","no-cache");response.setHeader("Cache-Control","no-catch");response.setDateHeader("Exprires",0);Graphics g=image.getGraphics();//设置背景颜色g.setColor(Color.WHITE);//填充图片g.fillRect(0,0,Width,Height);//设置验证码颜色g.setColor(Color.RED);//设置验证码字体及大小Font font=new Font("微软雅黑",Font.BOLD,20);//获取验证码String str=getRandomString(4);//获取sessionHttpSession session=request.getSession();//给str做标记session.setAttribute("str",str);//在图片中画出验证码g.drawString(str,50,50);//在图片中随机划线for (int i=0;i<15;i++){int x1= RandomUtils.nextInt(0,Width);int x2=RandomUtils.nextInt(0,Width);int y1=RandomUtils.nextInt(0,Height);int y2=RandomUtils.nextInt(0,Height);Color color=new Color(RandomUtils.nextInt(0,255),RandomUtils.nextInt(0,255),RandomUtils.nextInt(0,255));g.setColor(color);g.drawLine(x1,y1,x2,y2);}//输出图片ImageIO.write(image,"jpg",response.getOutputStream());}

}

(2)登陆验证的servlet:

import mons.lang3.RandomUtils;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import java.io.IOException;

public class LoginServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");//获取输入的验证码的值String yzm=request.getParameter("yzm");//获取输入的用户名String username=request.getParameter("username");//获取输入的密码String userpassword=request.getParameter("userpassword");//获取sessionHttpSession session=request.getSession();//获取session标记的值,即服务端生成的验证码的值String yzm2=(String) session.getAttribute("str");//比较验证码if (yzm.equals(yzm2)){if(username.equals("zhangsan")&&userpassword.equals("123456")){response.getWriter().print("登陆成功");}else{response.getWriter().print("用户名或密码错误");}}else{response.getWriter().print("验证码错误");}}

}

(3)登陆界面(比较简陋)

<html lang="en">

<head>

<meta charset="UTF-8">

<title>登陆</title>

</head>

<script type="text/javascript">

function refreshImg() {

document.getElementById("y").src="./Yzm";

}

</script>

<body>

<table width="760px" height="100%" align="center" style="background-color: azure">

<tr>

<td>

<div align="center"></div>

<table width="200" height="300">

<tr>

<td>

<form action="./Login" name="form1" method="post">

用户名:<input type="text" name="username">

密码:<input type="password" name="userpassword">

<a>

<img id="y" name="y" src="./Yzm" height="100" οnclick="refreshImg()">

</a>

<button type="submit" value="看不清?" style="background-color: red" οnclick="refreshImg()">看不清?</button><br>

验证码:<input type="text" name="yzm" id="yzm" style="width: 30px" οnclick="refreshImg()"><br>

<button type="submit" onclick="check()">提交</button><button type="reset">重置</button></form></td></tr></table></div></td></tr>

</table>

</body>

</html>

(4)配置web.xml

<?xml version="1.0" encoding="utf-8"?>

<!--

Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements. See the NOTICE file distributed with

this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0

(the "License"); you may not use this file except in compliance with

the License. You may obtain a copy of the License at

/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

-->

<web-app xmlns="/xml/ns/javaee"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/xml/ns/javaee

/xml/ns/javaee/web-app_3_0.xsd"

version="3.0"

metadata-complete="true">

<display-name>Welcome to Tomcat</display-name><description>Welcome to Tomcat</description><servlet><servlet-name>LoginServlet</servlet-name><servlet-class>servlet.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/Login</url-pattern></servlet-mapping><servlet><servlet-name>YzmServlet</servlet-name><servlet-class>servlet.YzmServlet</servlet-class></servlet><servlet-mapping><servlet-name>YzmServlet</servlet-name><url-pattern>/Yzm</url-pattern></servlet-mapping>

</web-app>

界面还有一些问题,不能更换验证码.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。