请问这个简单的JAVABEAN怎么写????

kangaroo 2001-07-31 12:20:16
就是 当用户登陆的时候,他输入用户名和密码? 通常jsp是这样处理,如果密码不对则用<script language =""JAVASCRIPT">
alert("invalid password & username!!!");
</script>
这样,如果每个页面都这样判断那么代码冗余太多!!
然后现在的要求是 要求自己开发一个工具包,只需传如参数就可以实现同样的功能!!多谢,指点?

...全文
226 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
24680 2001-08-07
  • 打赏
  • 举报
回复
这个也太多了吧。
package jb;
public class helloworld
{
public String name="hello world!";
public String hello()
{
return name;
}
}

robinhoodx 2001-08-05
  • 打赏
  • 举报
回复
最好买本书看看.
an classical example using isValidate() check validation in a JavaBean which can be reused in different jsp pages.
That's one of reasons why jsp is better than ASP(not ASP.net)

for detailed explaination, see www.jspin.com---tips and tutorial---form----advanced form processing


package forms;
import java.util.*;
import java.sql.*;
public class FormBean {
private String firstName;
private String lastName;
private String email;
private String userName;
private String password1;
private String password2;
private String zip;
private String[] faveMusic;
private String notify;
private Hashtable errors;



String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:Customer";
Connection conn = null;
Statement stmt=null;
ResultSet rs = null;
int count=0;
//private boolean isDouble=false;

public void connect(){
try {
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr);
checkForWarning (conn.getWarnings());
stmt = conn.createStatement();
System.out.println("connect to database :OK!");
}catch (SQLException ex) {
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println("SQLState:" +ex.getSQLState());
System.out.println(" Message :" +ex.getMessage());
System.out.println(" Vendor :" +ex.getErrorCode());
ex = ex.getNextException ();
}
}catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
}


public int execUpdate(String sql) {
try {
count=stmt.executeUpdate(sql);
if(count==0) {
System.out.println("error!");
}
// stmt.close();
// rs.close();
}catch (SQLException ex) {
System.out.println("Update error");
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println("SQLState:" +ex.getSQLState());
System.out.println(" Message :" +ex.getMessage());
System.out.println(" Vendor :" +ex.getErrorCode());
ex = ex.getNextException ();
}
}catch (java.lang.Exception ex) {
ex.printStackTrace ();

}
return count;
}
public ResultSet execQuery(String sql) {
try {
rs=stmt.executeQuery(sql);
/* if(rs.next()) {
isDouble=true;
System.out.println(" execQuery's result :"+rs.getString(1));}
else{
System.out.println("there is no record which you wanted!");
}*/
// stmt.close();
// rs.close();
}catch (SQLException ex) {
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println("SQLState:" +ex.getSQLState());
System.out.println(" Message :" +ex.getMessage());
System.out.println(" Vendor :" +ex.getErrorCode());
ex = ex.getNextException ();
}
}catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
return rs;
}

private boolean checkForWarning (SQLWarning warn) throws SQLException
{
// System.out.println("DBAccess checkForWarning");
boolean rc = false;

if (warn != null) {
System.out.println ("\n *** Warning ***\n");
rc = true;
while (warn != null) {
System.out.println("SQLState:" +warn.getSQLState());
System.out.println("Message :" +warn.getMessage());
System.out.println("Vendor :" +warn.getErrorCode());
warn = warn.getNextWarning ();
}
}
return rc;
}
public void disconnect() {
try {
System.out.println("DBAccess disconnect");
rs.close();
stmt.close();
conn.close();
}catch (SQLException ex) {
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println ("SQLState: " +ex.getSQLState());
System.out.println ("Message: " +ex.getMessage());
System.out.println ("Vendor: " +ex.getErrorCode());
ex = ex.getNextException ();
System.out.println ("");
}
}catch (java.lang.Exception ex) { ex.printStackTrace (); }
}

public String arrayToString(String[] arr){
String temp="";
int n=arr.length;
for (int i=0;i<n;i++)
{temp=temp+arr[i]+",";}
temp=temp.substring(0,(temp.length()-1));
return temp;
}
public boolean validate() {
System.out.println(firstName+","+lastName+","+email+","+userName+","+password1+","+zip+","+arrayToString(faveMusic)+","+notify);
boolean allOk=true;
if (firstName.equals("")) {
errors.put("firstName","Please enter your first name");
firstName="";
allOk=false;
}
if (lastName.equals("")) {
errors.put("lastName","Please enter your last name");
lastName="";
allOk=false;
}
if (email.equals("") || (email.indexOf('@') == -1)) {
errors.put("email","Please enter a valid email address");
email="";
allOk=false;
}

if (userName.equals("")) {
errors.put("userName","Please enter a username");
userName="";
allOk=false;
}else{
System.out.println(userName);
String querySQL="select * from register where username='"+userName+"'";
try{
rs=execQuery(querySQL);
if(rs.next()) {
errors.put("userName","The username has been exist,请换一个please change another");
allOk=false;
}
}catch(SQLException ex){
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println("SQLState:" +ex.getSQLState());
System.out.println(" Message :" +ex.getMessage());
System.out.println(" Vendor :" +ex.getErrorCode());
ex = ex.getNextException ();
}
}catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
/*if(isDouble){
errors.put("userName","The username has been exist,请换一个please change another");
allOk=false;
}*/
}

