日期:2014-05-17 浏览次数:20659 次
index.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>登录</title> </head> <body> <form id="form1" name="form1" method="post" action="login.jsp"> <p align="center">用户登录</p> <p align="center">用户名 <label> <input type="text" name="userName" /> </label> </p> <p align="center">密码: <input type="password" name="Password" /> </p> <p align="center"> <input type="submit" name="Submit" value="提交" /> </p> login.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="com.mysql.jdbc.*,java.sql.Connection, java.sql.DriverManager,java.sql.PreparedStatement,java.sql.ResultSet " %> <html> <body> <% String username = request.getAttribute("userName") ; String pass = request.getAttribute("password") ; Class.forName("com.mysql.jdbc.Driver"); //加载驱动 Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.44:3306/javassdb", "root","root");//创建连接 String sql ="select id , username, email , password from tbl_User where username = " + username + " and password=" + pass; PreparedStatement pstmt = null ; ResultSet rs = null ; boolean flag = false ; try{ pstmt = conn.prepareStatement(sql) ; // rs = pstmt.executeQuery() ;//查询 while(rs.next()){ //遍历查询结构,现实在页面中 flag = true ; break ; } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if (rs != null) rs.close() ; if (pstmt != null) pstmt.close() ; if (conn != null) conn.close() ; }catch(Exception e){ e.printStackTrace() ; } } if (flag){ request.getRequestDispatcher("welcome.jsp").forward(request, response) ; }else{ request.getRequestDispatcher(&q