servlet+html+mysql实现登录注册功能

这是结构图jdbc连接数据库package jdbc;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import com.mysql.jdbc.Connection;public class jdbc {public static Connecti...

这是结构图
jdbc连接数据库

package jdbc;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Connection;

public class jdbc {
	public static Connection getConnection() throws SQLException,
	ClassNotFoundException {
	Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost:3306/test";
    String username="root";
    String password="123456";
    Connection con=(Connection) DriverManager.getConnection(url,username,password);
	return con;
	}
}

user存储用户

package user;

public class user {
    private String Name;
    private String Password;
	
	
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getPassword() {
		return Password;
	}
	public void setPassword(String password) {
		Password = password;
	}
}

userdao是登录注册的操作

package userdao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import jdbc.jdbc;
import user.user;


public class userdao{
    public boolean select(user u) throws ClassNotFoundException, SQLException{
    	int x=0;
    	jdbc j=new jdbc();
        Connection con=j.getConnection();
        Statement stmt=(Statement) con.createStatement();
		String sql="select Name,Password from t1";
    	ResultSet rs=stmt.executeQuery(sql);
    	
    	
    	while(rs.next()){
    		System.out.println(u.getName()+rs.getString(1));
    		if(u.getName().equals(rs.getString(1))){
    			System.out.println("能比较用户名了");
    			if(u.getPassword().equals(rs.getString(2))){
    				System.out.println("匹配成功了");
    				x=1;
    				break;
    			}
    	   }
    		
    	}
    	if(x==1)
    		return true;
    	else
    		return false;
		
	   
   }
   
	public boolean insert(user u) throws ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		jdbc j=new jdbc();
        Connection con=j.getConnection();
        Statement stmt=(Statement) con.createStatement();
		System.out.println("得到连接");
        
    	String sql="insert into t1 values('"+u.getName()+"','"+u.getPassword()+"')";
    	   int s=stmt.executeUpdate(sql);
    	if(s>0)
    		return true;
    	return false;
	}
	
	

	
}

RegistServlet 是注册功能的servlet

  import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import user.user;
    import userdao.userdao;
    
    /**
     * Servlet implementation class RegistServlet
     */
    public class RegistServlet extends HttpServlet {
    	
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		String username=request.getParameter("name");
    		String password=request.getParameter("password");
    		user u=new user();
    		u.setName(username);
    		u.setPassword(password);
    		userdao us=new userdao();
    		boolean t = false;
    	    try {
    			t=us.insert(u);
    		} catch (ClassNotFoundException | SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		if(t==true){
    			response.sendRedirect("/Servlet2/form.html");
    		}else{
    			response.sendRedirect("/Servlet2/regist.html");
    		}
    		
    		
    }
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doGet(request, response);
    	}
    
    }

LoginServlet是登录功能的servlet

   import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import user.user;
    import userdao.userdao;
    
    /**
     * Servlet implementation class LoginServlet
     */
    public class LoginServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	   user u=new user();
    		String username=request.getParameter("username");
    		String password=request.getParameter("password");
    		u.setName(username);
    		u.setPassword(password);
    		
    		userdao us=new userdao();
    	      
    	       boolean x=false;
    	       try {
    			x=us.select(u);
    		} catch (ClassNotFoundException | SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		
    		if(x==true){
    			response.sendRedirect("/Servlet2/welcome.html");
    		}else{
    			response.sendRedirect("/Servlet2/form.html");
    		}
    		
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doGet(request, response);
    	}
    
    }

toregist是跳转页面的servlet

 import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class toregist
     */
    public class toregist extends HttpServlet {
    	
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/html;charset=utf-8");
    		response.sendRedirect("/Servlet2/regist.html");
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doGet(request, response);
    	}
    
    }

mysql数据库结构如图

本文标题为:servlet+html+mysql实现登录注册功能

基础教程推荐