java web写的jsp登陆页面中如何匹配数据库

就是一个jsp登陆页面中,输入用户名和密码,如何连接上数据库并且与数据库中储存的用户名和密码匹配,求助各位大神。

11个回答

jsp的用户名或者密码,到servlet通过request.getparameter(" xx");获取,通过jdbc存到数据库中。 http://blog.csdn.net/u013829202/article/details/52461437

不就是写jdbc么,百度一大推。

前端校验完成后,ajax发送后台,后台处理与数据库校验

可以尝试通过jdbc连接数据库,并将用户输入的信息与用户的数据库信息进行比较。慕课有关于java的视频教程。

1、将jsp页面的用户名密码传到servlet
2、链接数据库,根据用户名读取当前用户信息;(Java连接数据库读取数据,百度即可)
3、比较传入的密码是否和读取到的用户密码相同;
4、返回结果

直接贴代码吧,写的是有点low,不过还能用用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page import="java.sql.*"%> 
<%!  
    public static final String DRIVER = "com.mysql.jdbc.Driver";  
    public static final String USER = "root";  
    public static final String PASS = "123456";  
    public static final String URL = "jdbc:mysql://localhost/biyesheji?characterEncoding=utf8";  
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <style type="text/css">
    table.hovertable {
        font-family: verdana,arial,sans-serif;
        font-size:18px;
        color:#333333;
        border-width: 1px;
        border-color: #999999;
        border-collapse: collapse;
    }
    table.hovertable th {
        background-color:#c3dde0;
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #a9c6c9;
    }
    table.hovertable tr {
        background-color:#d4e3e5;
    }
    table.hovertable td {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #a9c6c9;
    }
    </style>
    <!-- PAGE TITLE -->
    <title>学生成绩查询</title>
    <%@ include file="style.jsp" %>

</head>

<body>
    <!-- ===================================
        PRELOADER
    ==================================== -->
    <div class="preloader">
        <div class="status"></div>
    </div>

    <!-- ===================================
        HEADER
    ==================================== -->
    <header>
        <!-- Navigation Menu start-->
        <nav class="navbar clean-main-menu" role="navigation">
            <div class="container">

                <!-- Navbar Toggle -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <!-- Logo -->
                    <a class="navbar-brand" href="index.html"><img style="width:200px" class="logo" id="logo" src="<%=request.getContextPath()%>/images/logo.jpg" alt="Page"></a>

                </div>
                <!-- Navbar Toggle End -->

                <!-- navbar-collapse start-->
                <div id="nav-menu" class="navbar-collapse collapse" role="navigation">
                    <ul class="nav navbar-nav clean-menu-wrapper">
                        <li>
                            <a href="<%=request.getContextPath()%>/student">个人中心</a>
                        </li>
                        <li>
                            <a href="<%=request.getContextPath()%>/student/chooseclass">选课管理</a>
                        </li>
                        <li class="active">
                            <a href="<%=request.getContextPath()%>/student/mark">成绩查询</a>
                        </li>
                        <li>
                            <a href="<%=request.getContextPath()%>/student/subject">班级课表</a>
                        </li>
                        <li>
                            <a href="<%=request.getContextPath()%>/student/teache">教师评价</a>
                        </li>      
                    </ul>

                </div>
                <%@ page import="java.io.*,java.util.*" %>
                <% String username = (String)session.getAttribute("username");%>
                <p style="float:right;"><a href="<%=request.getContextPath()%>/login">
                <% 
                    if(username != null){
                        out.print("<a href=\"\">"+username+"</a>"+",<a href=\"/biyesheji/login/logout\">"+"注销</a>");
                    } else out.print("<a href=\"/biyesheji/login\">【登陆】</a>");
                %></p>
            </div>
        </nav>
        <!-- Navigation Menu end-->
    </header>

    <!-- ===================================
        MAIN
    ==================================== -->

  <section id="contact" class="clean-section-wrapper clean-contact-section background-one" data-stellar-background-ratio="0.5">
        <div class="clean-parallax-overlay"></div>
        <div class="container">
            <div class="row">

                <!-- Section Header -->
                <div class="col-sm-8" style="margin-top: 50px;">

                    <table class="hovertable">
                        <tr>
                            <th>课程代号</th><th>课程名称</th><th>任课教师</th><th>成绩</th>
                        </tr>   
<%  
    String user = null;  
    String pass = null;  
    try{  
        Class.forName(DRIVER);  
        Connection con = DriverManager.getConnection(URL,USER,PASS);  
        String sql = "SELECT * FROM mark WHERE username=\""+username+"\"";  
        PreparedStatement stat = con.prepareStatement(sql);  
        ResultSet rs = stat.executeQuery();    

        while(rs.next()){         
            String classnumber = rs.getString("classnumber");
            String classname = rs.getString("classname");
            String teachername = rs.getString("teachername");
            int mark = rs.getInt("mark");
            int id = rs.getInt("id");

            out.println("<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">");
            out.println("<td>"+classnumber+"</td><td>"+classname+"</td><td>"+teachername+"</td><td>"+mark+"</td>");
            out.println("</tr>");   
        } 
        con.close();
        }
        catch(Exception e){  

    }  
%>                                      
                    </table>          
                </div>
            </div>      
        </div>



    </section>
    <!-- ===================================
        FOOTER SECTION
    ==================================== -->
    <%@ include file="script.jsp" %>

</body>
</html>

这个需要JDBC、Ajax技术。
输入账号密码后触发JavaScript里的ajax方法,然后到Java后台调用程序,查出和账号匹配的账号、密码。
若数据查不到和账号匹配,直接弹出账号错,
若账号正确,密码不正确,则弹出密码错。

写一个jdbc和数据库连接,在写dao方法进行账号密码判定最后根据返回的值(true|false)判断账号密码是否正确

连接上数据库并且与数据库中储存的用户名和密码匹配,就是用Java的JDBC操作数据库

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!