点击(此处)折叠或打开
-
package cn.com.xiebiao.tomcat;
-
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.annotation.WebServlet;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
/**
-
*
-
* Title : LoginServlet.java
-
* Company: ZhenBot
-
* Author : Vibe Xie @
-
* Time : Mar 7, 2015 4:25:46 PM
-
* Copyright: Copyright (c) 2015
-
*
-
*/
-
-
@WebServlet("/loginServlet")
-
public class LoginServlet extends HttpServlet {
-
private static final long serialVersionUID = 1L;
-
private static Login service;
-
public static int seviceTimes=0;
-
@Override
-
public void init() throws ServletException {
-
// TODO Auto-generated method stub
-
super.init();
-
service=new Login();
-
}
-
-
/**
-
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-
*/
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
// TODO Auto-generated method stub
-
doPost(request, response);
-
}
-
-
/**
-
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
-
*/
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
// TODO Auto-generated method stub
-
request.setCharacterEncoding("utf-8");
-
response.setContentType("text/html;charset=utf-8");
-
response.setCharacterEncoding("utf-8");
-
-
System.out.println("servlet第"+(seviceTimes++)+"次服务");
-
-
PrintWriter writer=response.getWriter();
-
-
String clientName=request.getParameter("name");
-
String clientPassword=request.getParameter("password");
-
-
System.out.println("请求的name:"+clientName);
-
System.out.println("请求的password:"+clientPassword);
-
-
boolean flag=service.isUserLogin(clientName, clientPassword);
-
-
if(flag==true){
-
writer.println("
登陆成功
");
-
}else {
-
writer.println("
登陆失败
");
-
}
-
-
writer.flush();
-
writer.close();
-
}
-
- }
点击(此处)折叠或打开
-
package cn.com.xiebiao.tomcat;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.PreparedStatement;
-
-
/**
-
*
-
* Title : Login.java
-
* Company: ZhenBot
-
* Author : Vibe Xie @
-
* Time : Mar 7, 2015 4:25:02 PM
-
* Copyright: Copyright (c) 2015
-
*
-
*/
-
public class Login {
-
public static String url="jdbc:mysql://localhost:3306/xiebiaoDB";
-
public static String user="xiebiao";
-
public static String password="*********";
-
-
public static Connection connection=null;
-
public static PreparedStatement preparedStatement=null;
-
public static ResultSet resultSet=null;
-
public static boolean isConnectServerTrue;
-
public static boolean isLoginTrue;
-
-
public static boolean connectServer(){
-
try {
-
Class.forName("com.mysql.jdbc.Driver");
-
connection=DriverManager.getConnection(url, user, password);
-
if(connection!=null){
-
System.out.println("数据库:"+url+" 连接成功");
-
return true;
-
}else {
-
System.out.println("数据库:"+url+" 连接失败");
-
return false;
-
}
-
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
System.out.println("jdbc错误");
-
e.printStackTrace();
-
}
-
return false;
-
}
-
-
public boolean isUserLogin(String clientName,String clientPassword){
-
// TODO Auto-generated method stub
-
String sql="select password from ids where name=?;";
-
-
if(connectServer()==false){
-
return false;
-
}
-
-
try {
-
preparedStatement=connection.prepareStatement(sql);
-
preparedStatement.setString(1,clientName);
-
resultSet=preparedStatement.executeQuery();
-
String serverPassword=null;
-
-
if(resultSet.next()==true){
-
serverPassword=resultSet.getString("password");
-
while(resultSet.next()){
-
serverPassword=resultSet.getString("password");
-
}
-
}else {
-
System.out.println("用户:"+clientName+" 不存在");
-
return false;
-
}
-
-
System.out.println("数据库中查询的password:"+serverPassword);
-
System.out.println("验证中...");
-
-
if(serverPassword.equals(clientPassword)){
-
System.out.println(clientName+" 登陆成功");
-
isLoginTrue=true;
-
}else {
-
System.out.println(clientName+" 登陆失败");
-
isLoginTrue=false;
-
}
-
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}finally{
-
try {
-
if(resultSet!=null){
-
resultSet.close();
-
resultSet=null;
-
}
-
-
if(preparedStatement!=null){
-
preparedStatement.close();
-
preparedStatement=null;
-
}
-
-
if(connection!=null){
-
connection.close();
-
connection=null;
-
}
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
return isLoginTrue;
-
}
- }