登录页
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.xiongsheng.com,熊胜的主页</title></head>
<body>
<center>
<%
request.setCharacterEncoding("GBK") ;
%>
<h1><font color="red">登陆程序</font></h1>
<hr>
<hr>
<%=request.getAttribute("info")!=null?request.getAttribute("info"):""%>
<form action="check.jsp" method="post">
用户ID:<input type="text" name="mid"><br>
密 码:<input type="password" name="password"><br>
保存密码: <select name="savetime">
<option value="0">不保存</option>
<option value="<%=3600 * 24%>"selected>保存一天</option>
<option value="<%=3600 * 24 * 10%>">保存十天</option>
<option value="<%=3600 * 24 * 30%>">保存三十天</option>
</select><br>
验证码:<input type="text" name="code" maxlength="4" size="4">
<img src="image.jsp">
<br>
<input type="submit" value="登陆">
<input type="reset" value="重置">
</form>
</center>
</body>
</html>
检查及保存页
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<html>
<head><title>www.xiongsheng.com,熊胜的主页</title></head>
<body>
<center>
<%
request.setCharacterEncoding("GBK") ;
%>
<h1><font color="red">登陆程序</font></h1>
<hr>
<%
String rand = (String) session.getAttribute("rand") ;
String code = request.getParameter("code") ;
if(!rand.equals(code)){
request.setAttribute("info","请输入正确的验证码!") ;
%>
<jsp:forward page="login.jsp"/>
<%
}
%>
<%!
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
public static final String DBURL = "jdbc:oracle:thin:@xiongsheng :1521:xiongsheng" ;
public static final String DBUSER = "xiong" ;
public static final String DBPASSWORD = "xs1990721" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
String mid = request.getParameter("mid") ;
String password = request.getParameter("password") ;
String sql = "SELECT name FROM member WHERE mid=? AND password=?" ;
%>
<%
boolean flag = false ;
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,mid) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
if(rs.next()){
session.setAttribute("id",mid) ;
Cookie c1 = new Cookie("mid",mid) ;
Cookie c2 = new Cookie("password",password) ;
int saveTime = Integer.parseInt(request.getParameter("savetime")) ;
c1.setMaxAge(saveTime) ;
c2.setMaxAge(saveTime) ;
response.addCookie(c1) ;
response.addCookie(c2) ;
flag = true ;
}
conn.close() ;
%>
<%
if(flag){
%>
<jsp:forward page="welcome.jsp"/>
<%
} else {
request.setAttribute("info","错误的用户名或密码!") ;
%>
<jsp:forward page="login.jsp"/>
<%
}
%>
</center>
</body>
</html>