if (password1.equals("") ) {
errors.put("password1","Please enter a valid password");
password1="";
allOk=false;
}
if (!password1.equals("") && (password2.equals("") ||
!password1.equals(password2))) {
errors.put("password2","Please confirm your password");
password2="";
allOk=false;
}
if (zip.equals("") || zip.length() !=5 ) {
errors.put("zip","Please enter a valid zip code");
zip="";
allOk=false;
} else {
try {
int x = Integer.parseInt(zip);
} catch (NumberFormatException e) {
errors.put("zip","Please enter a valid zip code");
zip="";
allOk=false;
}
}
if (allOk){
System.out.println("prepare to insert :");
System.out.println(firstName+","+lastName+","+email+","+userName+","+password1+","+zip+","+notify);
String strSQL="insert into register (username,password,firstname,lastname,email,zip,favorite,acception) " ;
strSQL=strSQL+"values('"+userName+"','"+password1+"','"+firstName+"','"+lastName+"','"+email+"','"+zip+"','"+arrayToString(faveMusic)+ "'," +true +")";
//firstName+lastName+email+userName+password1+zip+notify
System.out.println("strSQl: "+strSQL);
int n=execUpdate(strSQL);
System.out.println("insert "+n+"record");
if (n==0){
allOk=false;
errors.put("DB Access","Sorry,DataBase access failed!");
}

}
return allOk;
}


public String getErrorMsg(String s) {
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}

public FormBean() {
System.out.println("create FormBean ok");
firstName="";
lastName="";
email="";
userName="";
password1="";
password2="";
zip="";
faveMusic = new String[] { "1" };
notify="";
errors = new Hashtable();
connect();

}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public String getUserName() {
return userName;
}

public String getPassword1() {
return password1;
}

public String getPassword2() {
return password2;
}

public String getZip() {
return zip;
}

public String getNotify() {
return notify;
}

public String[] getFaveMusic() {
return faveMusic;
}

public String isCbSelected(String s) {
boolean found=false;

if (!faveMusic[0].equals("1"))
{
for (int i = 0; i < faveMusic.length; i++)
{
if (faveMusic[i].equals(s)) {
found=true;
break;
}
}
if (found) return "checked";
}
return "";
}

public String isRbSelected(String s) {
return (notify.equals(s))? "checked" : "";
}

public void setFirstName(String fname) {
firstName =fname;
}

public void setLastName(String lname) {
lastName =lname;
}

public void setEmail(String eml) {
email=eml;
}

public void setUserName(String u) {
userName=u;
}

public void setPassword1(String p1) {
password1=p1;
}

public void setPassword2(String p2) {
password2=p2;
}

public void setZip(String z) {
zip=z;
}

public void setFaveMusic(String[] music) {
faveMusic=music;
}

public void setErrors(String key, String msg) {
errors.put(key,msg);
}

public void setNotify(String n) {
notify=n;
}
}

acool 2001-08-04
  • 打赏
  • 举报
回复
我觉得大家好想没有理解他的意思,他说的是讲用户名和密码当作参数传进bean中,如果不正确则谈出对话框,其实很简单的,相信你在bean中怎么调数据库查询密码什么的肯定会了,如果迷吗不正确,这样就可以了out.print(<script language =\"JAVASCRIPT\">
alert(\"invalid password & username!!!\");
</script>),这样就可以轻松实现了,不用每次都写代码,不知道说的对不对,呵呵
kangaroo 2001-08-03
  • 打赏
  • 举报
回复
如果找你说的 ,那这个JAVABEAN又怎么写呢?
tjf1117 2001-08-03
  • 打赏
  • 举报
回复
这种判断程序,只会在登陆页面有,为什么非要写到javabean里面?
只用jsp也能实现,
如果你在jsp里面会写,javabean也是一样的。
tjf1117 2001-08-02
  • 打赏
  • 举报
回复
胡说,怎么可能这样用?
用户输入密码后,要post,然后web服务器才能收到信息,去数据库里面查,不正确再返回信息,一般都是一个密码错误的页面,
为什么要用客户端的javascript程序,来显示一个对话框?
你见那个网站这么做了?奇了怪了。
kangaroo 2001-08-02
  • 打赏
  • 举报
回复
具体怎么做啊,不管用什么实现?
otstar 2001-08-02
  • 打赏
  • 举报
回复
要用javabean,必须使用数组记录每个登陆用户的信息,需要好的算法
iaroyiaroy 2001-08-02
  • 打赏
  • 举报
回复
哪有这么做的.都是用session
kangaroo 2001-08-02
  • 打赏
  • 举报
回复
?????????????????
ExitWindows 2001-08-01
  • 打赏
  • 举报
回复
up
kangaroo 2001-08-01
  • 打赏
  • 举报
回复
?????????????????
ExitWindows 2001-07-31
  • 打赏
  • 举报
回复
up
ExitWindows 2001-07-31
  • 打赏
  • 举报
回复
up
ExitWindows 2001-07-31
  • 打赏
  • 举报
回复
up

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