用户登录

作者:Blueski 来源:中华网 添加时间:2006-5-21 10:28:15
虚拟小龙亭采用了简单的登录方式,在login.jsp中填写并提交登录信息,在login_confirm.jsp中加以验证,如果不合法则返回login.jsp要求重新登录,如果通过则调用passthrough.jsp提供下一步链接。

这里使用的JSP代码包括:

1 调用统一的javabeans接口执行数据库操作。这里是select-SQL。

2 读写session变量,例如:
写入:session.putValue("login_message","Error in Login!");
读取:String getmessage = (String) session.getValue("login_message");

3 在out.print("...")显示信息。

4 文件包含,采用如下方式:
<%@ include file="top.htm" %>

5 文件导向,例如:
<jsp:forward page="login.jsp"/>


以下为login.jsp源代码:

--login.jsp--

<html>
<head>
<title>登录</title>
<link rel=stylesheet href="style.css" type="text/css">
</head>
<script language="JavaScript">
function isValid(form){
if(form.user_id.value==""){
alert("登录名不能为空!");
return false;
}
else if((form.password.value.length<3)||(form.password.value.length>8)){
alert("密码必须是3-8位字母或数字!");
return false;
}
else {
return true;
}
}
</script>

<body>
<%@ include file="top.htm" %>
<center>
<% String getmessage = (String) session.getValue("login_message");
if(getmessage==null) getmessage="";
%>
<FORM METHOD=POST action="login_confirm.jsp" onSubmit="return isValid(this);">
<%= getmessage %>
<TABLE width='60%' height="150" border='1'>
<TR>
<TD colspan=2 align='center'>请您登录:</td>
</TR>
<TR>
<TD align='left'>登录名:</td>
<td><INPUT TYPE="text" NAME="user_id"></TD>
</TR>
<TR>
<TD align='left'>口令:</td>
<td><INPUT TYPE="password" NAME="password"></TD>
</TR>
<tr>
<td colspan=2 align="center">
<INPUT TYPE="submit" size="4" value="登录">
</td>
</tr>
</table>
</form>


</center>
<%@ include file="bottom.htm" %>
</body>
</html>

login_confirm.jsp代码如下:

--login_confirm.jsp--

<html>
<head>
<title>登录检查</title>
<link rel=stylesheet href="style.css" type="text/css">
</head>
<body>
<%@ include file="top.htm" %>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="LoginBean" scope="page" class="mydb.mydb" />
<%
String name1=request.getParameter("user_id");
String pwd1=request.getParameter("password");
String sql="select * from user where name='" + name1 + "' and password='" + pwd1 + "'";
ResultSet rs = LoginBean.executeQuery(sql);
if(rs.next()) { rs.close();
session.putValue("username",name1);
%>
<script language=javascript>
window.location="passthrough.jsp?showword=登录"
</script>
<% } 
else{ rs.close();
session.putValue("login_message","Error in Login!");
%>
<jsp:forward page="login.jsp"/>
<% } %>
<%@ include file="bottom.htm" %>
</body>
</html> 
站内搜索