zoukankan      html  css  js  c++  java
  • model1纯JSP(实现分页)

    Login.jsp  ----LoginCL.jsp-----跳转

    Login.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 action="LoginCl.jsp" method="post">
    <table >
                    <tr><td>用户名</td><td><input type="text" name="username"/></td></tr>
                    <tr><td>密    码</td><td><input type="text" name="password"/></td></tr>
                    <tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="清空"/></td></tr>
                </table>
    </form>
    </body>
    </html>
    View Code

    LoginCL.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        import="java.sql.*"
        %>
        
    <!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>Insert title here</title>
    </head>
    <body>
    <%
    String name=request.getParameter("username");
    String password=request.getParameter("password");
    
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
    
        String sql = "select* from user where username=? and password=?";
        ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, password);
       out.println(sql);
        // 结果
        rs = ps.executeQuery();
        if (rs.next()) {
            out.println(rs.getString(1) + " " + rs.getString(2));
        } else {
           out.println("登陆失败");
        }
    %>
    </body>
    </html>
    View Code

     改进版:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        import="java.sql.*"
        %>
        
    <!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>Insert title here</title>
    </head>
    <body>
    <%
    String name=request.getParameter("username");
    String password=request.getParameter("password");
    
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
    
        String sql = "select* from user where username=? and password=?";
        ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, password);
        // 结果
        rs = ps.executeQuery();
        if (rs.next()) {
            if(rs.getString(2).equals(name)&&rs.getString(5).equals(password)){
                out.print("登陆成功");
            }else{
                out.print("账号或者密码错误");
            } 
        } else {
           out.println("登陆失败,重新登陆");
        }}catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }}
    %>
    </body>
    </html>
    View Code

     标准版:

    LoginCL.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        import="java.sql.*"
        %>
        
    <!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>Insert title here</title>
    </head>
    <body>
    <%
    String name=request.getParameter("username");
    String password=request.getParameter("password");
    
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
    
        String sql = "select password from user where username=? ";
        ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        // 结果
        rs = ps.executeQuery();
        if (rs.next()) {
            if(rs.getString(1).equals(password))
                {out.print("登陆成功");
                response.sendRedirect("Main.jsp?user="+name);
                }else{
                    out.print("密码错误");
                    response.sendRedirect("Login.jsp?error=1");    
                }
        } else {
            response.sendRedirect("Login.jsp?error=2");    
           out.println("账号错误 ,重新登陆");
        }}catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }}
    %>
    </body>
    </html>
    View Code

    Login.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 action="LoginCl.jsp" method="post">
    <table >
                    <tr><td>用户名</td><td><input type="text" name="username"/></td></tr>
                    <tr><td>密    码</td><td><input type="text" name="password"/></td></tr>
                    <tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="清空"/></td></tr>
                </table>
    </form>
    </body>
    </html>
    View Code

    Main.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>Insert title here</title>
    </head>
    <body>
    欢迎来到新的页面!
    </body>
    </html>
    View Code

    实现分页版:

    Login.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 action="LoginCl.jsp" method="post">
    <table >
                    <tr><td>用户名</td><td><input type="text" name="username"/></td></tr>
                    <tr><td>密    码</td><td><input type="text" name="password"/></td></tr>
                    <tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="清空"/></td></tr>
                </table>
    </form>
    </body>
    </html>
    View Code

    LoginCl.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        import="java.sql.*"
        %>
        
    <!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>Insert title here</title>
    </head>
    <body>
    <%
    String name=request.getParameter("username");
    String password=request.getParameter("password");
    out.print(name+"  "+password);
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
    
        String sql = "select password from user where username=? ";
        ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        // 结果
        rs = ps.executeQuery();
        if (rs.next()) {
            if(rs.getString(1).equals(password))
                {out.print("登陆成功");
                response.sendRedirect("http://localhost:8080/practice/Welcom.jsp?pageNow=1");
                }else{
                    out.print("密码错误");
                    response.sendRedirect("Login.jsp?error=1");    
                }
        } else {
            response.sendRedirect("Login.jsp?error=2");    
           out.println("账号错误 ,重新登陆");
        }}catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }}
    %>
    </body>
    </html>
    View Code

    Welcom.jsp(分页)

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        import="java.sql.*"
        %>
    <!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>Insert title here</title>
    </head>
    <body>
    欢迎来到新的页面! <a href="Login.jsp">返回主页面</a>
       <hr/>
    <%
    //定义分页可以用到的变量
    int pageSize=3;
    int pageNow=1;
    int rowCount=0;
    int pageCount=0;
    String page1=request.getParameter("pageNow");
    if(page!=null){
        pageNow=Integer.parseInt(page1);
    }
    String name=request.getParameter("user");
    //查询得到
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
    
        String sql = "select count(*) from user";
        ps = conn.prepareStatement(sql);
        // 结果
        rs = ps.executeQuery();
        if (rs.next()) {//next不要忘了
            rowCount=rs.getInt(1);
            out.print(rowCount);
        }     
        ps.close();
        rs.close();
        //计算PageCount
        if(rowCount%pageSize==0){
            pageCount=rowCount/pageSize;
        }else {
            pageCount=rowCount/pageSize+1;
        }
        out.print("pageCount="+pageCount);
        //查询出需要显示的页面
        if(rowCount>=pageNow*pageSize){
            sql="select *from user limit "+(pageNow-1)*pageSize+","+pageSize;        
        }
        else{
            sql="select *from user limit "+(pageNow-1)*pageSize+","+(rowCount-(pageNow-1)*pageSize);
        }
        out.print(sql);
                ps = conn.prepareStatement(sql);
        // 结果
        rs = ps.executeQuery();  
       %>
       <table border="1px" >
      <tr><td>用户ID</td><td>用户名</td><td>邮箱</td><td>等级</td><td>密码</td></tr>
       <%
       while(rs.next()){
          %>
          <tr><td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td><td><%=rs.getString(3)%></td><td><%=rs.getInt(4)%></td><td><%=rs.getString(5)%></td></tr>
          <%  
       }
       %>
       </table>
       <%
       if(pageNow!=1){
       out.print("<a href='Welcom.jsp?pageNow="+(pageNow-1)+"'>上一页</a>");}
       //显示超链接
       for(int i=1;i<=pageCount;i++){
           out.print("<a href='Welcom.jsp?pageNow="+i+"'>["+i+"]</a>");
       }
       if(pageNow!=pageCount){
       out.print("<a href='Welcom.jsp?pageNow="+(pageNow+1)+"'>下一页</a>");}
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
    %>
    <hr/>
    </body>
    </html>
    View Code

     缺点分析;

  • 相关阅读:
    进制
    流程控制
    运算符
    格式化输出
    数据结构-树的遍历
    A1004 Counting Leaves (30分)
    A1106 Lowest Price in Supply Chain (25分)
    A1094 The Largest Generation (25分)
    A1090 Highest Price in Supply Chain (25分)
    A1079 Total Sales of Supply Chain (25分)
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/11052328.html
Copyright © 2011-2022 走看看