MVC

SERLVET 

package  servlets;

import  java.io.IOException;
import  java.io.PrintWriter;

import  javabean.Blog;

import  javax.servlet.RequestDispatcher;
import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;

public   class  Register  extends  HttpServlet  {

    
/**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * 
@param request the request send by the client to the server
     * 
@param response the response send by the server to the client
     * 
@throws ServletException if an error occurred
     * 
@throws IOException if an error occurred
     
*/

    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        response.setContentType(
"text/html");
        PrintWriter out 
= response.getWriter();
        
        String username
=request.getParameter("name");
        String userpassword
=request.getParameter("password");
        System.out.println(username);
        System.out.println(username);
        out.println(
"username");
        out.println(
"userpassword");
        out.println(
this.getClass());
        Blog blog
=Blog.checkEnable(username,userpassword);
        
if(blog!=null){
            HttpSession session
=request.getSession();
            session.setAttribute(
"user",blog);
            RequestDispatcher dispatcher
=null;
            dispatcher
=request.getRequestDispatcher("../index.jsp");
            dispatcher.forward(request,response);
        }

        
else{
            request.setAttribute(
"msg","user is enable");
            RequestDispatcher dispatcher
=null;
            dispatcher
=request.getRequestDispatcher("../error.jsp");
            dispatcher.forward(request,response);
        }

    }


}

DB

package  test;
/*
 * 载入所用到的PACKAGE包
 
*/

import  java.sql. * ;
import  javax.naming.Context;
import  javax.naming.InitialContext;


public   class  ConnectionManager {
    
public static void  main(String agrs[]){
        }

    
public static Connection getConn(){
         Connection conn
= null;
         String jndiName 
= "jdbc/fakir08";
         
try{
             Context initCtx 
= new InitialContext();
             
//创建一个命名对象
             

             Context Ctx 
= (Context)initCtx.lookup("java:comp/env");
             
//获取连接池对象
             System.out.print("initCtx="+initCtx);


             Object obj 
= (Object)Ctx.lookup(jndiName);
             
//类型转换
             System.out.print("obj="+obj);

             javax.sql.DataSource ds 
=(javax.sql.DataSource)obj;
             conn 
= ds.getConnection();
             
//获得连接池对象            
             System.out.print("conn="+conn);
         }

         
catch(SecurityException se){
             
throw se;
         }

         
catch(Exception e){
             e.printStackTrace();
         }

         
return conn;
        
    }

    
/*
     * 执行一条SQL语句
     
*/

    
public void ExcuteSql(String sqlString){
        Connection conn 
=null;
        PreparedStatement pstmt 
= null;
        
try{
            conn
= getConn();//获取数据库连接池实例
            pstmt = conn.prepareStatement(sqlString);//将要执行的SQL语句加入
            pstmt.executeUpdate();//执行已经加入的SQL语句
            
        }

        
catch(SQLException sqle){
            sqle.printStackTrace();
        }

        
finally{
            
try{
                pstmt.close();
            }
catch(Exception e){
                e.printStackTrace();
            }

            
try{
                conn.close();
            }

            
catch(Exception e){
                e.printStackTrace();
            }

        }
        
    }
    
}

JAVABEAN

 

package  javabean;


import  java.sql.Connection;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;

import  test.ConnectionManager;

// import firm.firm;

public   class  Blog
{
    
private String username;
    
private String password;
    
/*GET AND SET METHOD*/
    
public void setUsername(String username){
        
this.username = username;
    }

    
public String getUsername(){
        
return username;
    }
    
    
public void setPassword(String password){
        
this.password = password;
    }

    
public String getPassword(){
        
return password;
    }

    
    
    
/**载入已经存在的BLOG代码
    public static Blog load(String username)
    {
        
        Blog blog =new Blog();
        
        blog.setUsername("111");
        blog.setPassword("111");
        return blog;
    }
*/

    
    
    
public static Blog load(String user)
    
{
        String username;
        String password;
        String sqlString;
        Connection con 
= null;
        PreparedStatement ps
= null;
        ResultSet rs 
= null;
        Blog blog 
=new Blog();
        sqlString 
="select * from logon where username='"+user+"'";
        
try{
            con
=ConnectionManager.getConn();
            
            
//获取数据库连接实例
            ps = con.prepareStatement(sqlString);
            
        
            rs 
= ps.executeQuery();
            
//检索数据库
            while(rs.next()){
                
                blog.setUsername( rs.getString(
"username"));
                blog.setPassword( rs.getString(
"password"));
            
                
//将数据库中的数据赋值到实例中
            
            }

        }

        
catch(Exception e){
            e.printStackTrace();
        }

        
finally{
            
try{
                rs.close();
            }

            
catch(Exception e){
                e.printStackTrace();
            }

            
try{
                ps.close();
                }

            
catch(Exception e){
                e.printStackTrace();
            }

            
try{
                con.close();
                }

            
catch(Exception e){
                e.printStackTrace();
            }

        }

        
return blog;
    }

    
    
public static Blog checkEnable(String user,String pwd)
    
{
        Blog blog 
=null;
        blog 
= load(user);
        
// 根据名称载相应的BLOG
        if(blog != null){
            
/*如果载入的BLOG不为空,则比较密码是否符合**/
            
if(!pwd.equals(blog.getPassword())){
                blog 
= null;
            }

        }

        
//如果经过判断,用户合法则返回用户的实例,否则返回一个NULL;
        return blog;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值