时间:2021-07-01 10:21:17 帮助过:7人阅读
3.在LoginServlet中的 init 初始化函数中加入了数据初始化 ,添加了两个用户进入数据库
public class LoginServlet extends HttpServlet { @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); List<Person> l=new ArrayList<>(); Person p=new Person("root","admin"); l.add(p); p=new Person("123","123"); l.add(p); try { Dao.Insertdata(l); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String zh=req.getParameter("zh"); String mm=req.getParameter("mm"); String info=null; try { List<Person> l=Dao.Selectdata(zh); if(l.size()==0) { info="该用户不存在"; } else { if(l.get(0).getMm().equals(mm)) { info="登录成功:账号"+zh+" -密码:"+mm; } else { info="密码错误"; } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); info=e.getMessage(); } req.getSession(true).setAttribute("info",info); resp.sendRedirect(req.getContextPath()+"/"+"info.jsp"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); } }LoginServlet.java
项目其他源码参考 Servlet登录
百度云下载链接
链接:https://pan.baidu.com/s/1yyoCAWcwvq_c2kO84Kpo0A
提取码:mhe0
Servlet登录二-带数据库
标签:alt add 文件 gif str select static admin from