Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408828
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Java

2015-03-07 16:27:16


点击(此处)折叠或打开

  1. package cn.com.xiebiao.tomcat;

  2. import java.io.IOException;
  3. import java.io.PrintWriter;

  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;

  9. /**
  10.  *
  11.  * Title : LoginServlet.java
  12.  * Company: ZhenBot
  13.  * Author : Vibe Xie @
  14.  * Time : Mar 7, 2015 4:25:46 PM
  15.  * Copyright: Copyright (c) 2015
  16.  *
  17.  */

  18. @WebServlet("/loginServlet")
  19. public class LoginServlet extends HttpServlet {
  20.     private static final long serialVersionUID = 1L;
  21.     private static Login service;
  22.     public static int seviceTimes=0;
  23.     @Override
  24.     public void init() throws ServletException {
  25.         // TODO Auto-generated method stub
  26.         super.init();
  27.         service=new Login();
  28.     }

  29.     /**
  30.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  31.      */
  32.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  33.         // TODO Auto-generated method stub
  34.         doPost(request, response);
  35.     }

  36.     /**
  37.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  38.      */
  39.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  40.         // TODO Auto-generated method stub
  41.         request.setCharacterEncoding("utf-8");
  42.         response.setContentType("text/html;charset=utf-8");
  43.         response.setCharacterEncoding("utf-8");
  44.         
  45.         System.out.println("servlet第"+(seviceTimes++)+"次服务");
  46.         
  47.         PrintWriter writer=response.getWriter();
  48.         
  49.         String clientName=request.getParameter("name");
  50.         String clientPassword=request.getParameter("password");
  51.         
  52.         System.out.println("请求的name:"+clientName);
  53.         System.out.println("请求的password:"+clientPassword);
  54.         
  55.         boolean flag=service.isUserLogin(clientName, clientPassword);
  56.         
  57.         if(flag==true){
  58.             writer.println("

    登陆成功

    "
    );
  59.         }else {
  60.             writer.println("

    登陆失败

    "
    );
  61.         }
  62.         
  63.         writer.flush();
  64.         writer.close();
  65.     }

  66. }


点击(此处)折叠或打开

  1. package cn.com.xiebiao.tomcat;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.PreparedStatement;

  7. /**
  8.  *
  9.  * Title : Login.java
  10.  * Company: ZhenBot
  11.  * Author : Vibe Xie @
  12.  * Time : Mar 7, 2015 4:25:02 PM
  13.  * Copyright: Copyright (c) 2015
  14.  *
  15.  */
  16. public class Login {
  17.     public static String url="jdbc:mysql://localhost:3306/xiebiaoDB";
  18.     public static String user="xiebiao";
  19.     public static String password="*********";
  20.     
  21.     public static Connection connection=null;
  22.     public static PreparedStatement preparedStatement=null;
  23.     public static ResultSet resultSet=null;
  24.     public static boolean isConnectServerTrue;
  25.     public static boolean isLoginTrue;
  26.     
  27.     public static boolean connectServer(){
  28.         try {
  29.             Class.forName("com.mysql.jdbc.Driver");
  30.             connection=DriverManager.getConnection(url, user, password);
  31.             if(connection!=null){
  32.                 System.out.println("数据库:"+url+" 连接成功");
  33.                 return true;
  34.             }else {
  35.                 System.out.println("数据库:"+url+" 连接失败");
  36.                 return false;
  37.             }
  38.             
  39.         } catch (Exception e) {
  40.             // TODO Auto-generated catch block
  41.             System.out.println("jdbc错误");
  42.             e.printStackTrace();
  43.         }
  44.         return false;
  45.     }
  46.     
  47.     public boolean isUserLogin(String clientName,String clientPassword){
  48.         // TODO Auto-generated method stub
  49.         String sql="select password from ids where name=?;";
  50.         
  51.         if(connectServer()==false){
  52.             return false;
  53.         }
  54.         
  55.         try {
  56.             preparedStatement=connection.prepareStatement(sql);
  57.             preparedStatement.setString(1,clientName);
  58.             resultSet=preparedStatement.executeQuery();
  59.             String serverPassword=null;
  60.             
  61.             if(resultSet.next()==true){
  62.                 serverPassword=resultSet.getString("password");
  63.                 while(resultSet.next()){
  64.                     serverPassword=resultSet.getString("password");
  65.                 }
  66.             }else {
  67.                 System.out.println("用户:"+clientName+" 不存在");
  68.                 return false;
  69.             }
  70.             
  71.             System.out.println("数据库中查询的password:"+serverPassword);
  72.             System.out.println("验证中...");
  73.             
  74.             if(serverPassword.equals(clientPassword)){
  75.                 System.out.println(clientName+" 登陆成功");
  76.                 isLoginTrue=true;
  77.             }else {
  78.                 System.out.println(clientName+" 登陆失败");
  79.                 isLoginTrue=false;
  80.             }
  81.             
  82.         } catch (SQLException e) {
  83.             // TODO Auto-generated catch block
  84.             e.printStackTrace();
  85.         }finally{
  86.             try {
  87.                 if(resultSet!=null){
  88.                     resultSet.close();
  89.                     resultSet=null;
  90.                 }
  91.                 
  92.                 if(preparedStatement!=null){
  93.                     preparedStatement.close();
  94.                     preparedStatement=null;
  95.                 }
  96.                 
  97.                 if(connection!=null){
  98.                     connection.close();
  99.                     connection=null;
  100.                 }
  101.             } catch (SQLException e) {
  102.                 // TODO Auto-generated catch block
  103.                 e.printStackTrace();
  104.             }
  105.         }
  106.         return isLoginTrue;
  107.     }
  108. }

阅读(1110) | 评论(0) | 转发(0) |
0

上一篇:eclipse快捷键

下一篇:HttpURLConnection使用

给主人留下些什么吧!~~