WEB——简单登案例(获取数据库中数据)

来源:互联网 发布:天尚网络机顶盒遥控器 编辑:程序博客网 时间:2024/06/09 18:57

结构布局


index.jsp页面

<%@page contentType="text/html;charset=UTF-8"%><html><head><title>登陆</title></head><body><center><h4>登录案例</h4><hr><form action="login_conf.jsp" method="post"><table><tr><td colspan="2">用户登陆</td></tr><tr><td>用户名:</td><td><input type="text" name="uname"></td></tr><tr><td>密  码:</td><td><input type="password" name="upassword"></td></tr><tr><td colspan="2"><input type="submit" value="登陆"><input type="reset" value="重置"></td></tr></table></form></center></body></html>

login_conf.jsp

<%@ page contentType="text/html;charset=UTF-8"%><%@ page import="java.sql.*"%><html><head><title>登陆界面</title></head><body><center><%String name = request.getParameter("uname") ;String upassword = request.getParameter("upassword") ;// 定义变量,如果用户是合法用户,则将此标记变为trueboolean flag = false ;%><%Connection conn=null;Statement stmt=null;ResultSet rs= null ;PreparedStatement pstmt= null ;String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//驱动类String username="sa";//数据库用户名String password="123456";//数据库密码String sql="select * from t_images where name=? and password=?" ;//查询语句String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=image";//连接数据库的地址try{Class.forName(driver);//加载驱动器类conn=DriverManager.getConnection(url,username,password);//建立连接//建立处理的SQL语句pstmt = conn.prepareStatement(sql) ;pstmt.setString(1,name) ;pstmt.setString(2,upassword) ;rs = pstmt.executeQuery() ;//形成结果集if(rs.next()){// 如果有记录,则执行此段代码// 用户是合法的,可以登陆flag = true ;}rs.close();//关闭结果集pstmt.close();//关闭SQL语句集conn.close();//关闭连接}//捕获异常catch(ClassNotFoundException e){System.out.print(e);}//捕获异常catch(SQLException ee){System.out.print(ee);} %><%// 判断用户名及密码if(flag){// 合法用户%><jsp:forward page="login_success.jsp"/><%}else{// 非法用户%><jsp:forward page="login_failure.jsp"/><%}%></center></body></html></body></html>


login_failure.jsp

<%@page contentType="text/html;charset=UTF-8"%><html><head><title>登陆</title></head><body><center><h2><font color="red" size="20">登陆失败</font></h2><h3><font color="red"><%=request.getParameter("uname") %>未注册或错误的用户名及密码</h3><a href="index.jsp">重新登陆</a></center></body></html>


login_success.jsp

<%@page contentType="text/html;charset=UTF-8"%><html><head><title>登陆</title></head><body><center><h2>登陆成功</h2><h3>欢迎<font color="red" size="15"><%=request.getParameter("uname")%></font>光临!!!</h3><a href="index.jsp">重新登陆</a></center></body></ht


附上效果图




1 0
原创粉丝点击