package com.cvs.contorller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cvs.dao.UserDao;
import com.cvs.model.User;
public class LoginServlet extends HttpServlet {/**
* Constructor of the object.
*/
public LoginServlet(){super();}/**
* Destruction of the servlet. <br>
*/
public voiddestroy(){
super.destroy();// Just puts "destroy" string in log// Put your code here}/**
* 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 voiddoGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
UserDao dao=new UserDao();
User user =dao.findUserByName();if(username.equals(user.getName())&&username.equals(user.getPassword())){
request.getRequestDispatcher("WebRoot/jsp/header.jsp").forward(request, response);}else{
request.getRequestDispatcher("login.jsp").forward(request, response);}}/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @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 voiddoPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {doGet(request, response);}/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public voidinit() throws ServletException {// Put your code here}}
package com.cvs.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.cvs.model.User;
import com.cvs.util.DBUtil;
public class UserDao {
public User findUserByName(){
User user=new User();
Connection conn= DBUtil.getConnection();
try {
PreparedStatement ps=conn.prepareStatement("select * from user where username=?");
ps.setString(1,"username");
ResultSet rs= ps.executeQuery();while(rs.next()){
String name= rs.getString("username");
String password= rs.getString("password");
user.setName(name);
user.setPassword(password);}
rs.close();
ps.close();} catch (Exception e){// TODO: handle exception}finally{
DBUtil.closeConn();}return null;}}
package com.cvs.model;
public class User {
private int id;
private String name;
private String password;
private String sex;
private String birthday;
private String hometown;
public intgetId(){return id;}
public voidsetId(int id){
this.id = id;}
public String getName(){return name;}
public voidsetName(String name){
this.name = name;}
public String getPassword(){return password;}
public voidsetPassword(String password){
this.password = password;}
public String getSex(){return sex;}
public voidsetSex(String sex){
this.sex = sex;}
public String getBirthday(){return birthday;}
public voidsetBirthday(String birthday){
this.birthday = birthday;}
public String getHometown(){return hometown;}
public voidsetHometown(String hometown){
this.hometown = hometown;}
@Override
public String toString(){return"User [birthday="+ birthday +", hometown="+ hometown
+", id="+ id +", name="+ name +", password="+ password +", sex="+ sex +", getBirthday()="+getBirthday()+", getHometown()="+getHometown()+", getId()="+getId()+", getName()="+getName()+", getPassword()="+getPassword()+", getSex()="+getSex()+", getClass()="+getClass()+", hashCode()="+hashCode()+", toString()="+ super.toString()+"]";}}
package com.cvs.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.cvs.model.User;
public class DBUtil {
public static Connection conn=null;
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/qqzone","root","123456");} catch (ClassNotFoundException e){// TODO Auto-generated catch block
e.printStackTrace();} catch (SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}return conn;}
public staticvoidcloseConn(){if(null!=conn){
try {
conn.close();} catch (Exception e){// TODO: handle exception
e.printStackTrace();}}}}