집 >데이터 베이스 >MySQL 튜토리얼 >JSP_实现Servlet登陆,注册,链接Mysql数据库。_MySQL
本文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 */;
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>JSP_servlet_javabean</display-name> <servlet> <description>配置用於用戶登錄的Servlet</description> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <servlet> <description>配置用於用戶注册的Servlet</description> <display-name>RegisterServlet</display-name> <servlet-name>RegisterServlet</servlet-name> <servlet-class>RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/RegisterServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list></web-app>
lib:
mysql-connector-java.jar;
mysql-connector-java-5.1.17.jar;
login.html:
<title>Login.html</title><meta charset="UTF-8"><link rel="stylesheet" href="css/reset.css"><link rel="stylesheet" href="css/style.css" media="screen" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title> <script type="text/javascript"> alert("<%=checkLogin%>!"); window.location.href="login.html"; </script>
<title>系统登录成功页面</title> <script type="text/javascript"> alert("登录成功!") </script> 欢迎用户: <div id="box" style="width: 252px; font: 25px/25px 宋体; background: #000; color: #9f9; border: #999 20px ridge; text-shadow: 2px 3px 1px #0f0;"></div> <script> var domain = "www.zuidaima.com"; var author = "zuidaima"; var map = eval("[" + Array(23).join("0x801,") + "0xfff]"); var tatris = [ [ 0x6600 ], [ 0x2222, 0xf00 ], [ 0xc600, 0x2640 ], [ 0x6c00, 0x4620 ], [ 0x4460, 0x2e0, 0x6220, 0x740 ], [ 0x2260, 0xe20, 0x6440, 0x4700 ], [ 0x2620, 0x720, 0x2320, 0x2700 ] ]; var keycom = { "38" : "rotate(1)", "40" : "down()", "37" : "move(2,1)", "39" : "move(0.5,-1)" }; var dia, pos, bak, run; function start() { dia = tatris[~~(Math.random() * 7)]; bak = pos = { fk : [], y : 0, x : 4, s : ~~(Math.random() * 4) }; rotate(0); } function over() { document.onkeydown = null; clearInterval(run); alert("GAME OVER"); } function update(t) { bak = { fk : pos.fk.slice(0), y : pos.y, x : pos.x, s : pos.s }; if (t) return; for (var i = 0, a2 = ""; i < 22; i++) a2 += map[i].toString(2).slice(1, -1) + "<br/>"; for (var i = 0, n; i < 4; i++) if (/([^0]+)/.test(bak.fk[i].toString(2) .replace(/1/g, "/u25a1"))) a2 = a2.substr(0, n = (bak.y + i + 1) * 15 - RegExp.$_.length - 4) + RegExp.$1 + a2.slice(n + RegExp.$1.length); document.getElementById("box").innerHTML = a2.replace(/1/g, "/u25a0").replace(/0/g, "/u3000"); } function is() { for (var i = 0; i < 4; i++) if ((pos.fk[i] & map[pos.y + i]) != 0) return pos = bak; } function rotate(r) { var f = dia[pos.s = (pos.s + r) % dia.length]; for (var i = 0; i < 4; i++) pos.fk[i] = (f >> (12 - i * 4) & 15) << pos.x; update(is()); } function down() { ++pos.y; if (is()) { for (var i = 0; i < 4 && pos.y + i < 22; i++) if ((map[pos.y + i] |= pos.fk[i]) == 0xfff) map.splice(pos.y + i, 1), map.unshift(0x801); if (map[1] != 0x801) return over(); start(); } update(); } function move(t, k) { pos.x += k; for (var i = 0; i < 4; i++) pos.fk[i] *= t; update(is()); } document.onkeydown = function(e) { eval(keycom[(e ? e : event).keyCode]); }; start(); run = setInterval("down()", 400); </script>
<title>register.html</title><meta name="keywords" content="keyword1,keyword2,keyword3"><meta name="description" content="this is my page"><meta name="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
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); }}
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); }}