JSP+Servlet+JavaBean简单程序例子——用户名密码登陆,摘自《Tomcat&JavaWeb 技术手册》,亲测可用。
系统:Fedora19
IDE环境:MyEclipse10
1、建立Web Project,命名为Login_test。
2、添加 JavaBean文件,创建新类UserLoginBean,java
package login; import java.sql.*; public class UserLoginBean { private Connection con; //数据库连接 private ResultSet rs; //结果集 private Statement stmt; private static final String DRIVER_NAME="com.mysql.jdbc.Driver"; private static final String URL_STR="jdbc:mysql://localhost/test"; //构造函数 public UserLoginBean(){ try{//加载驱动程序 Class.forName(DRIVER_NAME); //获取数据库连接 con=DriverManager.getConnection(URL_STR, "root", "jiangshan"); //自己的用户名和密码 } catch(SQLException se){ se.printStackTrace(); } catch(ClassNotFoundException cnfe){ cnfe.printStackTrace(); } } //业务方法 public boolean login(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方法 } return b; } //close方法用于关闭相应资源 public void close(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(); } } }
3、添加Servlet类,命名UserLoginServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import login.UserLoginBean; public class UserLoginServlet extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ this.doPost(request, response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ PrintWriter out=response.getWriter(); try{ String userName=request.getParameter("userName").trim(); String passWord=request.getParameter("passWord").trim(); UserLoginBean ubl=new UserLoginBean(); 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.<br>"); out.println(e.toString()); } } // private void forward(HttpServletRequest request,HttpServletResponse response,String url) throws ServletException,IOException{ RequestDispatcher rd=request.getRequestDispatcher(url); rd.forward(request, response); } }
4、写登陆界面:创建index.jsp
<%@ page contentType="text/html; charset=GBK" %> <html> <head> <title>User_Login</title> </head> <body><br><br> <font size="5" color="#FF0000"> <b>Jsp+Servlet+JavaBean应用</b> </font><br><hr></hr><br><br> <form name="loginForm" method="post" action="UserLoginServlet"> <table border="0" align="center" cellpadding="2" cellspacing="2" bgcolor="snow"> <tr> <td> <div align ="center">用户名:</div> </td> <td><div align="center"> <input type="text" name="userName" size=20 maxlength="10"> </div></td></tr> <tr> <td> <div align="center">密 码</div> </td> <td> <div align="center"> <input type="password" name="passWord" size="20" maxlength="10"> </div></td></tr> <tr width="100%"> <td> <div align="center"> <input type="submit" value="登陆"> <input type="reset" value="重置"> </div> </td></tr> </table></form> </body> </html>
5、登陆成功界面:LoginOK.jsp
<%@ page contentType="text/html; charset=GBK" %> <html> <head> <title>User_LoginOK</title> </head> <body><br><br><br> <font size="5" color="#FF0000"> <b>JSP+Servlet+JavaBean</b> </font><br><br><hr></hr><br><br> <center> <font color="red" size="4" >Login OK!</font> </center></body> </html>
6、登陆失败界面:LoginFail.jsp
<%@ page contentType="text/html; charset=GBK" %> <html> <head> <title>User_LoginOK</title> </head> <body><br><br><br> <font size="5" color="#FF0000"> <b>JSP+Servlet+JavaBean</b> </font><br><br><hr></hr><br><br> <center> <font color="red" size="4" >Login Fail!</font> </center></body> </html>
7、启动tomcat,将此项目部署至tomcat的webapps文件下,打开webapps下的项目里的WEB-INF文件夹,修改web.xml文件,在<web-app>和</web-app>之间添加如下代码:
<servlet>
<servlet-name>UserLoginServlet</servlet-name>
<servlet-class>UserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserLoginServlet</servlet-name>
<url-pattern>/UserLoginServlet</url-pattern>
</servlet-mapping>
也可以添加如下代码,将主页设置为index.jsp:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
8、打开浏览器,输入http://localhost:8080/Login_test/ 即可进入登陆界面。