admin 管理员组文章数量: 887021
2024年1月16日发(作者:defaultstatus)
} } if (statement != null) { try { (); } catch (SQLException e) { tackTrace(); } } if (rs != null) { try { (); } catch (SQLException e) { tackTrace(); } } }}登录servletpackage ;import ption;import tException;import ;import rvlet;import rvletRequest;import rvletResponse;public class Login extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { tentType("text/html;charset=UTF-8"); racterEncoding("UTF-8"); Cookie[] cookies = kies(); String username = ""; String password = ""; String chck=""; if (cookies != null) { for (Cookie cookie : cookies) { if (e().equals("username")) { username = ue(); chck ="checked='checked'"; } if (e().equals("password")) { password = ue(); } } } ter().write("
"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}处理登录请求servletpackage ;import ption;import riter;import edStatement;import Set;import eption;import tion;import tException;import ;import rvlet;import rvletRequest;import rvletResponse;public class work1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { tentType("text/html;charset=UTF-8"); racterEncoding("UTF-8"); PrintWriter writer = ter(); String sql = "select * from users where username=? and password=?"; String username = ameter("username"); String password = ameter("password"); String chck = ameter("chck");
Connection connection = nection(); PreparedStatement prepareStatement = null; try { prepareStatement = eStatement(sql); ing(1, username); ing(2, password); ResultSet rs = eQuery(); while (()) { String user = ing("username"); String pw = ing("password"); if ((username) && (password)) { ter().write("登录成功"); Cookie cookie1 = new Cookie("username", username); Cookie cookie2 = new Cookie("password", password); h("/"); h("/"); if (chck != null) { Age(60*5); Age(60*5); } else { Age(0); Age(0); } kie(cookie1); kie(cookie2); ter().write(username); ter().write(password); } else { ter().write("登录失败嗷"); der("refresh", "3;/day33work/login"); } } } catch (SQLException e) { tackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
}配置 ml/ns/javaee /xml/ns/javaee/web-app_3_" id="WebApp_ID" version="3.0">
版权声明:本文标题:servlet连接数据库实现登录 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705406622h483659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论