开发实战练习1 jsp+jdbc 登陆程序

来源:互联网 发布:手机上做班服的软件 编辑:程序博客网 时间:2024/04/19 08:12

使用jsp+jdbc完成一个用户登陆程序,登录成功后可以使用session进行用户的登录验证,用户根据需要也可以直接进行系统的退出操作。本程序在mysql数据库里建立了一个member表。

mid varchar pk,

password varchar,

name varchar.


login.jsp:

<%@pagecontentType="text/html; charset=utf-8"%>

<html>

<head><title>登录页面</title></head>

<body>

<center>

<%=request.getAttribute("info")!=null?request.getAttribute("info"):""%>

<h1>登陆页面</h1>

<formaction="check.jsp"method="post">

用户名:<inputtype="text"name="mid"><br>

&nbsp;&nbsp;&nbsp;&nbsp;码:<inputtype="password"name="password"><br>

<inputtype ="submit"value="登录">

<inputtype ="reset"value="重置">

</form>

</center>

</body>

</html>



check.jsp:

<%@pagecontentType="text/html; charset=utf-8"%>

<%@pageimport="java.sql.*"%>

<html>

<head><title>验证页面</title></head>

<body>


<%!

publicstatic final String DBDRIVER ="org.gjt.mm.mysql.Driver";

publicstatic final String DBURL ="jdbc:mysql://localhost:3306/mysql";

public static final String DBUSER = "root";

public static final String DBPASS = "123456";

%>

<%

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

boolean flag =false;

String name = null;

%>

<%

Class.forName(DBDRIVER);

conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);

String mid = request.getParameter("mid");

String password = request.getParameter("password");

String sql = "select name from member where mid=? and password=?";

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, request.getParameter("mid"));

pstmt.setString(2, request.getParameter("password"));

rs = pstmt.executeQuery();

if(rs.next()){

session.setAttribute("id", mid);

flag = true;

}

rs.close();

pstmt.close();

conn.close();

%>

<%

if(flag){

%>

<jsp:forwardpage="welcome.jsp"/>

<%

}else{

request.setAttribute("info","错误的用户名或密码");

%>

<jsp:forwardpage="login_failure.jsp"/>

<% 

 

%>

</body>

</html>


welcome.jp:

<%@pagecontentType="text/html; charset=utf-8"%>

<html>

<head><title>welcome</title></head>

<body>

<center>

<h1>登陆程序</h1>

<%

if(session.getAttribute("id")!=null){

%>

<h2>欢迎<fontcolor="RED"><%=session.getAttribute("id")%></font>光临!</h2>

<h3><ahref="logout.jsp">登陆注销</a></h3>

<%

}else{

request.setAttribute("info","请先登陆!");

%>

<jsp:forwardpage="login.jsp"/>

<%

}

%>

</center>

</body>

</html>


logout.jsp:

<%@pagecontentType="text/html; charset=utf-8"%>

<html>

<head><tile>logout</head>

<body>

<center>

<%

response.setHeader("refresh","2;URL=login.jsp");

session.invalidate();//让session失效

%>

<h3>您已经成功退出本系统,两秒后跳转回首页!</h3>

<h3>如果没有跳转,请按<ahref="login.jsp">这里</a></h3>

<center/>

</body>

</html>


0 0
原创粉丝点击