首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

添加普通登陆解决方法

2012-02-21 
添加普通登陆开始设计一个系统,首页没有登陆界面,只有管理员在管理界面登陆后直接跳转后到管理界面,原来管

添加普通登陆
开始设计一个系统,首页没有登陆界面,只有管理员在管理界面登陆后直接跳转后到管理界面,原来管理员有一个表,现在重新建一个普通人员登陆表,怎样改写如下的登陆JAVABEAN可以实现,设计后的登陆界面是有选择的,如果是选择管理员就登陆到管理界面,如果是普通人员登陆就转到首页。


/*
  *   Created   on   2004-9-20
  *
  *   To   change   the   template   for   this   generated   file   go   to
  *   Window>Preferences>Java>Code   Generation>Code   and   Comments
  */
package   util;
import   news.sql_data;
import   news.strFormat;
import   java.sql.*;

/**
  *   Title:               用户登录信息检查
  *   Description:
  *   Copyright:         Copyright   (c)   2004
  *   @author:   ning
  *   @version   1.0
  */

public   class   login       {
     
private   String   username;//登录用户名
private   String   passwd;//登录密码
private   long   userid=0;//用户ID号
private   String   authority;       //用户权限
String   sqlStr;                             //检索数据库SQL语句
sql_data   sqlbean=new   sql_data();
public   static   void   main   (String   []   args)   {}
public   login()   throws   Exception{
username   =   " ";
passwd   =   " ";
}

public   String   getUsername()   {
return   username;
}
public   void   setUsername(String   newusername)   {
username   =   newusername;
}

public   String   getPasswd()   {
return   passwd;
}
public   void   setPasswd(String   newpasswd)   {
passwd   =   newpasswd;
}

public   long   getUserid()   {
return   userid;
}
public   String   getAuthority()   {
return   authority;
}
public   void   setUserid   (long   uid)   {
userid   =   uid;
}

public   String   getSql()   {

sqlStr   =   "select   *   from   admin   where   adminuser   =   ' "   +   strFormat.toSql(username)   +   " '   and   adminpass   =   ' "   +   strFormat.toSql(passwd)   +   " ' ";

return   sqlStr;
}
public   boolean   excute()   throws   Exception   {
boolean   flag   =   false;
ResultSetrs   =   sqlbean.executeQuery(getSql());
if   (rs.next()){
userid   =   rs.getLong( "id ");
authority=rs.getString( "authority ");
flag   =   true;
}
rs.close();
sqlbean.closeConn();
return   flag;
}
public   void   setinfo(String   lastip,String   username)   throws   Exception   {

  sqlbean.executeUpdate( "UPDATE   admin   SET   lasttime   =   getdate(),lastip= ' "+lastip+ " '   where   adminuser= ' "+username+ " '   ");

sqlbean.closeConn();  
}
}



[解决办法]
登陆的JSP文件如下


<%@ page errorPage= "error.jsp " %>
<%@ page contentType= "text/html; charset=gb2312 " %>
<%@ page language= "java " import= "java.sql.* "%>
<jsp:useBean id= "sqlbean " scope= "page " class= "news.sql_data "/>
<jsp:useBean id= "login " scope= "page " class= "util.login "/>
<%String mesg = " ";
if( request.getParameter( "username ")!=null && !request.getParameter( "username ").equals( " ")){
String username =request.getParameter( "username ");
String passwd = request.getParameter( "passwd ");
login.setUsername(username);
login.setPasswd(passwd);
boolean flag=login.excute();
if (flag!=false){
session.setAttribute( "admin ",username);
session.setAttribute( "authority ",login.getAuthority());
String lastip=request.getRemoteAddr();
login.setinfo(lastip,username);
response.sendRedirect( "index.jsp ");
}else {
mesg = "登录出错! ";
}
}
%>

<%if (request.getParameter( "action ")!=null) {
session.removeAttribute( "admin ");
session.removeAttribute( "authority ");
response.sendRedirect( "login.jsp ");
}%>

<html>
<head>
<title> 企业内部管理信息平台 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<script language= "javascript ">
function checkform() {
if (document.form1.username.value== " " || document.form1.passwd.value== " "){
alert( "用户名或密码为空! ");
return false;
}
return true;

}
</script>
</head>

<body text= "#000000 ">
<div align= "center ">
<table width= "100% " border= "0 " height= "100% " cellspacing= "0 " cellpadding= "0 ">
<tr>
<td align= "center ">
<form name= "form1 " method= "post " action= "login.jsp ">


<table width= "360 " border= "1 " cellspacing= "2 " cellpadding= "2 " bgcolor= "#66CCFF " bordercolor= "#66CCFF " style= "font-size:9pt ">
<tr align= "center ">
<td colspan= "2 ">
<h3> <br>
<font color= "#FF0000 "> 系统登录 </font> </h3>
<p>
<select name=select1>
<option value= "manager " select> 管理员
<option value= "employee "> 普通员工
</select>
</p> </td>
</tr>
<tr>
<td align= "right " width= "150 "> 帐号: </td>
<td>
<input type= "text " name= "username " size= "12 " maxlength= "20 ">
</td>
</tr>
<tr>
<td align= "right " width= "150 "> 密码: </td>


<td>
<input type= "password " name= "passwd " size= "12 " maxlength= "20 ">
</td>
</tr>
<tr align= "center ">
<td colspan= "2 ">
<input type= "submit " name= "Submit " value= "登录 " onclick= "javascript:return(checkform()); ">
<input type= "reset " name= "Submit2 " value= "取消 ">
</td>
</tr>

</table>
</form>
<p> &nbsp; </p>
CopyRight@2004 <br>
</td>
</tr>
</table>
</div>
</body>
</html>

热点排行