登錄失敗信息回顯不會的新的一個頁面,而是顯示在登錄頁面 一種方法是: 登錄頁面表單中每個欄位後添加<span>標簽寫入失敗信息,利用ajax技術 通過改變<span>標簽的display:none屬性添加失敗信息 這裡用的類似的方法: 資料庫準備略, 註意寫好對應的user類供BeanHandler ...
登錄失敗信息回顯不會的新的一個頁面,而是顯示在登錄頁面
一種方法是:
登錄頁面表單中每個欄位後添加<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>
即可實現簡單的信息回顯