Servlet案例5:使用者登入失敗資訊回顯,servlet回顯

來源:互聯網
上載者:User

Servlet案例5:使用者登入失敗資訊回顯,servlet回顯

登入失敗資訊回顯不會的新的一個頁面,而是顯示在登入頁面

一種方法是:

登入頁面表單中每個欄位後添加<span>標籤寫入失敗資訊,利用ajax技術

通過改變<span>標籤的display:none屬性添加失敗資訊

 

這裡用的類似的方法:

資料庫準備略,

注意寫好對應的user類供BeanHandler使用

sevlet:

package login;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;public class LoginServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        // 獲得使用者名稱密碼        String username = request.getParameter("username");        String password = request.getParameter("password");        // 在資料庫中查詢        User login = null;        try {            login = login(username, password);        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        if (login != null) {            // 成功,跳轉首頁            response.sendRedirect(request.getContextPath());        } else {            // 錯誤            // 在request域中存入錯誤資訊            request.setAttribute("loginInfo", "使用者名稱或密碼錯誤");            request.getRequestDispatcher("/login.jsp").forward(request, response);        }    }    public User login(String username, String password) throws SQLException {        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());        String sql = "select * from user where username=? and password=?";        User user = runner.query(sql, new BeanHandler<User>(User.class), username, password);        return user;    }    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}
View Code

 

抽取串連池類:

package login;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DataSourceUtils {    private static DataSource dataSource = new ComboPooledDataSource();    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();    // 直接可以擷取一個串連池    public static DataSource getDataSource() {        return dataSource;    }    // 擷取連線物件    public static Connection getConnection() throws SQLException {        Connection con = tl.get();        if (con == null) {            con = dataSource.getConnection();            tl.set(con);        }        return con;    }    // 開啟事務    public static void startTransaction() throws SQLException {        Connection con = getConnection();        if (con != null) {            con.setAutoCommit(false);        }    }    // 交易回復    public static void rollback() throws SQLException {        Connection con = getConnection();        if (con != null) {            con.rollback();        }    }    // 提交並且 關閉資源及從ThreadLocall中釋放    public static void commitAndRelease() throws SQLException {        Connection con = getConnection();        if (con != null) {            con.commit(); // 事務提交            con.close();// 關閉資源            tl.remove();// 從線程綁定中移除        }    }    // 關閉資源方法    public static void closeConnection() throws SQLException {        Connection con = getConnection();        if (con != null) {            con.close();        }    }    public static void closeStatement(Statement st) throws SQLException {        if (st != null) {            st.close();        }    }    public static void closeResultSet(ResultSet rs) throws SQLException {        if (rs != null) {            rs.close();        }    }}
View Code

 

注意寫好c3p0-config.xml、web.xml設定檔

 

在登入頁面加上這一句代碼

<div><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo")%></div>

 

即可實現簡單的資訊回顯

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.