标题 java web表单验证码
代码如下
package demon;
public class Account {
private String username;
private String realname;
public Account(String username, String realname) {
this.username = username;
this.username = realname;
}
public String getRealname1() {
return username;
}
public String getRealname() {
return realname;
}
public String toString() {
return "Account [username=" + username +", realname=" + realname +"]";
}
}
package demon;
public class AccountService {
//根据用户名和密码获取账号信息;
public Account getByUsernameAndPassword(String username, String password) {
//假装到数据可中查找符合用户名和密码的账号记录;
if("zhangsan".equalsIgnoreCase(username)&&"123456".equals(password)) {
return new Account("zhangsan","张三");
}
return null;
}
}
package demon;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/check-code.png")
public class CheckCodeServlet implements Servlet{
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
//从Response对象上获取字节输出流;
ImageGenerater.write(resp.getOutputStream());
}
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
}
}
package demon;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/gsd")
public class GenericServletDemo extends GenericServlet {
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("Hello,GenericServlet!");
resp.getWriter().println(getServletConfig());
}
public void init() throws ServletException{
}
}
package demon;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
// 声明该 Servlet 在站点中的路径;
// http://127.0.0.1:8080/demo/hello
@WebServlet("/hello")
// 必须直接或间接实现 javax.servlet.Servlet 接口,该类才是真正的 Servlet 类;
public class HelloServlet implements Servlet {
// 构造方法
public HelloServlet() {
System.out.println("正在执行:构造方法。");
}
// 初始化(initialize)方法;
// Servlet 初始化时(service 方法执行之前)容器(Tomcat)会自动调用 init 方法;
// 只会执行一次;
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("正在执行:初始化方法。");
}
// 服务方法
// 当浏览器访问此 Servlet 时,容器(Tomcat)会自动调用 service 方法;
// 根据 Request 中的信息,决定给出的 Response;
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
System.out.println("正在执行:服务方法。");
resp.setContentType("text/plain; charset=UTF-8");
// 获取面向浏览器的字符输出流:
PrintWriter out = resp.getWriter();
// 1 打印;
String name = "张三";
int age = 20;
out.printf("我是%s,我今年%s。\n", name, age);
// 2 追加;
out.append("追").append("加").append("!");
// 3 写,输出;
out.write("Hello, World!");
}
// 销毁方法
// Servlet 下线时容器(Tomcat)会自动调用 destroy 方法;
// 最多执行一次;
@Override
public void destroy() {
System.out.println("正在执行:销毁方法。");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
}
package demon;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public class ImageGenerater {
public static void write(OutputStream out) throws FileNotFoundException, IOException {
// 1 构造 BufferedImage (图像)对象,用于表示内存中的图像数据;
// arg0: width,图像的宽度,即水平方向上的像素数;
// arg1: height,图像的高度,即垂直方向上的像素数;
// arg2: imageType,图像色彩空间的类型;
BufferedImage image = new BufferedImage(60, 32, BufferedImage.TYPE_BYTE_BINARY);
// 2 编辑图像;
// 2.1 获取 Graphics(绘图)对象;
Graphics g = image.getGraphics();
// 2.2 设置颜色;
g.setColor(Color.WHITE);
// 2.3 设置字体;
g.setFont(new Font("SimSong", Font.ITALIC, 20));
// 2.4 绘制字符串;
// arg0: str,目标字符串,即被绘制的内容;
// arg1: x, 水平起始坐标;
// arg2: y,垂直起始坐标;
String text = "";
for(int i = 0; i < 4; i++) {
text += (int) (Math.random() * 10);
}
g.drawString(text, 8, 24);
// 3 输出图像;
// arg0: im,图像对象;
// arg1: formatName,图像格式,如 png、jpg、bmp 等;
// arg2: output,输出流;
ImageIO.write(image, "png", out);
}
}
<!DOCTYPE Html>
<!--
URL:http//127.0.0.1:8080/demon/lodin.html
-->
<meta charset="UTF-8"/>
<style>
body {
background-image: url(img/2.png);
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
background-size:100% 100% ;
}
</style>
<!-- action: http://127.0.0.1:8080/demon/login.do-->
<form action="./Login.do" method="post">
<p>
<a href="https://www.bh3.com/"><h1 align="center">点击一下有惊喜</h1></a>
<input name="un" type="text" placeholder="用户名"/>
</p>
<p align="right">彼岸双生<br>
姓名:希儿芙乐艾生日:10月18日<br></p>
<img src="img/13.gif" align ="right" width="400" height="400" >
<input name="pw" type="password" placeholder="密码"/>
</p>
<p>
<u>看不清楚,换一张</u><br>
<button>验证码</button>
<img src="./check-code.png" /><br>
<button>登录</button>
</p>
性别:
<input type="radio"/>男
<input type="radio"/>女<br/><br/>
兴趣爱好:
<input type="checkbox"/>睡觉
<input type="checkbox"/>唱歌
<input type="checkbox"/>打游戏
<input type="checkbox"/>看小说<br/><br/>
上传头像:
<input type="file"/><br/><br/>
请输入您的邮箱:
<input type="email"/><br/><br/>
请输入您的个人网站
<input type="url"/><br/><br/>
请选取一种颜色:
<input type="color"/><br/><br/>
请输入数值:
<input type="number" value="10" min="8" max="20"/><br/><br/>
<input type="date"/><br/><br/>
评论:<br/>
<textarea cols="50" rows="10">
评论的时候,请注意自己的言行举止
</textarea><br/><br/>
你所在的城市:<br/>
<select>
<option>北京</option>
<option>上海</option>
<option>广州</option>
<option selected=""selected>西安</option>
<option>沈阳</option>
<lect><br/><br/><br/><br/><br/>
<input type="submit" value="提交信息"/>
<input type="button" value="普通按钮"/>
<input type="reset" value="重置"/>
</form>
运行结果