java登录时jsp中的用户名_JSP+Servlet+JavaBean简单程序——用户名密码登陆程序

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

User_Login

Jsp+Servlet+JavaBean应用


用户名:
密  码

index.jsp

5、登陆成功界面:LoginOK.jsp

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

User_LoginOK

JSP+Servlet+JavaBean


Login OK!

LoginOK.jsp

6、登陆失败界面:LoginFail.jsp

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

User_LoginOK

JSP+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/   即可进入登陆界面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值