JavaWeb JavaBean JSP MVC MySQL Tomcat JavaScript Bootstrap
基础JSP+Servlet或JSP+SSM(Spring、SpringMVC、MyBatis)框架或JSP+SSM+Maven(pom.xml)框架或SpringBoot…均可
开发工具:eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
import com.demo.service.impl.UserServiceImpl;
import com.demo.vo.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 用户模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
* 而后再调用UserService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
*/
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
/**
* 处理Post请求
*
* @param request
return (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS);
}
/**
* 判断字符串是否是乱码
*
* @param strName
* @return
*/
public static boolean isMessyCode(String strName) {
java.util.regex.Pattern p = java.util.regex.Pattern.compile("\\s*|\t*|\r*|\n*");
import com.demo.vo.User;
import javax.imageio.ImageIO;
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 java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
str = new String(str.getBytes("GB2312"), "ISO-8859-1");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("GBK"), "ISO-8859-1");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("UTF-8"), "GB2312");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("ISO-8859-1"), "GB2312");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("GBK"), "GB2312");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("UTF-8"), "GBK");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("ISO-8859-1"), "GBK");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("GB2312"), "GBK");
<%@ include file="include/head.jsp" %>
</head>
<body>
<div class="container-fluid">
<ul class="nav nav-tabs">
<li><a href="UserServlet?action=list">用户列表</a></li>
<li class="active"><a href="#">添加</a></li>
</ul>
<br/>
<form class="form-horizontal" role="form" action="UserServlet?action=add" method="post" onsubmit="return check()">
<div class="form-group">
<label class="col-sm-3 control-label">用户名:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="username" name="username">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">密码:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="password" name="password">
</div>
if (totalRecord % pageSize == 0) {
//说明整除,正好每页显示pageSize条数据,没有多余一页要显示少于pageSize条数据的
this.totalPage = totalRecord / pageSize;
} else {
//不整除,就要在加一页,来显示多余的数据。
this.totalPage = totalRecord / pageSize + 1;
}
//开始索引
this.startIndex = (pageNum - 1) * pageSize;
//显示5页,这里自己可以设置,想显示几页就自己通过下面算法修改
this.start = 1;
list.add(vo);
}
String sqlCount = "select count(*) from `t_user` where 1=1 " + condition;
ps = c.prepareStatement(sqlCount);
rs = ps.executeQuery();
if (rs.next()) {
totalCount = rs.getInt(1);
}
rs.close();
ps.close();
c.close();
this.end = 5;
}
if (end > this.totalPage) {
//比如当前页是倒数第2页或者最后一页,也同样不符合上面这个规则
this.end = totalPage;
this.start = end - 5;
}
}
}
//get、set方法。
public int getPageNum() {
return pageNum;
<input type="text" class="form-control" id="password" name="password">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">姓名:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="realName" name="realName">
</div>
</div>
<div class="form-group">
public void setId(Long id) {
this.id = id;
}
public String getLaifangName() {
return laifangName;
}
public void setLaifangName(String laifangName) {
this.laifangName = laifangName;
}
public String getLaifangSex() {
return laifangSex;
}
ps.execute();
ps.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//@Override
public boolean delete(long id) {
try {
Connection c = Util.getConnection();
Statement s = c.createStatement();
String sql = "delete from `t_laifang` where id = " + id;
s.execute(sql);
s.close();
c.close();
return true;
} catch (Exception e) {
e.printStackTrace();
public String getLaifangOutdate() {
return laifangOutdate;
}
public void setLaifangOutdate(String laifangOutdate) {
this.laifangOutdate = laifangOutdate;
}
public String getLaifangReason() {
return laifangReason;
}
public void setLaifangReason(String laifangReason) {
this.laifangReason = laifangReason;
return laifangText;
}
public void setLaifangText(String laifangText) {
this.laifangText = laifangText;
}
}
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>来访编辑</title>
<%@ include file="include/head.jsp" %>
</head>
//不整除,就要在加一页,来显示多余的数据。
this.totalPage = totalRecord / pageSize + 1;
}
//开始索引
this.startIndex = (pageNum - 1) * pageSize;
//显示5页,这里自己可以设置,想显示几页就自己通过下面算法修改
this.start = 1;
this.end = 5;
//显示页数的算法
if (totalPage <= 5) {
//总页数都小于5,那么end就为总页数的值了。
/**
* 公告模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
* 而后再调用NoticeService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
*/
//@WebServlet("/NoticeServlet")
public class NoticeServlet extends HttpServlet {
/**
* 处理Post请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//过滤编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String action = Util.decode(request, "action");
<input type="submit" class="btn btn-line btn-primary btn-sm" value="保存">
<input type="button" class="btn btn-line btn-default btn-sm" value="返回" onclick="javascript:history.back(-1);">
</div>
</div>
</form>
</div>
</body>
<script type="text/javascript">
//提交之前进行检查,如果return false,则不允许提交
function check() {
//根据ID获取值
if (document.getElementById("username").value.trim().length == 0) {
alert("用户名不能为空!");
}
public String getLaifangReason() {
return laifangReason;
}
public void setLaifangReason(String laifangReason) {
this.laifangReason = laifangReason;
}
public String getLaifangText() {
return laifangText;
}
public void setLaifangText(String laifangText) {
this.laifangText = laifangText;
}
*/
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
/**
* 处理Post请求
*
* @param request
* @param response
* @throws ServletException
//@Override
public void update(Notice vo) {
String sql = "update `t_notice` set `notice_name` = ? ,`notice_text` = ? ,`notice_type` = ? ,`create_date` = ? where `id` = ?";
try {
Connection c = Util.getConnection();
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1, vo.getNoticeName());
ps.setString(2, vo.getNoticeText());
ps.setString(3, vo.getNoticeType());
ps.setString(4, vo.getCreateDate());
ps.setLong(5, vo.getId());
ps.execute();
ps.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
response.sendRedirect("notice_list.jsp");
}
}
package com.demo.dao.impl;
import com.demo.util.Util;
import com.demo.dao.UserDAO;
import com.demo.vo.User;
import java.io.Serializable;
import java.sql.Connection;
// 获得红色的随机颜色值
int red = minColor + random.nextInt(maxColor - minColor);
// 获得绿色的随机颜色值
int green = minColor + random.nextInt(maxColor - minColor);
// 获得蓝色的随机颜色值
int blue = minColor + random.nextInt(maxColor - minColor);
return new Color(red, green, blue);
}
}
package com.demo.servlet;
import com.demo.util.Util;
import com.demo.service.LaifangService;
import com.demo.service.impl.LaifangServiceImpl;
import com.demo.vo.Laifang;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getStartIndex() {
return startIndex;
}
}
/**
* 测试连接是否成功
*
* @param args
* @throws Exception
*/
public static void main(String[] args) {
try {
Connection conn = Util.getConnection();
System.out.println("数据库连接成功!!!");
conn.close();
} catch (Exception e) {
e.printStackTrace();
private int totalPage; //总页数,通过totalRecord和pageSize计算可以得来
//开始索引,也就是我们在数据库中要从第几行数据开始拿,有了startIndex和pageSize,
//就知道了limit语句的两个数据,就能获得每页需要显示的数据了
private int startIndex;
//分页显示的页数,比如在页面上显示1,2,3,4,5页,start就为1,end就为5,这个也是算过来的
private int start;
private int end;
private String servlet;//查询时要请求的接口
private String searchColumn;//待模糊查询的列
private String keyword;//待模糊查询的关键字
if ("add".equals(action)) {//增加
Notice vo = new Notice();
//取出页面传进来的各个数据,并设置到Notice对象的属性里
vo.setNoticeName(Util.decode(request, "noticeName"));
vo.setNoticeText(Util.decode(request, "noticeText"));
vo.setNoticeType(Util.decode(request, "noticeType"));
vo.setCreateDate(Util.decode(request, "createDate"));
NoticeService noticeService = new NoticeServiceImpl();
//调用Service层增加方法(add),增加记录
noticeService.add(vo);
this.redirectList(request, response);
} else if ("delete".equals(action)) {//删除
//取出表要删除的公告记录的主键
long id = Long.parseLong(Util.decode(request, "id"));
NoticeService noticeService = new NoticeServiceImpl();
//调用Service层删除方法(delete),将对应的记录删除
noticeService.delete(id);
for (User user : list) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {//找到这个管理员了
request.getSession().setAttribute("loginUser", user);
request.getRequestDispatcher("menu.jsp").forward(request, response);
return;
}
}
request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else if ("register".equalsIgnoreCase(action)) {//注册
String username = Util.decode(request, "username");
String password = Util.decode(request, "password");
System.out.println("username=" + username);
System.out.println("password=" + password);
UserService userService = new UserServiceImpl();
Map<String, Object> params = new HashMap();
params.put("searchColumn", "username");//使用`username`字段进行模糊查询
}
if (isMessyCode(str)) {
str = new String(str.getBytes("GBK"), "UTF-8");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("GB2312"), "ISO-8859-1");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("GBK"), "ISO-8859-1");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("UTF-8"), "GB2312");
}
if (isMessyCode(str)) {
str = new String(str.getBytes("ISO-8859-1"), "GB2312");
</script>
</head>
<body>
<div class="bg"></div>
<div class="container">
<div class="line bouncein">
<div class="xs6 xm4 xs3-move xm4-move">
<div style="height:150px;"></div>
<div class="media media-y margin-big-bottom">
</div>
<form action="AuthServlet?action=login" method="post" onsubmit="return check()">
<div class="panel loginbox">
<div class="text-center margin-big padding-big-top" style="font-size: 35px;font-weight: 700;color:#000000;text-shadow: 2px 3px #FFFFFF;">访客管理系统</div>
<a style="font-size: 24px;color: #269abc;text-decoration: none;padding-left: 140px;">登录</a>       <a href="register.jsp" style="font-size: 24px;color:black ;text-decoration: none;">注册</a>
<div class="panel-body" style="padding:30px; padding-bottom:10px; padding-top:10px;">
// 保存minColor最大不会超过255
if (maxColor > 255)
maxColor = 255;
// 获得红色的随机颜色值
int red = minColor + random.nextInt(maxColor - minColor);
// 获得绿色的随机颜色值
int green = minColor + random.nextInt(maxColor - minColor);
// 获得蓝色的随机颜色值
int blue = minColor + random.nextInt(maxColor - minColor);
return new Color(red, green, blue);
}
}
package com.demo.servlet;
import com.demo.util.Util;
import com.demo.service.LaifangService;
import com.demo.service.impl.LaifangServiceImpl;
import com.demo.vo.Laifang;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
运行环境
Java≥6、Tomcat≥7.0、MySQL≥5.5
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
技术框架
JavaWeb JavaBean JSP MVC MySQL Tomcat JavaScript Bootstrap
基础JSP+Servlet或JSP+SSM(Spring、SpringMVC、MyBatis)框架或JSP+SSM+Maven(pom.xml)框架或SpringBoot…均可
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
登录、注册、退出、用户模块、公告模块、来访模块的增删改查管理
↖[获取源码方式]见左侧