Servlet案例5:用户登录失败信息回显

时间:2021-01-01 18:40:11

登录失败信息回显不会的新的一个页面,而是显示在登录页面

一种方法是:

登录页面表单中每个字段后添加<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);
}
}

抽取连接池类:

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();
}
} }

注意写好c3p0-config.xml、web.xml配置文件

在登录页面加上这一句代码

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

即可实现简单的信息回显