jsp
两个JSP页面,一个登录界面,一个是验证码页面,如何将二者合为一个页面显示出来!大侠,帮帮忙!用<img src="yanzhengma.jsp" alt=""> 或是<%@ include file="yzm.jsp" %> 貌似都不行呀! --------------------编程问答-------------------- 你的验证码页面是怎么写的? --------------------编程问答--------------------
<%@ page language="java" contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%!
Color getRandColor(int cc,int bb)
{
Random random = new Random();
if(cc>255) cc=255;
if(bb>255) bb=255;
int r=cc+random.nextInt(bb-cc);
int g=cc+random.nextInt(bb-cc);
int b=cc+random.nextInt(bb-cc);
return new Color(r,g,b);
} //获取随机颜色
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=80; //定义验证码图片的长度
int height=30; //定义验证码图片的宽度
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//定义字体形式
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int i_x = random.nextInt(width);
int i_y = random.nextInt(height);
int i_xl = random.nextInt(12);
int i_yl = random.nextInt(12);
g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);
}
//用线条画背景
String s_Rand="";
for (int i=0;i<4;i++)
{
String rand=String.valueOf(random.nextInt(10));
s_Rand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
//产生4位随机码
session.setAttribute("rand",s_Rand);
//将验证码存入Session中
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
//输出验证图片
out.clear();
out = pageContext.pushBody();
%>
</body>
</html> --------------------编程问答-------------------- 从你的代码来看,你根本没理解验证码整体的程序逻辑。
验证码其实本身就不是个HTML,你前后加上什么 HTML BODY 就是错误的。
验证码本身做两个事情:
一、模拟随机数并记录到session中;
二、把自身当作图片来输出。
主页面用:<img src="yanzhengma.jsp" alt="">
验证码页面把所有HTML相关的全部删除,类似这些:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
--------------------编程问答-------------------- 还是什么都不显示! --------------------编程问答-------------------- 贴下你修改后的验证码JSP --------------------编程问答--------------------
+1
lZ,你把后面的</body></html>也去掉。
然后看看你这个文件的路径,仔细整理下应该是没问题的。
--------------------编程问答--------------------
<%@ page language="java" contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="UTF-8"%>
<%!
Color getRandColor(int cc,int bb)
{
Random random = new Random();
if(cc>255) {cc=255;}
if(bb>255) {bb=255;}
int r=cc+random.nextInt(bb-cc);
int g=cc+random.nextInt(bb-cc);
int b=cc+random.nextInt(bb-cc);
return new Color(r,g,b);
} //获取随机颜色
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=80; //定义验证码图片的长度
int height=30; //定义验证码图片的宽度
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//定义字体形式
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int i_x = random.nextInt(width);
int i_y = random.nextInt(height);
int i_xl = random.nextInt(12);
int i_yl = random.nextInt(12);
g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);
}
//用线条画背景
String s_Rand="";
for (int i=0;i<4;i++)
{
String rand=String.valueOf(random.nextInt(10));
s_Rand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
//产生4位随机码
session.setAttribute("rand",s_Rand);
//将验证码存入Session中
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
//输出验证图片
out.clear();
out = pageContext.pushBody();
%>
单独运行没有问题,但合在一起一直没出来过! --------------------编程问答-------------------- 引用界面denglu.jsp:代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="wrapper">
<h1>Welcome To Tourism Management System</h1>
<div id="xg_container" class="xg_container">
<div class="xg_bgimages">
<img src="images/denglu.jpg" alt="" style="display:block;"/>
<div class="xg_denglu"><%@ include file="denglu.html" %></div>
<div class="xg_yzm"><img alt="" src="yzm.jsp"></div>
</div>
</div>
</div>
<p></p>
<p></p>
</body>
</html>
denglu.html是一个界面,可以合denglu.jsp一起运行出来!
--------------------编程问答-------------------- 最后这两句话干掉:
out.clear();
out = pageContext.pushBody();
g.dispose();这句话应放到ImageIO.write(image, "JPEG", response.getOutputStream());后面去
不过你的denglu.jsp这个界面比较怪,应该是直接写SRC标签:
<div class="xg_denglu"><img src="http://localhost:8080/xxoo/yanzhengma.jsp" ></div>
不知道你又搞个HTML是在干啥。
按理来说,如果你直接浏览器输入:
http://localhost:8080/xxoo/yanzhengma.jsp
能看到图片的话,那么用SRC标签就肯定可以。 --------------------编程问答-------------------- 我测试了一下,你的方法是对的,不过,验证码在背景图片的下面,所以一直看不见!但现在又有一个问题,里面的那个验证码怎么让它放在背景图片的上面呢? --------------------编程问答-------------------- 在背景下面?这是个啥状况,把代码贴出来,说明下背景在哪定义的? --------------------编程问答-------------------- style.css:
.xg_bgimages img{position:absolute;
display:none;top:0px;
left:0px;width:800px;
height:530px;opacity:0.3;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=60);
}
.xg_denglu{position:relative;top:130px;left:520px;}
denglu.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css" />
<title>登录</title>
</head>
<body>
<form action="servlet/denglu" method="post">
<div >
<h2>用户登陆</h2>
<p>登录名:<input type="text" width="80px" name="name"/><a href="zhuce.jsp">注册</a><br><br>
<p>输密码:<input type="text" width="80px" name="pwd"/><a href="code.html">招回密码</a><br><br>验证码:
<br>
<br><br>
<input type="submit" value="确定" /><input type="reset" name="重置"/>
</div>
</form>
</body>
</html>
denglu.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="wrapper">
<h1>Welcome To Tourism Management System</h1>
<div id="xg_container" class="xg_container">
<div class="xg_bgimages">
<img src="images/denglu.jpg" alt="" style="display:block;"/>
<div class="xg_denglu"><%@ include file="denglu.html" %></div>
<div class="xg_yzm"><img alt="" src="http://localhost:8080/Lyglxt/yzm.jsp"></div>
</div>
</div>
</div>
<p></p>
<p></p>
</body>
</html> --------------------编程问答-------------------- 将验证码做成一个bean,在用
<jsp:userBean />
<>
补充:Java , Eclipse