BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #58222同步于 2017/11/29
Java机器人发帖

【问题】eclipse servlet 404报错

xiaopaofu
2017/11/29镜像同步0 回复
在eclipse中用jquery+servlet实现动态验证码 打开页面报错404,请各位指点一下,谢谢[em71] <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>yan</servlet-name> <servlet-class>serve.logcap</servlet-class> </servlet> <servlet-mapping> <servlet-name>yan</servlet-name> <url-pattern>/servl</url-pattern> </servlet-mapping> 下面是jquery代码,得到的图片渲染到页面 $.ajax({ url:'servl', method:'GET', success:function(data){ console.log(data); $('.cap img').attr('src',data); } }) public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 告知浏览当作图片处理 response.setContentType("image/jpeg"); // 告诉浏览器不缓存 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setHeader("expires", "0"); // 产生由4位数字构成的验证码 int length = 4; String valcode = ""; Random rd = new Random(); for(int i=0; i<length; i++) valcode+=rd.nextInt(10); // 把产生的验证码存入到Session中 HttpSession session = request.getSession(); session.setAttribute("valcode", valcode); // 产生图片 int width = 80; int height = 30; BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); // 获取一个Graphics Graphics g = img.getGraphics(); // 填充背景色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 填充干扰线50 for(int i=0; i<50; i++){ g.setColor(new Color(rd.nextInt(100)+155,rd.nextInt(100)+155,rd.nextInt(100)+155)); g.drawLine(rd.nextInt(width), rd.nextInt(height),rd.nextInt(width), rd.nextInt(height)); } // 绘制边框 g.setColor(Color.GRAY); g.drawRect(0, 0, width-1, height-1); // 绘制验证码 Font[] fonts = {new Font("隶书",Font.BOLD,18),new Font("楷体",Font.BOLD,18),new Font("宋体",Font.BOLD,18),new Font("幼圆",Font.BOLD,18)}; for(int i=0; i<length; i++){ g.setColor(new Color(rd.nextInt(150),rd.nextInt(150),rd.nextInt(150))); g.setFont(fonts[rd.nextInt(fonts.length)]); g.drawString(valcode.charAt(i)+"", width/valcode.length()*i+2, 18); } // 输出图像 g.dispose(); ImageIO.write(img, "jpeg", response.getOutputStream()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // 获取验证码 String valcode = request.getSession().getAttribute("valcode").toString(); // 获取用户填写的验证码 String vcode = request.getParameter("vcode"); // 进行验证 if(!valcode.equals(vcode)) System.out.println(">>>验证码错误!"); else System.out.println(">>>验证码正确!"); }
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。