package com.chao.controller;
import com.chao.entity.User;
import com.chao.utils.DBUtil;
import com.chao.utils.StringUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping({"login"})public classLoginController {
@RequestMappingpublicString toLoginIndex(Model model) {return "login";
}
@RequestMapping({"loginyy"})public voidtoLogin(HttpServletRequest request, HttpServletResponse response) {
User user= newUser();
String username= request.getParameter("username");
String password= request.getParameter("password");
user.setUsername(username);
user.setPassword(password);
System.out.println("----------------" + username + "--" + password + "---------------");if ((StringUtil.isEmpty(password)) ||(StringUtil.isEmpty(username))) {
HttpSession session=request.getSession();
session.setAttribute("error", "不能为空");try{
response.sendRedirect(request.getContextPath()+ "/login");
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("---------kong----------");return;
}
DBUtil db= newDBUtil();
String sql= "select username from users where username='" + user.getUsername() + "'";
String sql1= "select password from users where password='" + user.getPassword() + "'";try{
Statement stmt=db.getCon().createStatement();
Statement stmt1=db.getCon().createStatement();
ResultSet rs=stmt.executeQuery(sql);
ResultSet rs1=stmt1.executeQuery(sql1);if ((rs.next()) &&(rs1.next())) {
HttpSession session=request.getSession();
session.setAttribute("currentUser", "登陆成功");
response.sendRedirect("/file");
}else{
System.out.println("-------用户名或密码错误-----");
HttpSession session=request.getSession();
session.setAttribute("error", "错误");
session.setAttribute("username", username);
session.setAttribute("password", password);
response.setCharacterEncoding("GB2312");
PrintWriterout =response.getWriter();out.print("");
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}