404错误


package com.imooc.code;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Random;
/*
验证码工具类
*/
public class CaptcahCode {
/*
验证码生成的方法
*/
public static String drawImage(HttpServletResponse response){
//1:定义以字符串的拼接的StringBuilder
StringBuilder builder = new StringBuilder();
//准备产生4个字符串的随机数
for(int i=0;i<4;i++){
builder.append(randomChar());
}
String code = builder.toString();
//2:定义图片的宽度和高度
int width = 70;
int height = 25;
//简历bufferedImage对象,制定图片的长度和宽度以及色彩
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
//3:获取到 Graphics2D 绘制对象,开始绘制验证码
Graphics2D g = bi.createGraphics();
//4:设置文字的字体和大小
Font font = new Font("微软雅黑",Font.PLAIN,20);
//设置字体的颜色
Color color = new Color(0,0,0);
//设置字体
g.setFont(font);
//设置颜色
g.setColor(color);
//设置背景
g.setBackground(new Color(226,226,240));
//开始绘制对象
g.clearRect(0,0,width,height);
//绘制形状,获取矩形对象
FontRenderContext context = g.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(code,context);
//计算文件的坐标和间距
double x = (width - bounds.getWidth())/2;
double y = (height - bounds.getHeight())/2;
double ascent = bounds.getY();
double baseY = y - ascent;
g.drawString(code,(int)x,(int)baseY);
//结束绘制
g.dispose();
try {
ImageIO.write(bi,"jpg",response.getOutputStream());
//刷新响应流
response.flushBuffer();
}catch(Exception ex){
ex.printStackTrace();
}
return code;
}
private static char randomChar(){
//1.定义验证码需要的字母和数字
String string = "QWERTYUIOPASDFGHJKLZXCVBNM123456789";
//2.定义随机对象
Random random = new Random();
return string.charAt(random.nextInt(string.length()));
}
public static void main(String[] args) {
CaptcahCode.drawImage(null);
}
}
<%@ page import="com.imooc.code.CaptcahCode" %><%--
Created by IntelliJ IDEA.
User: 小鑫鑫
Date: 2019/8/31
Time: 9:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//1清空浏览器缓存,为了清空浏览器的缓存,因为浏览器会被网站的资源文件和图像进行记忆存储,如果
//浏览器加载过的图片记忆起来,记忆用户文件就不会和服务器交互,如果我们验证不清空的话,可能造成一个问题就是:验证码刷新以后没有效果
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires",0);
//2调用编写的生成验证码的工具
String code=CaptcahCode.drawImage(response);
session.setAttribute("code",code);
%>
<%--
Created by IntelliJ IDEA.
User: 小鑫鑫
Date: 2019/8/31
Time: 9:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="code.jsp" alt="">
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>0
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕阿莹
2019-08-31 10:57:46
1、404错误代表访问路径有误,同学输入http://localhost:8080/imooccode_war_ecploded/index.jsp
试试。
2、请同学看一下:idea的这个Tomcat中配置

3、在web.xml
中改为一下代码试试:意思是,启动后默认加载index.jsp
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星