@CHARSET "utf-8";(common.css)
/** 图书分享平台公用css开始部分*/
body {
background-image: url("images/find.jpg");
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: 100% 100%;
font-family: "Microsoft YaHei";
}
.warpper {
width: 100%;
border: 1px solid;
min-height: 500px;
margin: auto;
}
/**顶部部分*/
#headerWarpper {
width: 100%;
height: 50px;
background-color: #2B6C85;
}
#headerWarpper #header {
height: 50px;
width: 1200px;
margin: 0 auto;
}
#headerWarpper #header .logo {
float: left;
width: 240px;
height: 50px;
background-image: url("images/booklogo.png");
background-position: 0px 5px;
background-size: 40px 40px;
background-repeat: no-repeat;
font-size: 20px;
color: #FFF;
line-height: 50px;
text-align: left;
padding-left: 50px;
}
#headerWarpper #header .menu {
float: left;
width: 500px;
height: 50px;
}
#headerWarpper #header .menu li {
float: left;
width: 120px;
height: 50px;
}
#headerWarpper #header .menu li a {
width: 120px;
height: 50px;
line-height: 50px;
color: #FFF;
font-size: 15px;
text-decoration: none;
display: inline-block;
background-color: #4F92AB;
border-radius: 1px;
}
#headerWarpper #header .tool {
float: right;
width: 300px;
height: 50px;
position: relative;
}
#headerWarpper #header .tool input {
position: absolute;
top: 10px;
left: 0px;
width: 240px;
height: 30px;
border-radius: 3px;
background-color: #E3F7FF;
text-indent: 4px;
border: 1px solid #E3F7FF;
}
#headerWarpper #header .tool .search_button {
position: absolute;
top: 11px;
left: 211px;
background-color: #E3F7FF;
cursor: pointer;
}
#headerWarpper #header .tool .role_switch_button {
position: absolute;
top: 10px;
right: 0px;
cursor: pointer;
}
/**面包屑部分*/
#breadcrumb {
height: 30px;
margin: 0 auto;
width: 1200px;
}
#breadcrumb ul {
overflow: hidden;
}
#breadcrumb li {
height: 30px;
margin: 0 auto;
color: #707070;
float: left;
width: 70px;
line-height: 30px;
text-align: left;
}
#breadcrumb li a {
color: #707070;
}
.main {
overflow: hidden;
margin: 0 auto;
}
/**图书分享平台公用部分结束*/
@CHARSET "utf-8";(login.css)
body {
background-image: url("images/find.jpg");
background-repeat: no-repeat;
background-size: 100% 100%;
background-position: center;
background-attachment: fixed;
font-family: Microsoft YaHei;
}
.login_title {
height: 110px;
width: 460px;
border: 1px solid;
position: absolute;
background-image: url("images/write.jpg");
background-size: 460px 110px;
background-repeat: no-repeat;
top: 400px;
left: 140px;
text-indent: -1000px;
}
.login_form {
width: 360px;
height: 380px;
background-color: #E5EEFF;
border-radius: 3px;
position: absolute;
top: 160px;
left:720px;
}
.login_form .logo {
width: 140px;
height: 25px;
margin-top: 30px;
margin-left: 40px;
background-image: url("images/logo.png");
background-size: 140px 25px;
background-repeat: no-repeat;
}
.login_form .title {
width: 340px;
height: 40px;
margin-top: 15px;
margin-left: 40px;
text-align: left;
font-size: 40px;
color: #5165b2;
line-height: 40px;
}
.login_form .line {
height: 30px;
margin-top: 40px;
position: relative;
line-height: 30px;
}
#loginForm label {
position: absolute;
top: 0px;
left: 40px;
font-size: 15px;
color: #333;
}
#loginForm input {
position: absolute;
top: 0px;
left: 110px;
width: 210px;
height: 28px;
border: 1px solid #858585;
border-radius: 3px;
text-indent: 4px;
}
.login_form .button {
margin-top: 20px;
width: 210px;
height: 30px;
margin-left: 110px;
line-height: 30px;
font-size: 15px;
background: #5394cb;
border-radius: 3px;
color: #ffffff;
cursor: pointer;
}
.login_footer {
width: 800px;
height: 30px;
border-top: 1px solid #E7E7E7;
position: relative;
top: 600px;
margin: auto;
color: #E7E7E7;
font-size: 12px;
padding-top: 10px;
}
@CHARSET "utf-8";(reset.css)
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,
center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,
section,summary,time,mark,audio,video{
margin: 0;
padding:0;
}
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
display: block;
}
body {
font: 12px/1.55em arial, helvetica, sans-serif;
text-align: center;
background-color: #FFFFFF;
}
ol, ul {
list-style: none;
}
a {
text-decoration: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>很抱歉,你访问的页面不存在。
</body>
</html>
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/exam
jdbc.user = root
jdbc.password = abc123456
package com.albert.exam;
public class Constants {
public static final String TIP_MESSAGE = "TIP_MESSAGE";
public static final String ERROR_FIELDS = "ERROR_FIELDS";
public static final String USER = "USER";
}
package com.albert.exam.exception;
public class SqlNotFoundException extends Exception{
private static final long serialVersionUID = -5203831562015184247L;
String errorMessage = "";
public String getErrorMessage(){
return errorMessage;
}
public void addErrorMessage(String message){
errorMessage = message;
}
public boolean isErrorMessage(){
return !errorMessage.equals("");
}
}
package com.albert.exam.exception;
import java.util.HashMap;
import java.util.Map;
public class ParameterException extends Exception{
private static final long serialVersionUID = 6326532694067109305L;
Map<String,String> errorFileds = new HashMap<String,String>();
public Map<String,String> getErrorfileds(){
return errorFileds;
}
public void setErrorFileds(Map<String,String> errorFileds){
this.errorFileds = errorFileds;
}
public void addErrorFiled(String filedName,String message){
errorFileds.put(filedName, message);
}
public boolean isErrorFiled(){
return !errorFileds.isEmpty();
}
}
package com.albert.exam.exception;
public class DBException extends RuntimeException {
private static final long serialVersionUID = 5969405751786729079L;
}
package com.albert.exam.model;
public class User {
private int id;
private String userName;
private String userPassword;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userPassword="
+ userPassword + "]";
}
}
package com.albert.exam.service;
public class ServiceException extends Exception{
private static final long serialVersionUID = 1L;
private int code;
private String message;
public ServiceException(int code, String message) {
super();
this.code = code;
this.message = message;
}
public ServiceException(String message) {
super();
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.albert.exam.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.albert.exam.exception.DBException;
import com.albert.exam.model.User;
import com.albert.exam.util.DBUtil;
public class UserDao {
public User getUserByName(String userName){
User user = null;
if(userName == null || userName == ""){
return null;
}
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
//3:准备PreparedStatement
stmt = (PreparedStatement) conn.prepareStatement("SELECT * FROM user WHERE user_name = ?");
stmt.setString(1,userName);
//4.得到数据处理
rs = stmt.executeQuery();
if (rs.next() ){
user = new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("user_name"));
user.setUserPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
throw new DBException();
} finally {
//5.关闭
DBUtil.close(conn, stmt, rs);
}
return user;
}
}
package com.albert.exam.service;
import com.albert.exam.dao.UserDao;
import com.albert.exam.exception.ParameterException;
import com.albert.exam.exception.SqlNotFoundException;
import com.albert.exam.model.User;
public class UserService {
public User login(String userName,String password) throws ParameterException, SqlNotFoundException, ServiceException{
ParameterException parameterException = new ParameterException();
if (userName == null || userName.equals("")) {
parameterException.addErrorFiled("userName","User Name is required");
}
if(password == null || password.equals("")){
parameterException.addErrorFiled("password","User password is required");
}
if(parameterException.isErrorFiled()){
throw parameterException;
}
User user = null;
UserDao userDao = new UserDao();
user = userDao.getUserByName(userName);
SqlNotFoundException sqlNotFoundException = new SqlNotFoundException();
if (user == null){
throw new ServiceException(1000,"用户不存在");
}
if (!password.equals(user.getUserPassword())) {
throw new ServiceException(1001,"用户密码错误");
}
if(sqlNotFoundException.isErrorMessage()){
throw sqlNotFoundException;
}
return user;
}
}
package com.albert.exam.util;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.albert.exam.exception.DBException;
public class DBUtil {
public static Connection getConnection(){
//1:加载驱动
try{
Class.forName(PropertyUtil.getProperty("jdbc.driver"));
} catch (ClassNotFoundException e){
e.printStackTrace();
}
//2:得到connection对象
//String jdcUrl="jdbc:mysql://localhost:3306/book";
String jdcUrl = PropertyUtil.getProperty("jdbc.url");
System.out.println(jdcUrl);
Connection conn = null;
try{
conn = (Connection) DriverManager.getConnection(jdcUrl,PropertyUtil.getProperty("jdbc.user"),PropertyUtil.getProperty("jdbc.password"));
} catch (SQLException e){
e.printStackTrace();
throw new DBException();
}
return conn;
}
public static void close(Connection conn,PreparedStatement stmt,ResultSet rs){
try {
if (rs != null){
rs.close();
}
if (stmt != null){
stmt.close();
}
if (conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.albert.exam.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyUtil {
private static Properties p = null;
static {
InputStream in = null;
try {
//in = ClassLoader.getSystemResourceAsStream("app.properties");
in = PropertyUtil.class.getClassLoader().getResourceAsStream("app.properties");
p = new Properties();
p.load(in);
} catch (IOException e){
e.printStackTrace();
} finally {
//if (in != null){
try {
if (in != null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}
}
}
public static String getProperty(String key){
return p.getProperty(key);
}
}
package com.zhengmenbb.book.servlet;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.albert.exam.Constants;
import com.albert.exam.exception.ParameterException;
import com.albert.exam.exception.SqlNotFoundException;
import com.albert.exam.model.User;
import com.albert.exam.service.ServiceException;
import com.albert.exam.service.UserService;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String LOGIN_PAGE = "/WEB-INF/jsp/login.jsp";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = null;
UserService userService = new UserService();
try{
user = userService.login(request.getParameter("userName"),request.getParameter("password"));
HttpSession session = request.getSession();
session.setAttribute("USER", user);
response.sendRedirect(request.getContextPath()+"/mybook");
} catch (ParameterException paramException) {
Map<String, String> errorFields = paramException.getErrorfileds();
request.setAttribute(Constants.ERROR_FIELDS, errorFields);
request.getRequestDispatcher(LOGIN_PAGE).forward(request,response);
} catch (ServiceException serviceException) {
request.setAttribute(Constants.TIP_MESSAGE, serviceException.getMessage() + "{" + serviceException.getCode() + "}");
request.getRequestDispatcher(LOGIN_PAGE).forward(request,response);
serviceException.printStackTrace();
} catch (SqlNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}