jsp+jdbc+servlet登陆完整程序和注解文档格式.docx
《jsp+jdbc+servlet登陆完整程序和注解文档格式.docx》由会员分享,可在线阅读,更多相关《jsp+jdbc+servlet登陆完整程序和注解文档格式.docx(14页珍藏版)》请在冰点文库上搜索。
![jsp+jdbc+servlet登陆完整程序和注解文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-4/30/7634c02a-291d-4edc-9b89-325b8c44ca41/7634c02a-291d-4edc-9b89-325b8c44ca411.gif)
Listall=(List)request.getAttribute("
);
Iteratoriter=all.iterator();
while(iter.hasNext())
{
%>
<
li>
%=iter.next()%>
}
}
formaction="
servlet/loginServlet"
method="
Post"
>
<
tablealign="
CENTER"
tr>
tdcolspan="
2"
align="
h3>
fontcolor="
#0000ff"
strong>
/strong>
/font>
/h3>
/td>
/tr>
<
tdalign="
right"
用户名:
td>
inputtype="
text"
name="
username"
/>
密&
nbsp&
nbsp码:
password"
center"
Submit"
submit"
value="
提交"
Reset"
reset"
重置"
/table>
/form>
/body>
/html>
如下web.xml文件
?
xmlversion="
1.0"
encoding="
web-appversion="
2.4"
xmlns="
xmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
servlet>
description>
ThisisthedescriptionofmyJ2EEcomponent<
/description>
display-name>
ThisisthedisplaynameofmyJ2EEcomponent<
/display-name>
servlet-name>
loginServlet<
/servlet-name>
servlet-class>
com.lgy.servlet.loginServlet<
/servlet-class>
/servlet>
servlet-mapping>
url-pattern>
/servlet/loginServlet<
/url-pattern>
/servlet-mapping>
welcome-file-list>
welcome-file>
index.jsp<
/welcome-file>
/welcome-file-list>
/web-app>
注意
Web.xm中的<
和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)){
密码不能为空!
if(this.password.length()<
3||this.password.length()>
10){
您应该输入合法的密码,的长度在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="
//声明一个数据库连接对象
privateConnectionconn=null;
//在构造方法之中连接数据库
publicMysqlConnection()
{
try
//加载驱动程序
Class.forName(DBDRIVER);
//连接数据库
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
catch(Exceptione)
System.out.println(e);
//返回一个数据库连接
publicConnectiongetConnection()
///返回连接对象
returnthis.conn;
//关闭数据库连接
publicvoidclose()
this.conn.close();
};
下面是servlet控制处理用户发送来的请求
packagecom.lgy.servlet;
importjava.io.IOException;
importjava.util.ArrayList;
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)
Listerrors=newArrayList();
Stringpath="
../index.jsp"
Stringname=request.getParameter("
这是页面上的用户名:
+name);
Stringpwd=request.getParameter("
这是页面上的密码:
+pwd);
loginlg=newlogin();
lg.setUsername(name);
lg.setPassword(pwd);
lg.setErrors(errors);
booleanb=lg.invalidate();
判断输入格式的真假"
+b);
//下面的判断为如果输入的格式合法,那么我们进行数据库端得验证
if(b){
if(DAOFactory.getLoginDAOInstance().isLogin(lg)){
System.out.println("
进行数据库端得判断***********"
path="
../success.jsp"
;
}else{
错误的用户名和密码!
request.setAttribute("
errors);
Lg"
lg);
//request.getRequestDispatcher(path)
request.getRequestDispatcher(path).forward(request,response);
下面是很据servlet的处理由具体实现类处理数据
DAO
packagecom.lgy.dao;
publicinterfaceloginDao{
publicbooleanisLogin(loginlg);
DAOFactory
packagecom.lgy.factory;
importcom.lgy.dao.loginDao;
importcom.lgy.daoImp.loginDaoImp;
publicclassDAOFactory{
publicstaticloginDaogetLoginDAOInstance()
returnnewloginDaoImp();
}
DaoImpl
packagecom.lgy.daoImp;
importcom.lgy.dao.*;
importcom.lgy.db.*;
importjava.sql.*;
importjava.sql.PreparedStatement;
importjava.sql.Connection;
importjava.sql.ResultSet;
publicclassloginDaoImpimplementsloginDao{
publicbooleanisLogin(loginlg){
booleanflag=false;
PreparedStatementpstm=null;
ResultSetrs=null;
Stringsql=null;
MysqlConnectionmc=newMysqlConnection();
sql="
selectusername,passwordfromloginwhereusername=?
andpassword=?
try{
System.out.println("
这是要用到的sql………………:
+sql);
pstm=mc.getConnection().prepareStatement(sql);
@@@@@@@@要查询的用户名:
+lg.getUsername());
pstm.setString(1,lg.getUsername());
pstm.setString(2,lg.getPassword());
rs=pstm.executeQuery();
if(rs.next()){
flag=true;
lg.setUsername(rs.getString("
));
}catch(Exceptionex){
ex.printStackTrace();
}
呵呵!
别忘了导入数据库的jar包!
!