LoginDao.java
package com;
import java.sql.SQLException;
import javax.xml.registry.Connection;
import javax.xml.registry.JAXRException;
import user.UserInfo;
import com.mysql.jdbc.PreparedStatement;
public class LoginDao {
private static LoginDao instance=null;
public static LoginDao getInstance(){
if(instance==null)
instance=new LoginDao();
return instance;
}
public boolean saveUser(UserInfo user) throws JAXRException
{
Connection conn=null;
try{
conn=(Connection) MySQLDBCon.getConn();
String sql="insert into user(name,password,Verifypwd,mailbox,tel)values(?,?,?,?,?)";
PreparedStatement pstmt=(PreparedStatement) ((java.sql.Connection) conn).prepareStatement(sql);
pstmt.setString(1,user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getVerifypwd());
pstmt.setString(4,user.getMailbox());
pstmt.setString(5,user.getTel());
pstmt.executeUpdate();
return true;
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
}
return false;
}
}
MyLoginServlet.java:
package com;
import java.sql.SQLException;
import javax.xml.registry.Connection;
import javax.xml.registry.JAXRException;
import user.UserInfo;
import com.mysql.jdbc.PreparedStatement;
public class LoginDao {
private static LoginDao instance=null;
public static LoginDao getInstance(){
if(instance==null)
instance=new LoginDao();
return instance;
}
public boolean saveUser(UserInfo user) throws JAXRException
{
Connection conn=null;
try{
conn=(Connection) MySQLDBCon.getConn();
String sql="insert into user(name,password,Verifypwd,mailbox,tel)values(?,?,?,?,?)";
PreparedStatement pstmt=(PreparedStatement) ((java.sql.Connection) conn).prepareStatement(sql);
pstmt.setString(1,user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getVerifypwd());
pstmt.setString(4,user.getMailbox());
pstmt.setString(5,user.getTel());
pstmt.executeUpdate();
return true;
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
}
return false;
}
}
MyRegisterServlet.java
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import user.UserInfo;
public class MyRegisterServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public MyRegisterServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
String Verifypwd=request.getParameter("Verifypwd");
String mailbox=request.getParameter("mailbox");
String tel=request.getParameter("tel");
UserInfo user=new UserInfo();
user.setName(name);
user.setPassword(password);
user.setVerifypwd(Verifypwd);
user.setMailbox(mailbox);
user.setTel(tel);
request.getRequestDispatcher("register_check.jsp").forward(request, response);
}
}
MySQLDBCon.java
package com;
import com.mysql.jdbc.Connection;
public class MySQLDBCon {
private static Connection conn=null;
public static Connection getConn(){
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/id_passenger";
conn=(Connection) java.sql.DriverManager.getConnection(url,"root","1111");
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
}
SetCharacterEncodingFilter.java
package myFilter;
import java.io.IOException;
import javax.servlet.Filter;//主要就是实现这个借口,不要选错了
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter{
String newCharSet;
public void destroy(){}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
{
request.setCharacterEncoding(newCharSet);
response.setContentType("text/html;charset="+newCharSet);
//
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig)throws ServletException
{
if(filterConfig.getInitParameter("newCharSet")!=null)
{
newCharSet=filterConfig.getInitParameter("newCharSet");
}
else
newCharSet="GB2312";
}
public boolean test(int node) {
// TODO Auto-generated method stub
return false;
}
}
UserInfo.java
package user;
public class UserInfo {
private String name;
private String password;
private String Verifypwd;
private String mailbox;
private String tel;
private boolean valid;
public UserInfo(){}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public String getVerifypwd()
{
return Verifypwd;
}
public void setVerifypwd(String Verifypwd)
{
this.Verifypwd=Verifypwd;
}
public String getMailbox()
{
return mailbox;
}
public void setMailbox(String mailbox)
{
this.mailbox=mailbox;
}
public String getTel()
{
return tel;
}
public void setTel(String tel)
{
this.tel=tel;
}
public boolean isValid(){
char Arr[]=name.toCharArray();
int firstChar=(int)Arr[0];
StringBuffer sb=new StringBuffer("");
if((firstChar>=65&&firstChar<=90)||(firstChar>=97&&firstChar<=122)){
for(int i=1;i<Arr.length;i++){
int ascii=Arr[i];
if((ascii>=48&&ascii<=57)||(ascii>=65&&ascii<=90)||(ascii>=97&&ascii<=122)){
sb.append(Arr[i]);
}
}
int length=Arr.length-sb.toString().length();
if(length==1){
valid=true;
return valid;
}
else{
valid=false;
return valid;
}
}else{
valid=false;
return valid;
}
}
}
login.css
@charset "utf-8";
body{
margin:0 auto;
/*padding是边框内部*/
padding:30px 60px;
width:500px;
border:1px solid gray;
background: url(../images/12306.png) #F8F8F8 no-repeat center width="1200" height="900";
/*圆角*/
-webkit-border-radius: 5px;
}
.up{
color: red;
background: #CDCDCD;
padding: 20px;
-webkit-border-radius: 5px;
}
reserve.css
@charset "utf-8";
body{
margin:0 auto;
/*padding是边框内部*/
padding:30px 60px;
width:500px;
border:1px solid gray;
background: #F8F8F8;
/*圆角*/
-webkit-border-radius: 5px;
}
.up{
color: red;
background: #CDCDCD;
padding: 20px;
-webkit-border-radius: 5px;
}
.submit{
height:40px;
width:50px;
-webkit-border-radius:5px;
}
.reset{
height:40px;
width:50px;
-webkit-border-radius:5px;
}
reserve.css
@charset "utf-8";
/*内外边距都是0*/
body,h1,ul{
margin: 0;
padding: 0;
}
/*将ul前面的点去掉*/
ul{
list-style: outside none none;
}
/*将链接的下划线去掉*/
a{
text-decoration: none;
}
/*#333是黑色*/
#nav{
width: 100%;
height: 70px;
background-color: #333;
}
#nav .center{
width: 1263px;
height: 70px;
margin: 0 auto;
}
/*将文本移走用到text-indent并插入logo*/
#nav .logo{
width: 240px;
height: 70px;
background-image: url(../img/4.png);
text-indent: -9999px;
float:left;
}
/*#eee是灰色 line-height设置垂直居中*/
#nav .link{
width: 650px;
color:#eee;
float: right;
height: 70px;
line-height: 70px;
}
/*水平居中*/
#nav .link li{
float: left;
width:120px;
text-align: center;
}
/*将a由内联设置成块*/
#nav .link a{
color:#eee;
display: block;
}
/*鼠标效果*/
#nav .link a:hover,
#nav .active a{
background-color: #aaa;
}
/*日期区CSS*/
/*日期区CSS*/
#time{
position: absolute;
top:550px;
left:1150px;
width:140px;
color:green;
font-size:20px;
}
#footer .bottom{
position: absolute;
top:570px;
left:1150px;
color:#777;
}
.content{
text-indent: 35px;
line-height: 40px;
position: absolute;
top:250px;
font-size: 20px;
}
#footer .register{
position: relative;
top:190px;
left: 400px;
width: 100px;
height:50px;
font-size: 20px;
font-weight: bold;
border:1px solid gray;
border-radius: 5px;
border-color:blue;
}
#footer .login{
position: relative;
top:140px;
left: 700px;
width: 100px;
height:50px;
font-size: 20px;
font-weight: bold;
border:1px solid gray;
border-radius: 5px;
border-color:blue;
}
#footer .link a{
color:red;
text-align: center;
line-height: 50px;
display: block;
}
/*鼠标效果*/
#footer .link a:hover,
#footer .active a{
background-color: gray;
-webkit-border-radius: 5px;
}
login_check.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'login_check.jsp' starting page</title>
</head>
<body>
<%
java.sql.Connection conn;
java.sql.PreparedStatement pstmt = null; // 数据库预处理操作
java.sql.ResultSet rs;
String url="jdbc:mysql://localhost/id_passenger";
response.setCharacterEncoding("utf-8");
boolean flag = false; // 保存标记
String myname = null; // 保存真实姓名
%>
<%
try{
%>
<%
Class.forName("com.mysql.jdbc.Driver");
conn=java.sql.DriverManager.getConnection(url,"root","1111");
String sql = "SELECT name FROM user WHERE name=? AND password=?";
String name=request.getParameter("username");///获取表单
String password=request.getParameter("password");
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
rs = pstmt.executeQuery(); // 查询
if (rs.next()) { // 如果有数据,则可以执行
flag = true; // 表示登陆成功
myname = rs.getString(1);
}
%>
<%
} catch (Exception e) {
e.printStackTrace();
}
%>
<%
if (flag) { // 登陆成功
%>
<jsp:forward page="login_success.jsp">
<jsp:param name="myname" value="<%=myname%>" />
</jsp:forward>
<%
} else { // 登陆失败
%>
<jsp:forward page="register.html" />
<%
}
%>
</body>
</html>
login_sucess.jsp
<%@page contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
<title></title>
<style>
body background: url(../images/12306.png) #F8F8F8 no-repeat center width="1200" height="900";}
</style>
</head>
<body>
<h1 style="text-align:center">
欢迎 <font color="red"><%=request.getParameter("myname")%></font> 光临!
</h1>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charse=utf-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<link rel="stylesheet" type="text/css" href="css/login.css">
<script>
function logincheckin()
{
name=userregister.name.value;
password=userregister.password.value;
if(name=="")
{
alert("用户名不能为空");
return false;
}
if(password=="")
{
alert("密码不能为空");
return false;
}
return true;
}
</script>
</head>
<body>
<div class="up">
<h1>Welcome to login</h1>
<h2>12306 are always at your service</h2>
</div>
<br><br>
<div class="content">
<form action="MyLoginServlet" onSubmit="logincheckin()" method="post" >
<table width="500" border="1" cellspacing="0">
<caption>Login for 12306</caption>
<tr>
<td>Your name: </td>
<td><input type="text" name="username" ></td>
</tr>
<tr>
<td>Your password: </td>
<td><input type="password" name="password"></td>
</tr>
</table>
<br>
<input type="submit" name="submit" value="提交" style="height:40px;width:500px;-webkit-border-radius:5px;font-size:20px">
<br><br><br>
<br><br><br>
<input type="checkbox" name="" value="">记住密码
<input type="checkbox" name="" value="">自动登录
</form>
</div>
</body>
</html>
register_check.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ pagecontentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'register.jsp' starting page</title>
</head>
<body>
<%
java.sql.Connection conn;
java.sql.PreparedStatement pstmt;
java.sql.ResultSet rs;
String myname="";
String url="jdbc:mysql://localhost/id_passenger";
request.setCharacterEncoding("gb2312");
String name=request.getParameter("name");
String password=request.getParameter("password");
String Verifypwd=request.getParameter("Verifypwd");
String mailbox=request.getParameter("mailbox");
String tel=request.getParameter("tel");
String sqlStr="insert into user values(?,?,?,?,?)";
try{
Class.forName("com.mysql.jdbc.Driver");
conn=java.sql.DriverManager.getConnection(url,"root","1111");
pstmt=conn.prepareStatement(sqlStr);
pstmt.setString(1,name);
pstmt.setString(2,password);
pstmt.setString(3,Verifypwd);
pstmt.setString(4,mailbox);
pstmt.setString(5,tel);
pstmt.execute();
sqlStr ="select * from user where name='"+name+"'";
rs=pstmt.executeQuery(sqlStr);
if(rs.next())
{
myname = rs.getString(1);
%>
<h3 style="color:red;text-align:center;color:green;font-size:50px;"><%=myname%>注册成功</h3>
<table border="1" align="center" cellpadding ="3" cellspacing ="0" bordercolor="666">
<tr>
<td width="100">姓名</td>
<td width="100"><%=rs.getString(1) %></td>
</tr>
<tr>
<td width="100">密码</td>
<td width="100"><%=rs.getString(2) %></td>
</tr>
<tr>
<td width="100">确认密码</td>
<td width="100"><%=rs.getString(3) %></td>
</tr>
<tr>
<td width="100">邮箱</td>
<td width="100"><%=rs.getString(4) %></td>
</tr>
<tr>
<td width="100">电话</td>
<td width="100"><%=rs.getString(5) %></td>
</tr>
</table>
<%
}
}
catch(java.sql.SQLException e){
out.println(e.toString());
}
%>
</body>
</html>
register_error.jsp
<%@page contentType="text/html" pageEncoding="utf-8"%>
<%@ pagecontentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<p>error</p>
</body>
</html>
register_sucess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ pagecontentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'register.jsp' starting page</title>
</head>
<body>
<%
java.sql.Connection conn;
java.sql.PreparedStatement pstmt;
java.sql.ResultSet rs;
String myname="";
String url="jdbc:mysql://localhost/id_passenger";
request.setCharacterEncoding("gb2312");
String name=request.getParameter("name");
String password=request.getParameter("password");
String Verifypwd=request.getParameter("Verifypwd");
String Idnumber=request.getParameter("Idnumber");
String tel=request.getParameter("tel");
String sqlStr="insert into user values(?,?,?,?,?)";
try{
Class.forName("com.mysql.jdbc.Driver");
conn=java.sql.DriverManager.getConnection(url,"root","1111");
pstmt=conn.prepareStatement(sqlStr);
pstmt.setString(1,name);
pstmt.setString(2,password);
pstmt.setString(3,Verifypwd);
pstmt.setString(4,Idnumber);
pstmt.setString(5,tel);
pstmt.execute();
sqlStr ="select * from user where name='"+name+"'";
rs=pstmt.executeQuery(sqlStr);
if(rs.next())
{
myname = rs.getString(1);
%>
<h3 style="color:red;text-align:center;color:green;font-size:50px;"><%=myname%>注册成功</h3>
<table border="1" align="center" cellpadding ="3" cellspacing ="0" bordercolor="666">
<tr>
<td width="100">姓名</td>
<td width="100"><%=rs.getString(1) %></td>
</tr>
<tr>
<td width="100">密码</td>
<td width="100"><%=rs.getString(2) %></td>
</tr>
<tr>
<td width="100">确认密码</td>
<td width="100"><%=rs.getString(3) %></td>
</tr>
<tr>
<td width="100">身份证号</td>
<td width="100"><%=rs.getString(4) %></td>
</tr>
<tr>
<td width="100">电话</td>
<td width="100"><%=rs.getString(5) %></td>
</tr>
</table>
<%
}
}
catch(java.sql.SQLException e){
out.println(e.toString());
}
%>
</body>
</html>
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="css/register.css">
<script language="javascript">
function registercheckin()
{
name=userregister.name.value;
password=userregister.password.value;
Verifypwd=userregister.Verifypwd.value;
mailbox=userregister.mailbox.value;
tel=userregister.tel.value;
if(name=="")
{
alert("用户名不能为空");
form.name.focus();
return;
}
if (password == "")
{
alert("请输入密码 !");
password.focus();
return ;
}
if(password!=Verifypwd)
{
alert("两次输入密码不一致");
password.focus();
return;
}
var regm = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;//验证Mail的正则表达式,^[a-zA-Z0-9_-]:开头必须为字母,下划线,数字,
if (mailbox.value != "" && !mailbox.value.match(regm)) {
alert("邮件格式不对,检查后重新输入!");
form.email.focus();
return ;
}
return true;
}
</script>
</head>
<body>
<div class="up">
<h1>Register Form</h1>
<h3>Please fill all the texts in the fields</h3>
</div>
<br><br>
<form name="userregister" onSubmit="registercheckin()" action="MyRegisterServlet" method="post">
<table width="500" border="1" cellspacing="0">
<tr>
<td>Your name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Your pwd:</td>
<td><input type="password" name="password" ></td>
</tr>
<tr>
<td>Verify pwd: </td>
<td><input type="password" name="Verifypwd" ></td>
</tr>
<tr>
<td>Your mailbox:</td>
<td><input type="text" name="mailbox" ></td>
</tr>
<tr>
<td>Your tel: </td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td colspan=2>
<input type="submit" name="submit" value="提交" style="height:40px;width:240px;-webkit-border-radius:5px;font-size:20px">
<input type="submit" name="reset" value="重置" style="height:40px;width:240px;-webkit-border-radius:5px;font-size:20px">
</td>
</tr>
</table>
<br>
<br><br><br>
<input type="checkbox" name="" value="">记住密码
<input type="checkbox" name="" value="">自动登录
</form>
</body>
</html>
register.html
<!DOCTYPE html>
<html>
<head>
<title>reserve.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charse=utf-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<link rel="stylesheet" type="text/css" href="css/reserve.css">
<script>
function show()
{
var date = new Date();
var now = "";
now = date.getFullYear() + "年";
now = now + (date.getMonth()+1) + "月";
now = now + date.getDate() + "日";
now = now + date.getHours() + "时";
now = now + date.getMinutes() + "分";
now = now + date.getSeconds() + "秒";
document.getElementById("time").innerHTML =now;
setTimeout("show()", 1000);
}
</script>
</head>
<body οnlοad="show()">
<div>
<img src="images/logo.png" width="1366" height="180">
</div>
<nav id="nav">
<section class="center">
<h1 class="logo">星月旅行社</h1>
<ul class="link">
<li class="active"><a href="###">首页</a></li>
<li><a href="###">购票资讯</a></li>
<li><a href="###">火车订购</a></li>
<li><a href="###">出门旅游</a></li>
<li><a href="###">关于我们</a></li>
</ul>
</section>
</nav>
<div class="content">
<p> 中国铁路客户服务中心(12306网)是铁路服务客户的重要窗口,将集成全路客货运输信息,
为社会和铁路客户提供客货运输业务和公共信息查询服务。客户通过登录本网站,可以
查询旅客列车时刻表、票价、列车正晚点、车票余票、售票代售点、货物运价、车辆技术
参数以及有关客货运规章。铁路货运大客户可以通过本网站办理业务。请先注册账号,已有账号直接登录。
</p>
</div>
<div id="footer">
<ul class="link">
<li class="register"><a href="register.jsp">register</a></li>
<li class="login"><a href="login.html">login</a></li>
</ul>
<div id="time">
</div>
</div>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MyLoginServlet</servlet-name>
<servlet-class>com.MyLoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MyRegisterServlet</servlet-name>
<servlet-class>com.MyRegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MyLoginServlet</servlet-name>
<url-pattern>/MyLoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MyRegisterServlet</servlet-name>
<url-pattern>/MyRegisterServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Encoder</filter-name>
<filter-class>myFilter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>newcharset</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoder</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>