JSP+Servlet+JavaBean简单程序例子——用户名密码登陆,摘自《Tomcat&JavaWeb 技术手册》,亲测可用。
系统:Fedora19
IDE环境:MyEclipse10
1、建立Web Project,命名为Login_test。创建userLogn表:
create table userLogin(
user_name varchar(10) not null,
user_pwd varchar(10) not null,
constraint user_pk primary key (user_name)
);
2、添加 JavaBean文件,创建新类UserLoginBean,java
packagelogin;import java.sql.*;public classUserLoginBean {private Connection con; //数据库连接
private ResultSet rs; //结果集
privateStatement stmt;private static final String DRIVER_NAME="com.mysql.jdbc.Driver";private static final String URL_STR="jdbc:mysql://localhost/test";//构造函数
publicUserLoginBean(){try{//加载驱动程序
Class.forName(DRIVER_NAME);//获取数据库连接
con=DriverManager.getConnection(URL_STR, "root", "jiangshan"); //自己的用户名和密码
}catch(SQLException se){
se.printStackTrace();
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
}//业务方法
public booleanlogin(String userName,String password)
{boolean b = false;try{
stmt=con.createStatement();
String sql="SELECT user_pwd FROM userLogin Where user_name='"+userName+"'";
rs=stmt.executeQuery(sql);while(rs.next()){
String pwd= rs.getString(1);if(pwd.equals(password)){
b=true;
}
}
}catch(SQLException se){
se.printStackTrace();
b=false;
}finally{this.close(con,rs,stmt); //下面定义close方法
}returnb;
}//close方法用于关闭相应资源
public voidclose(Connection con,ResultSet rs,Statement stmt){try{if(con!=null){
con.close();
}if(rs!=null){
rs.close();
}if(stmt!=null){
stmt.close();
}
}catch(SQLException se){
se.printStackTrace();
}
}
}
UserLoginBean.java
3、添加Servlet类,命名UserLoginServlet.java
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;importlogin.UserLoginBean;public class UserLoginServlet extendsHttpServlet{public voiddoGet(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{this.doPost(request, response);
}public voiddoPost(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{
PrintWriter out=response.getWriter();try{
String userName=request.getParameter("userName").trim();
String passWord=request.getParameter("passWord").trim();
UserLoginBean ubl=newUserLoginBean();boolean b =ubl.login(userName, passWord);if(b){this.forward(request,response,"/loginOK.jsp");
}else{this.forward(request,response,"/LoginFail.jsp");
}
}catch(Exception e){
out.println("Login fail.
");
out.println(e.toString());
}
}// private voidforward(HttpServletRequest request,HttpServletResponse response,String url)throwsServletException,IOException{
RequestDispatcher rd=request.getRequestDispatcher(url);
rd.forward(request, response);
}
}
UserLoginServlet.java
4、写登陆界面:创建index.jsp
User_LoginJsp+Servlet+JavaBean应用
用户名:
| |
密 码
| |
index.jsp
5、登陆成功界面:LoginOK.jsp
User_LoginOKJSP+Servlet+JavaBean
Login OK!
LoginOK.jsp
6、登陆失败界面:LoginFail.jsp
User_LoginOKJSP+Servlet+JavaBean
Login Fail!
LoginFail.jsp
7、启动tomcat,将此项目部署至tomcat的webapps文件下,打开webapps下的项目里的WEB-INF文件夹,修改web.xml文件,在和之间添加如下代码:
UserLoginServlet
UserLoginServlet
UserLoginServlet
/UserLoginServlet
也可以添加如下代码,将主页设置为index.jsp:
index.jsp
8、打开浏览器,输入http://localhost:8080/Login_test/ 即可进入登陆界面。