index.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<style>
*{
color:#369;
padding:10 15 10 15;
margin:0px;font-family: "Microsoft YaHei";
font-weight:normal;font-size:18px;
text-align:center;
}
.login{
position:absolute;
}
input{
padding:10px;
background:transparent;
border:1px solid #3BB0DB;
text-align:center;
}
td{
text-align:center;
}
body{
background-image:url("http://image.tianjimedia.com/uploadImages/2013/312/PM71H8JFI7KK.jpg");
background-size:cover;
}
html {
overflow-y: auto;
overflow-x: hidden;
}
</style>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.js"></script>
<form class="login" method="post" action="index.jsp">
<table>
<tr><td><input type="text" name="userName" style="border-right:0px"><input type="submit" value="登录"></td></tr>
<tr><td><input type="password" name="passWord" style="border-right:0px"><input type="button" value="注册"></td></tr>
</table>
</form>
<script>
$(document).ready(
function(){
$(":button").click(function(){
window.location.href="register.jsp";
});
}
);
center(".login");
function center(element){
var padding = parseInt($(element).css("padding"))*2;//parseInt()可以把a123转换成123
var border = parseInt($(element).css("border"))*2;
if(!border){border=0}//NaN无数值的话 赋值为0
if(!padding){padding=0}
var left = ($(element).parent().width()-$(element).width()-padding-border)/2;
var top = ($(element).parent().height()-$(element).height()-padding-border)/2;
$(element).css({"left":left,"top":top});
}
$(window).resize(function() {
center(".login");
});
</script>
<%
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
if(userName!=null && passWord!=null){
userName = new String(userName.getBytes("ISO-8859-1"),"utf-8");
passWord = new String(passWord.getBytes("ISO-8859-1"),"utf-8");
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8","root","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from userinfo where userName='"+userName+"' and passWord='"+passWord+"'");
if(rs.next())
{
response.sendRedirect("/Message.jsp?userName="+userName+"&passWord="+passWord);
}
else{
out.print("<script>alert('密码或用户名错误');</script>");
}
rs.close();
stmt.close();
conn.close();
}
%>
Message.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<style>
*{
width:100%;
font-size:32px;
margin:0px;
padding:0px
}
textarea,input,button{
background:rgba(0, 0, 0, 0.2) none repeat scroll 0 0 !important;
margin:5px;
border:0px;
border-radius:5px;
}
body{
background-image:url("http://image.tianjimedia.com/uploadImages/2013/312/PM71H8JFI7KK.jpg");
background-size:cover;
}
html {
overflow-y: auto;
overflow-x: hidden;
}
</style>
<script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"></script>
<textarea style="height:600px;" id="oTextarea">
<%
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
String message = request.getParameter("message");
String sql = "select * from message";
if(userName!=null && passWord!=null){
if(message!=null){
message = new String(message.getBytes("ISO-8859-1"),"utf-8");
}
userName = new String(userName.getBytes("ISO-8859-1"),"utf-8");
passWord = new String(passWord.getBytes("ISO-8859-1"),"utf-8");
String insert = "insert into message (name,message) value ('"+userName+"','"+message+"')";
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8","root","");
Statement stmt=conn.createStatement();
String logic = "select * from userinfo where userName = '"+userName+"' and passWord='"+passWord+"'" ;
ResultSet rs=stmt.executeQuery(logic);
if(message!=null && rs.next()){
stmt.executeUpdate(insert);
}
rs=stmt.executeQuery(sql);
while(rs.next()){
out.print(rs.getString(2)+" 说:"+rs.getString(3)+"\n在:"+rs.getString(4)+"\n");
}
rs.close();
stmt.close();
conn.close();
}else{
response.sendRedirect("/index.jsp");
}
%>
</textarea>
<input type="text" style="height:60px;">
<button class="Message">发送</button>
<button class="Return">返回</button>
<script>
oTextarea.scrollTop=oTextarea.scrollHeight;
$(document).ready(function(){
$(".Message").click(function(){
var message = $("input").val();
if(message){
location.href = "/Message.jsp?userName=<%=userName%>&passWord=<%=passWord%>&message="+message;
}else{alert("禁止空消息!")}
});
$(".Return").click(function(){
location.href = "index.jsp";
});
})
</script>
register.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<style>
a{
text-decoration:none;
}
*{
width:100%;
font-size:32px;
margin:0px;
padding:0px;
}
body{
background-image:url("http://image.tianjimedia.com/uploadImages/2013/312/PM71H8JFI7KK.jpg");
background-size:cover;
}
textarea,input,button{
background:rgba(0, 0, 0, 0.2) none repeat scroll 0 0 !important;
margin:5px;
border:0px;
border-radius:5px;
}
html {
overflow-y: auto;
overflow-x: hidden;
}
</style>
<form action="register.jsp" method="post">
<input type="text" name="userName" size="10" value="用户名" onfocus="if (value =='用户名'){value =''}"onblur="if (value ==''){value='用户名'}">
<input type="password" name="passWord" size="10" value="密码" onfocus="if (value =='密码'){value =''}"onblur="if (value ==''){value='密码'}">
<input type="submit" name="submit" value="注册">
<button><a href="index.jsp">返回</a></button>
</form>
<%
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
if(userName!=null && passWord!=null){
userName = new String(userName.getBytes("ISO-8859-1"),"utf-8");
passWord = new String(passWord.getBytes("ISO-8859-1"),"utf-8");
String insert = "insert into userinfo (userName,passWord) value ('"+userName+"','"+passWord+"')";
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8","root","");
Statement stmt=conn.createStatement();
stmt.executeUpdate(insert);
stmt.close();
conn.close();
response.sendRedirect("index.jsp");
}else{
out.print("</br>用户名或密码为空");
}
%>
test.sql
/*
Navicat MySQL Data Transfer
Source Server : sunny
Source Server Version : 50712
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50712
File Encoding : 65001
Date: 2016-05-25 14:53:59
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `message`
-- ----------------------------
DROP TABLE IF EXISTS `message`;
CREATE TABLE `message` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` char(10) NOT NULL,
`message` char(10) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of message
-- ----------------------------
INSERT INTO `message` VALUES ('1', '孙宇亮', '大家好', '2016-05-25 14:24:05');
INSERT INTO `message` VALUES ('2', 'sun', '中文', '2016-05-25 14:49:43');
-- ----------------------------
-- Table structure for `userinfo`
-- ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userName` char(10) NOT NULL,
`passWord` char(10) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES ('18', '孙宇亮', 'syl', '2016-05-25 13:42:51');
INSERT INTO `userinfo` VALUES ('19', '用户名', '密码', '2016-05-25 14:02:43');
INSERT INTO `userinfo` VALUES ('20', '用户名', '密码', '2016-05-25 14:02:47');
INSERT INTO `userinfo` VALUES ('21', '用户名', '密码', '2016-05-25 14:07:34');
INSERT INTO `userinfo` VALUES ('22', '用户名', '密码', '2016-05-25 14:17:39');
INSERT INTO `userinfo` VALUES ('23', '用户名', '密码', '2016-05-25 14:28:34');
INSERT INTO `userinfo` VALUES ('24', '用户名', '密码', '2016-05-25 14:28:37');
INSERT INTO `userinfo` VALUES ('25', 'sun', '123', '2016-05-25 14:48:59');
效果图
点评
代码很乱很垃圾,细节做得不到位,有时间再改改,用jq写个共通,做做过滤什么的