求一段JAVA登陆界面验证代码 本人是新手,不知道该怎么弄,请高手帮帮忙啊...登陆界面两个文本框,一个账号框,一个密码框请高手给出具体代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 if(用户名对&&密码对){ 成功!} else { 失败!} 九成是 Web 写一个 Servlet,URL 映射为 /servlet/ValidateCodepackage com.servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;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 com.util.DaoConfig;public class ValidateCode extends HttpServlet { private static final long serialVersionUID = 1L; private final String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private Color getRandColor(int fc, int bc) { // 给定范围获得随机颜色 Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } // private Font mFont=new Font("宋体", Font.PLAIN,12);//设置字体 // 处理post public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置页面不缓存 response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); // 在内存中创建图象 int width = 90, height = 22; 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(DaoConfig.getValidateCodeFont(), Font.BOLD + Font.ITALIC, 20)); // 画边框 // g.setColor(new Color()); // g.drawRect(0,0,width-1,height-1); // 随机产生一些干扰线 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // 取随机产生的认证码(6位字符) StringBuffer sRand = new StringBuffer(6); for (int i = 0; i < 6; i++) { String rand = str.charAt(random.nextInt(36)) + ""; sRand.append(rand); // 将认证码显示到图象中 g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110))); // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawString(rand, 13 * i + 6, 17); } // 将验证码存入 session HttpSession session = request.getSession(); session.setAttribute("validateCode", sRand.toString()); g.dispose(); // 输出图象到页面 ImageIO.write(image, "jpeg", response.getOutputStream()); }}页面中:<script language="javascript"> <!-- function changeValidateCode(){ var temp = Math.random(); // 加 temp 的目的用于 Firefox 刷新验证码 document.getElementById("val").src="/calendar/servlet/ValidateCode?temp=" + temp; } --></script><img id="val" border="1" title="看不清,请单击图片" src="/<projectname>/servlet/ValidateCode" onclick="changeValidateCode();"/>页面提交后采用 session.getAttribute("validateCode"); 来进行对比。 swing用jbuilder画一个web用Dreamweaver画一个,别太懒了啊 我的WINFORM形式的,而且还要连接数据库 //先从数据库中取出帐号、密码 具体连数据库什么的就不写了String user=(String)rs.get("user");String password=(String)rs.get("password");//从提交的页面取出帐号、密码String _user=(String)request.getParameter("user");String _password=(String)request.getParameter("password");//然后比较进行比较if(_user.equales(user)){ }else{} to: Davis129自己写的,应该不是标准的吧,程序有标准可言吗? WinForm 形式的,那就是 Swing 的啰。这种 C/S 的需要用到验证码吗? to : bao110908(长牙了,好痛)哦 不好意思 我表达不准确 我的意思是大家都比较认可 用的比较多一种方式还有楼主的验证 是说验证帐号密码是否正确 不是你说的验证码... 这是我们老师将javaweb时候用dreamweaver做的一个例子,其实用别的语言编写也是大同小异,楼主看看吧<script language="JavaScript"> function subimtonclick(){ if(form1.text1.value==""){ alert("用户名不能为空"); form1.text1.focus(); return; } if(form1.text1.value.length<6 || form1.text1.value.length>10){ alert("用户名不能少于六个字符,不能超过十个字符"); form1.text1.focus(); return; } if(form1.text2.value==""){ alert("密码不能为空"); form1.text2.focus(); return; } if(form1.text2.value.length<6 || form1.text2.value.length>10){ alert("密码不能少于六个字符,不能超过十个字符"); form1.text2.focus(); return; } if(form1.text3.value.length>4){ alert("用户小于四个字符"); form1.text3.focus(); return; } //性别 var sex; if(form1.radiobutton.item(1).checked==true){ sex="女"; } if(form1.radiobutton.item(0).checked==true){ sex="男"; } //兴趣 var selectstr=""; for(i=0;i<form1.select1.options.length;i++){ if(form1.select1.options.item(i).selected==true){ selectstr+=form1.select1.options.item(i).text+" "; } } //城市 var citystr=""; citystr=form1.select2.options.item(form1.select2.selectedIndex).text; str="用户名:"+form1.text1.value+"<br>"+ "密码:"+form1.text2.value+"<br>"+ "性别:"+sex+"<br>" //document.write(); //document.write(""); document.writeln(selectstr); }</script> 简单的登陆窗口import java.awt.*;import java.awt.event.*;import javax.swing.*;public class LoginDemo extends JFrame{ JLabel l1; JLabel l2; JTextField t1; JPasswordField t2; JButton b1; JButton b2; public void init(){ l1=new JLabel("用户名"); l2=new JLabel("密码"); t1=new JTextField(""); t2=new JPasswordField(""); b1=new JButton("登陆"); b2=new JButton("取消"); setLayout(new GridLayout(5,2)); add(l1); add(t1); add(l2); add(t2); add(b1); add(b2); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if((t1.getText().equals("zhangsan"))&&(t2.getText().equals("123"))){ JFrame jf=new JFrame("welcome "+t1.getText()); jf.setSize(100,200); jf.setVisible(true); } else JOptionPane.showMessageDialog(null,"error,please enter again"); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ t1.setText(""); t2.setText(""); t1.requestFocus(); } }); setSize(200,200); setVisible(true); } LoginDemo(){ super("登陆窗口"); init(); } public static void main(String[] args){ LoginDemo ld=new LoginDemo(); }} 关于socket 面向对象真有意思,请教一个问题,谢谢 使用javaSwing調用CRX1 急求一个简单的java源代码!100分!!马上结帐 关于Hashtable的顺序的问题?************************* 小问题:那位大牛解释一下!谢了! HashTable 的存取问题! .rar文件如何打开???? 关于Java的共享内存总是抛出java.lang.OutOfMemoryError: Map failed java生成类似xml文档形式 如何实现JAVA中文档的读入 String.matches() 正则表达试的问题,麻烦看下,谢谢
成功!
} else {
失败!
}
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;import javax.imageio.ImageIO;
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 com.util.DaoConfig;public class ValidateCode extends HttpServlet { private static final long serialVersionUID = 1L; private final String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private Color getRandColor(int fc, int bc) {
// 给定范围获得随机颜色
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
} // private Font mFont=new Font("宋体", Font.PLAIN,12);//设置字体
// 处理post
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置页面不缓存
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg"); // 在内存中创建图象
int width = 90, height = 22;
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(DaoConfig.getValidateCodeFont(), Font.BOLD + Font.ITALIC, 20)); // 画边框
// g.setColor(new Color());
// g.drawRect(0,0,width-1,height-1); // 随机产生一些干扰线
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
} // 取随机产生的认证码(6位字符)
StringBuffer sRand = new StringBuffer(6);
for (int i = 0; i < 6; i++) {
String rand = str.charAt(random.nextInt(36)) + "";
sRand.append(rand);
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110))); // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 17);
} // 将验证码存入 session
HttpSession session = request.getSession();
session.setAttribute("validateCode", sRand.toString());
g.dispose();
// 输出图象到页面
ImageIO.write(image, "jpeg", response.getOutputStream());
}
}页面中:<script language="javascript">
<!--
function changeValidateCode(){
var temp = Math.random();
// 加 temp 的目的用于 Firefox 刷新验证码
document.getElementById("val").src="/calendar/servlet/ValidateCode?temp=" + temp;
}
-->
</script><img id="val" border="1" title="看不清,请单击图片" src="/<projectname>/servlet/ValidateCode" onclick="changeValidateCode();"/>页面提交后采用 session.getAttribute("validateCode"); 来进行对比。
web用Dreamweaver画一个,
别太懒了啊
String user=(String)rs.get("user");
String password=(String)rs.get("password");//从提交的页面取出帐号、密码
String _user=(String)request.getParameter("user");
String _password=(String)request.getParameter("password");//然后比较进行比较if(_user.equales(user))
{
}
else
{}
哦 不好意思 我表达不准确 我的意思是大家都比较认可 用的比较多一种方式
还有楼主的验证 是说验证帐号密码是否正确 不是你说的验证码...
其实用别的语言编写也是大同小异,楼主看看吧
<script language="JavaScript">
function subimtonclick(){
if(form1.text1.value==""){
alert("用户名不能为空");
form1.text1.focus();
return;
}
if(form1.text1.value.length<6 || form1.text1.value.length>10){
alert("用户名不能少于六个字符,不能超过十个字符");
form1.text1.focus();
return;
}
if(form1.text2.value==""){
alert("密码不能为空");
form1.text2.focus();
return;
}
if(form1.text2.value.length<6 || form1.text2.value.length>10){
alert("密码不能少于六个字符,不能超过十个字符");
form1.text2.focus();
return;
}
if(form1.text3.value.length>4){
alert("用户小于四个字符");
form1.text3.focus();
return;
}
//性别
var sex;
if(form1.radiobutton.item(1).checked==true){
sex="女";
}
if(form1.radiobutton.item(0).checked==true){
sex="男";
}
//兴趣
var selectstr="";
for(i=0;i<form1.select1.options.length;i++){
if(form1.select1.options.item(i).selected==true){
selectstr+=form1.select1.options.item(i).text+" ";
}
}
//城市
var citystr="";
citystr=form1.select2.options.item(form1.select2.selectedIndex).text;
str="用户名:"+form1.text1.value+"<br>"+
"密码:"+form1.text2.value+"<br>"+
"性别:"+sex+"<br>"
//document.write();
//document.write("");
document.writeln(selectstr);
}
</script>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginDemo extends JFrame{
JLabel l1;
JLabel l2;
JTextField t1;
JPasswordField t2;
JButton b1;
JButton b2;
public void init(){
l1=new JLabel("用户名");
l2=new JLabel("密码");
t1=new JTextField("");
t2=new JPasswordField("");
b1=new JButton("登陆");
b2=new JButton("取消");
setLayout(new GridLayout(5,2));
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if((t1.getText().equals("zhangsan"))&&(t2.getText().equals("123"))){
JFrame jf=new JFrame("welcome "+t1.getText());
jf.setSize(100,200);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(null,"error,please enter again");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
t1.setText("");
t2.setText("");
t1.requestFocus();
}
});
setSize(200,200);
setVisible(true);
}
LoginDemo(){
super("登陆窗口");
init();
}
public static void main(String[] args){
LoginDemo ld=new LoginDemo();
}
}