登陆、注销

loginUI.jsp 

<!-- 显示表单 -->
<s:form action="user_login" focusElement="loginNameInput">
    <div id="CenterAreaBg"> 
        <div id="CenterArea">
            <div id="LogoImg"><img border="0" src="${pageContext.request.contextPath}/style/blue/images/logo.png" /></div>
            <div id="LoginInfo">
                <table BORDER=0 CELLSPACING=0 CELLPADDING=0 width=100%>
                	<tr>
                		<td colspan="3"><!-- 显示错误 -->
							<font color="red"><s:fielderror/></font>
                		</td>
                	</tr>
                    <tr>
                        <td width=45 class="Subject"><img border="0" src="${pageContext.request.contextPath}/style/blue/images/login/userId.gif" /></td>
                        <td>
                        	<s:textfield name="loginName" size="20" tabindex="1" cssClass="TextField required" id="loginNameInput" />
                        </td>
                        <td rowspan="2" style="padding-left:10px;">
                        	<input type="image" tabindex="3" src="${pageContext.request.contextPath}/style/blue/images/login/userLogin_button.gif" />
                        </td>
                    </tr>
                    <tr>
                        <td class="Subject"><img border="0" src="${pageContext.request.contextPath}/style/blue/images/login/password.gif" /></td>
                        <td><s:password name="password" id="aa" size="20" tabindex="2" showPassword="false" cssClass="TextField required" /></td>
                    </tr>
                </table>
            </div>
            <div id="CopyRight"><a href="javascript:void(0)">© 2010 版权所有 </a></div>
        </div>
    </div>
    </s:form>

UserAction.java

    /**登陆*/
    public String login() throws Exception{
    	User user = userService.findByLoginNameAndPassword(model.getLoginName(),model.getPassword());
    	if(user == null){
    		addFieldError("login", "用户名或密码不正确");
    		return "loginUI" ;
    	}else{
    		//登陆用户
    		ActionContext.getContext().getSession().put("user", user);
    		return "toIndex" ;
    	}
    }
    
    /**注销*/
    public String logOut() throws Exception{  
    	ActionContext.getContext().getSession().remove("user");
    	return "logOut" ;
    }

UserServiceImpl.java

public User findByLoginNameAndPassword(String loginName, String password) {
		//使用密码的MD5摘要进行对比
		String md5Digest = DigestUtils.md5Hex(password);
		return (User) getSession().createQuery(//
				"FROM User u WHERE u.loginName=? AND u.password=?")//
				.setParameter(0, loginName)//
				.setParameter(1, md5Digest)//
				.uniqueResult();
	}

struts.xml

<!-- 用户管理 -->
        <action name="user_*" class="userAction" method="{1}">
        	<result name="list">/WEB-INF/jsp/userAction/list.jsp</result>
        	<result name="toList" type="redirectAction">user_list</result>
        	<result name="saveUI">/WEB-INF/jsp/userAction/saveUI.jsp</result>
        	
        	<result name="loginUI">/WEB-INF/jsp/userAction/loginUI.jsp</result>
        	<result name="logOut">/WEB-INF/jsp/userAction/logout.jsp</result>
        	<result name="toIndex" type="redirect">/index.jsp</result>
        </action>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 Python 的登录、注册和注销功能,可以通过使用数据库来实现。一般来说,我们可以使用 MySQL 或者 SQLite 等数据库来存储用户信息,然后通过 Python 的数据库操作模块来实现用户的注册、登录和注销功能。 具体实现步骤如下: 1. 创建一个数据库,用于存储用户信息,包括用户名、密码等字段。 2. 在 Python 中连接到该数据库,并创建一个表,用于存储用户信息。 3. 实现用户注册功能,即向表中插入一条新的用户信息。 4. 实现用户登录功能,即查询表中是否存在该用户,并验证密码是否正确。 5. 实现用户注销功能,即删除表中该用户的信息。 下面是一个简单的示例代码,仅供参考: ```python import sqlite3 # 连接到数据库 conn = sqlite3.connect('user.db') # 创建一个表 conn.execute('''CREATE TABLE IF NOT EXISTS user (ID INT PRIMARY KEY NOT NULL, USERNAME TEXT NOT NULL, PASSWORD TEXT NOT NULL);''') # 注册功能 def register(username, password): # 查询是否已经存在该用户 cursor = conn.execute("SELECT * FROM user WHERE USERNAME = ?", (username,)) if cursor.fetchone() is not None: print("该用户名已经被注册!") return False # 插入新用户信息 conn.execute("INSERT INTO user (ID, USERNAME, PASSWORD) VALUES (?, ?, ?)", (None, username, password)) conn.commit() print("注册成功!") return True # 登录功能 def login(username, password): # 查询是否存在该用户 cursor = conn.execute("SELECT * FROM user WHERE USERNAME = ?", (username,)) row = cursor.fetchone() if row is None: print("该用户不存在!") return False # 验证密码是否正确 if row[2] != password: print("密码错误!") return False print("登录成功!") return True # 注销功能 def logout(username): # 删除该用户信息 conn.execute("DELETE FROM user WHERE USERNAME = ?", (username,)) conn.commit() print("注销成功!") # 关闭数据库连接 conn.close() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值