您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > jsp+jdbc+servlet登陆完整程序和注解
Jsp+Dao+Jdbc+Servelet用户完整登录总结首先我们要准备两个页面,即(login)index.jsp和login_success.jspIndex.jsp实现用户登录页面,login_success.jsp实现用户登录成功的页面显示用户XX登录成功!登录步骤:用户填写好用户名和密码后发出登录请求给servlet(tomcat启动要加载web.xml文件,web.xml会根据index的action找到web.xml中的servlet,然后由servlet控制分发请求调用登录方法,Index.jsp--web.xml---servlet.java---dao---daoImpl--servlet--login_success.jspIndex.jsp页面如下:%@pagelanguage=javaimport=java.util.*pageEncoding=gb2312%%@pageimport=java.util.*%htmlheadtitle用户登录/title/headbody%if(request.getAttribute(Error)!=null){//有错误,要进行打印输出Listall=(List)request.getAttribute(Error);Iteratoriter=all.iterator();while(iter.hasNext()){%li%=iter.next()%%}}%formaction=servlet/loginServletmethod=Posttablealign=CENTERtrtdcolspan=2align=CENTERh3fontcolor=#0000ffstrong用户登录/strong/font/h3/td/trtrtdalign=right用户名:/tdtdinputtype=textname=username//td/trtrtd密  码:/tdtdinputtype=passwordname=password//td/trtrtdalign=centerinputtype=Submitname=submitvalue=提交//tdtdalign=centerinputtype=Resetname=resetvalue=重置//td/tr/table/form/body/html如下web.xml文件?xmlversion=1.0encoding=gb2312?web-appversion=2.4xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/j2ee/web-app_2_4.xsdservletdescriptionThisisthedescriptionofmyJ2EEcomponent/descriptiondisplay-nameThisisthedisplaynameofmyJ2EEcomponent/display-nameservlet-nameloginServlet/servlet-nameservlet-classcom.lgy.servlet.loginServlet/servlet-class/servletservlet-mappingservlet-nameloginServlet/servlet-nameurl-pattern/servlet/loginServlet/url-pattern/servlet-mappingwelcome-file-listwelcome-fileindex.jsp/welcome-file/welcome-file-list/web-app注意Web.xm中的url-pattern/servlet/loginServlet/url-pattern和index.jsp中的action=“servlet/loginServlet”必须一致下面是javaBean所谓的VO(POJO)主要负责对数据库访问packagecom.lgy.vo;importjava.util.List;publicclasslogin{privateintid;privateStringusername;privateStringpassword;privateListerrors;publicbooleaninvalidate(){booleanflag=true;/***1.先验证输入不为空,在验证合法的输入格式。*如果输入不为空,那么进行下一项合法的格式的验证*/System.out.println(这是pojo得到的用户名********:+this.username);if(this.username==null||.equals(this.username)){flag=false;errors.add(用户名不能为空!);}//合法格式的验证else{if(this.username.length()3||this.username.length()10){flag=false;errors.add(您应该输入合法的用户名,长度在3~10之间!);}}if(this.password==null||.equals(this.password)){flag=false;errors.add(密码不能为空!);}else{if(this.password.length()3||this.password.length()10){flag=false;errors.add(您应该输入合法的密码,的长度在3~10之间!);}}returnflag;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicListgetErrors(){returnerrors;}publicvoidsetErrors(Listerrors){this.errors=errors;}}下面是数据库的链接类负责链接数据库//本类只用于数据库连接及关闭操作packagecom.lgy.db;importjava.sql.*;publicclassMysqlConnection{//属性//定义数据库操作的常量、对象//数据库驱动程序privatefinalStringDBDRIVER=com.mysql.jdbc.Driver;//数据库连接地址privatefinalStringDBURL=jdbc:mysql://localhost:3306/test;//数据库用户名privatefinalStringDBUSER=root;//数据库连接密码privatefinalStringDBPASSWORD=root;//声明一个数据库连接对象privateConnectionconn=null;//在构造方法之中连接数据库publicMysqlConnection(){try{//加载驱动程序Class.forName(DBDRIVER);//连接数据库conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);}catch(Exceptione){System.out.println(e);}}//返回一个数据库连接publicConnectiongetConnection(){///返回连接对象returnthis.conn;}//关闭数据库连接publicvoidclose(){try{this.conn.close();}catch(Exceptione){}}};下面是servlet控制处理用户发送来的请求packagecom.lgy.servlet;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.RequestDispatcher;importcom.lgy.factory.DAOFactory;importcom.lgy.vo.login;publicclassloginServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{this.doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Listerrors=newArrayList();Stringpath=../index.jsp;Stringname=request.getParameter(username);System.out.println(这是页面上的用户名:+name);Stringpwd=request.getParameter(password);System.out.println(这是页面上的密码:+pwd);loginlg=newlogin();lg.setUsername(name);lg.setPassword(pwd);lg.setErrors(errors);booleanb=lg.invalidate();System.out.println(判断输入格式的真假+b);//下面的判断为如果输入的格式合法,那么我们进行数据库端得验证if(b){if(DAOFactory.getLoginDAOInstance().isLogin(lg)){System.out.println(进行数据库端得判断***********);path=../success.jsp;}else{errors.add(错误的用户名和密码!);}}request.setAttribute(Error,errors);request.setAttribute(Lg,lg);//request.getRequestDispatcher(path)request.getRequestDispatcher(path).forward(requ
本文标题:jsp+jdbc+servlet登陆完整程序和注解
链接地址:https://www.777doc.com/doc-4335333 .html