jsp+servlet+mysql 로그인 등록 실현

7978 단어 HTML
로그인 인터페이스:





 









     
     

$(document).ready(function(){ $("#kong").click(function(){ $("#zhan").val(""); $("#mima").val(""); }); });

등록 인터페이스:





 









     

$(document).ready(function(){ $("#kong").click(function(){ $("#zhan").val(""); $("#mima").val(""); $("#bumen").val(""); }); });

loginservlet:
package com.smxy.zjq.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.smxy.zjq.db.DbPool;

/**
 * Servlet implementation class LoginServlet
 */
//@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	static Connection conn = DbPool.getConnectionFromPool();
	static PreparedStatement pstmt = null;
	private static ResultSet rs = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(" :"+userName+" :"+password);
		//System.err.println(userName+";"+password);
		int i;
		try {
			i = search(userName,password);
			if(i == 1) {
				request.getRequestDispatcher(
						"/index.jsp").forward(request, response);
			}else if( i == -1){
				request.setAttribute("message", " , ");
				request.getRequestDispatcher("/Login.jsp").forward(request, response);
			}else if(i == 0){
				request.setAttribute("message", " !");
				request.getRequestDispatcher("/Login.jsp").forward(request, response);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public int search(String name,String pass) throws SQLException// , 
	{
		String sql="select * from user where username = '"+name+"'";
		pstmt= (PreparedStatement)conn.prepareStatement(sql);
		rs=pstmt.executeQuery();
		if(rs==null)
		{
			return 0;
		}
		if(rs.next()) {
			String pas=rs.getString("password");
			if(pas.equals(pass)) {
				return 1; 
			}
			else 
			{
				return -1;
			}
				
		}
		return 0;
	}

}

registerservlet:
package com.smxy.zjq.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.smxy.zjq.Class.User;
import com.smxy.zjq.db.DbPool;

/**
 * Servlet implementation class RegisterServlet
 */
//@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Connection conn = DbPool.getConnectionFromPool();
	static PreparedStatement pstmt = null;
	private static ResultSet rs = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(" :"+userName+" :"+password);
		
		User user = new User(userName,password);
		
		if(savedata(user)){
			request.getRequestDispatcher(
					"/index.jsp").forward(request, response);
		}else{
			request.setAttribute("message", " !");
			request.getRequestDispatcher("/Register.jsp").forward(request, response);
		}
	}
	
	public static boolean  savedata(User user) {

		String insertsql = "insert into user(username,password) values(?,?)";//  
		try {
			int i = 1;
			pstmt = conn.prepareStatement(insertsql);
			pstmt.setString(i++, user.getUsername());
			pstmt.setString(i++, user.getPassword());
			int val = pstmt.executeUpdate();
			if (val > 0) {
				return true;
			} else {
				return false;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

}

dbpool:
package com.smxy.zjq.db;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;



public class DbPool {

static DataSource dataSource;
	
	static {
		// , c3p0 : 、 
		dataSource=new ComboPooledDataSource();
	}
	
	public static Connection getConnectionFromPool(){
		try {
			Connection  connection=dataSource.getConnection();
		    return connection;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}

좋은 웹페이지 즐겨찾기