欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

JSP_实现Servlet登陆,注册,链接Mysql数据库。_MySQL

程序员文章站 2022-04-13 20:10:58
...
本文http://blog.csdn.net/svitter

也是一个MVC的实例

源码:http://download.csdn.net/detail/svitter/7532621


user.sql:

-- phpMyAdmin SQL Dump-- version phpStudy 2014-- http://www.phpmyadmin.net---- 主机: localhost-- 生成日期: 2014 年 06 月 21 日 17:42-- 服务器版本: 5.5.38-- PHP 版本: 5.3.28SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";SET time_zone = "+00:00";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;---- 数据库: `users`---- ------------------------------------------------------------ 表的结构 `ur`--CREATE TABLE IF NOT EXISTS `ur` (  `name` varchar(255) NOT NULL,  `passwd` varchar(255) NOT NULL,  `id` bigint(20) NOT NULL AUTO_INCREMENT,  PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=gbk AUTO_INCREMENT=2 ;---- 转存表中的数据 `ur`--INSERT INTO `ur` (`name`, `passwd`, `id`) VALUES('a', 'a', 1);/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

web.xml:

JSP_servlet_javabean配置用於用戶登錄的ServletLoginServletLoginServletLoginServletLoginServlet/LoginServlet配置用於用戶注册的ServletRegisterServletRegisterServletRegisterServletRegisterServlet/RegisterServletlogin.html

lib:

mysql-connector-java.jar;

mysql-connector-java-5.1.17.jar;


login.html:

Login.html
Login:
Password:
register

loginCheck.jsp:

Insert title here

loginsuccess.jsp:

系统登录成功页面	欢迎用户:		

register.html:

register.html
Login:
Password:

LoginServlet.java:

import java.io.*;import javax.servlet.*;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import bean.ConnectionBean;import bean.userBean;/** * @author Svitter * */@SuppressWarnings("serial")public class  LoginServlet extends HttpServlet {	userBean user =  new userBean();	ConnectionBean connBean = new ConnectionBean();	public void doPost(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {		String checkLogin;		String username = request.getParameter("login");		String password = request.getParameter("password");		checkLogin = connBean.checkUser(username, password);		if(checkLogin.equals("Success")) {			user.setUserName(username);			request.setAttribute("user", user);			getServletConfig().getServletContext().getRequestDispatcher("/loginsuccess.jsp").forward(request, response);		} else {			request.setAttribute("checkLogin", checkLogin);			getServletConfig().getServletContext().getRequestDispatcher("/loginCheck.jsp").forward(request, response);		}	}	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException{		doPost(request, response);	}}

RegisterServlet.java:

import java.io.*;import javax.servlet.*;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import bean.ConnectionBean;import bean.userBean;/** * @author Svitter * */@SuppressWarnings("serial")public class  RegisterServlet extends HttpServlet {	userBean user =  new userBean();	ConnectionBean connBean = new ConnectionBean();	public void doPost(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {		String username = request.getParameter("login");		String password = request.getParameter("password");		connBean.addUser(username, password);		getServletConfig().getServletContext().getRequestDispatcher("/login.html").forward(request, response);	}	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException{		doPost(request, response);		}}

太多了不贴了。。详见我的资源。。