特此声明:本项目只做学习模仿借鉴学习使用。
随着时代的发展和科技的不断更新换代,人们也渐渐习惯了运用计算机等科技产品进行代替传统的行为已达到节省空间,时间,财力,物力等目的。
20世纪90年代后期,特别是近几年,我国的超市产业飞速发展,其经营模式更为复杂,旧的管理体制已经无法适应超市的发展。这就迫切的需要引进新的管理技术。超市的数据和业务越来越庞大,而计算机就是一种高效的管理系统。这就需要我们把超市的管理与计算机结合起来,因此,超市管理系统应运而生。依靠现代化的计算机信息处理技术来管理超市,节省了大量的人力和物力,缓解了员工的压力,并且能够快速的对商品信息进行管理和数据分析,从而使管理人员能快速对市场的变化做出相应的决策,加快超市经营管理效率。因此本人设计并开发了商品货物管理系统。
商品货物管理系统可以用来查询对录入货物的各种信息、从而方便员工的查看和管理,具有完善的功能。在开发中,采用了JSP、Servlet及JavaBean等技术,使用MVC模式和SQL数据库设计开发了本系统。
主界面
修改产品信息
删除产品信息
查看用户信息
CREATE TABLE product
(
id
INT ( 11 ) NOT NULL auto_increment,
name
VARCHAR ( 20 ) NOT NULL,
addr
VARCHAR ( 50 ) NOT NULL,
price
DOUBLE NOT NULL,
PRIMARY KEY ( id
)
) ENGINE = INNODB AUTO_INCREMENT = 6 DEFAULT CHARSET = utf8;
DROP TABLE
IF EXISTS user
;
CREATE TABLE user
(
id
INT ( 11 ) NOT NULL auto_increment,
name
VARCHAR ( 20 ) NOT NULL,
password
VARCHAR ( 20 ) NOT NULL,
type
VARCHAR ( 20 ) DEFAULT NULL,
PRIMARY KEY ( id
)
) ENGINE = INNODB AUTO_INCREMENT = 2 DEFAULT CHARSET = utf8;
1、贴出开发环境下系统的结构图,并全部展开,列出每个文件的作用。
2、说明程序的首页是哪个,相互之间的调用关系。
login.jsp
通过login.jsp进入程序
调用关系
拆分图一
拆分图二
3、系统有什么特色,运用了哪些新技术,并举例说明。
本系统运用的是MVC模式也就是经典模式Jsp + Servlet + JavaBean的模式,其中运用了EL表达式和JSTL标签还有部分JS编程语言另外使用数据库连接池实现与数据库的交互,采用外部样式表将样式分离开来,使系统可操作性更高,更灵活,同时对各个jsp页面进行了美化,以提高用户体验感,增加了用户登陆过滤器等。
首先,学习这门课程就必须要先了解什么是Web应用。通过游览器可以访问新浪网,中华网,淘宝网,网易,搜狐网,微软的软件及sun公司的网站等。这些就是Web应用对于Web应用,我们需要使用浏览器,通过网络访问在远程的服务器运行的程序。Web应用指的就是这些网站中的程序。
其次是编程,学习编程最重要的就是勤记忆,勤练习,勤思考,需要记忆很多,有些东西必须要死记,没有偷懒的办法,在记忆的时候可以找一些规律,比如把类似的东西放在一起记忆,平时在做练习的时候,书上的例题和习题很重要,最后在期末复习过程中,要不时的复习前面的知识。这样才能熟练的掌握所学的知识。在思考方面,就是在平时的编程练习中遇到困难的时候,不要立即急着去问身边的同学应当自己应当对照相关学习资料慢慢进行修改,只有在自己确实已经尽力但仍然不知道如何操作的情况下才去询问别人,这样才能加深印象。
还记得自己当初最开始学习Java的场景,那时候只知道什么是继承,封装,多态,抽象类接口这些外,其他的就什么都不知道了,后来自己在慢慢探索中才渐渐积了对这门学科的敏感度。开始javaweb觉得servlet是一个非常深奥的技术,我曾经想学习网页设计,看到HTML那些松散的标记,我放弃了,最终也没有学会做网页。知道网页编辑工作有DW,当接触servlet后发现做网页居然也可以像写Java程序一样进行编写。接下来很快就接触到了JSP,JSP并不需要我在java程序的写HTML代码,而是变成了在HTML中写java程序。一开始学的时候,所有的数据库连接,数据库访问,数据显示,业务代码和HTML标记等都写在JSP代码中,觉得JSP真的是无所不能,所以在做一些小应用,使开发速度也是非常快。而当遇到了比较复杂的业务逻辑后,JSP开发就像是一个噩梦,每天大量的工作就是调试页面,当其它页面需要类似的功能时,进行代码拷贝来进行代码的复用,后来才知道这就是典型的Model 1模型。当把JSP与Servlet相结合后,一切都发生了改变。JSP仅用于数据展示,而Servlet用于进行业务调用和页面流程控制,这就是所谓的Model2模型。原来JSP并不是Servlet的替代品,JSP 更需要与Servlet 进行合作。再明白了Servlet 与的重要后,才回过头开始去学习Servlet,包括Servlet的生命周期,Servlet 的重要接口及方法,自定义标签等。通过这样反复的联系我终于在面对java和jsp时不再像以前那样那么没有自信了。所以花费些力气去学习Java基础,学习Jsp,Servlet,甚 于学HTML,Javascript对进行Web开发都是十分必要的。另外由于编程有一些专用的英语术语,因此学好一些必要与之相关的英语对我们今后更好地学习这门学科是很有帮助的。
“Java Web程序设计”是一门涉及技术种类较多,与实际开发结合紧密的专业课.针对该课程的特点,以项目驱动的方式设计教学模式,将教学环节分解为理论课,上机课,综合性实验和课程设计四个部分,并以一个项目的开发过程贯穿起来.同时,在不同的教学环节采用不同的教学方法和手段,以提高我们学生实际开发能力。
Product.java
package com.jdbc.bean;
/**
* 商品实体类
* @author
*
*/
public class Product {
private int id;
private String name;
private String addr;
private double price;
public Product() {
super();
}
public Product(int id, String name, String addr, double price) {
super();
this.id = id;
this.name = name;
this.addr = addr;
this.price = price;
}
public Product(String name, String addr, double price) {
super();
this.name = name;
this.addr = addr;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
User.Java
在这里插入代码片package com.jdbc.bean;
/**
* 用户实体类
* @author
*
*/
public class User {
private int id;
private String name;
private String password;
private String type;
public User() {
super();
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public User(int id, String name, String password, String type) {
super();
this.id = id;
this.name = name;
this.password = password;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", type=" + type + "]";
}
}
ConnectDatabase.java
package com.jdbc.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 数据库连接工具类
* @author
*
*/
public class ConnectDatabase {
private static final String DRIVER_STRING="com.mysql.cj.jdbc.Driver";
private static final String URL_STRING="jdbc:mysql://localhost:3306/dbms?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8";
private static final String USER_STRING="root";
private static final String PASS_STRING="1234";
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName(DRIVER_STRING);
connection=DriverManager.getConnection(URL_STRING, USER_STRING, PASS_STRING);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public void realse(Connection conn,PreparedStatement ps,ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DaoFactory.java
package com.jdbc.dao;
import com.jdbc.service.IProductDao;
import com.jdbc.service.IUserDao;
/**
* DAO工厂类,为DAO调用者提供DAO实例对象
* @author
*
*/
public class DaoFactory {
public static IUserDao getUserDao(){
return new UserDaoImp();
}
public static IProductDao getProductDao(){
return new ProductImp();
}
}
ProductImp.java
package com.jdbc.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.jdbc.bean.Product;
import com.jdbc.conn.ConnectDatabase;
import com.jdbc.service.IProductDao;
/**
* 商品操作接口实现类,实现商品的增加、删除、更新、添加等操作
* @author
*
*/
public class ProductImp implements IProductDao {
PreparedStatement ps = null;
public boolean insert(Product p) {
String sql ="insert into product(name,addr,price) values(?,?,?)";
int n =0;
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1, p.getName());
ps.setString(2, p.getAddr());
ps.setDouble(3, p.getPrice());
n=ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}
return n>0;
}
public boolean delete(Product p) {
String sql ="delete from product where id="+p.getId();
int n=0;
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
n=ps.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
return n>0;
}
public boolean update(Product p) {
String sql = "update product set name=?,addr=?,price=? where id=?";
int n = 0;
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1, p.getName());
ps.setString(2, p.getAddr());
ps.setDouble(3, p.getPrice());
ps.setInt(4, p.getId());
n=ps.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
return n>0;
}
public List<Product> queryProduct(Map<String, Object> map) {
StringBuffer sql= new StringBuffer("select * from product where 1 =1");
if(map.get("keyword")!=null && map.get("keyword")!=""){
sql.append(" and name like '"+"%"+map.get("keyword")+"%'"
+"or addr like '"+"%"+map.get("keyword")+"%'"+
"or price like '"+"%"+map.get("keyword")+"%'"
);
}
List<Product> list = new ArrayList<Product>();
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql.toString());
ResultSet rs = ps.executeQuery();
while(rs.next()){
Product p = new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setAddr(rs.getString("addr"));
p.setPrice(rs.getDouble("price"));
list.add(p);
}
}
catch (Exception e) {
e.printStackTrace();
}
return list;
}
public Product queryById(Product p) {
Product product = null;
String sql = "select * from product where id="+p.getId();
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setAddr(rs.getString("addr"));
product.setPrice(rs.getDouble("price"));
}
}catch(Exception e){
e.printStackTrace();
}
return product;
}
}
UserDaoImp.java
package com.jdbc.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.jdbc.bean.Product;
import com.jdbc.bean.User;
import com.jdbc.conn.ConnectDatabase;
import com.jdbc.service.IUserDao;
/**
* 用户接口操作实现类,实现用户相关的基本操作
* @author
*
*/
public class UserDaoImp implements IUserDao {
PreparedStatement ps = null;
/**
* 用户注册
*/
public boolean insert(User u) {
int n=0;
String sql="insert into user(name,password) values(?,?)";
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1, u.getName());
ps.setString(2, u.getPassword());
n = ps.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
return n>0;
}
public boolean delete(User u) {
String sql="delete from user where id=?";
int n = 0;
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
ps.setInt(1, u.getId());
n=ps.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
return n>0;
}
public boolean update(User u) {
String sql = "update user set name=?,password? where id=?";
int n = 0;
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1, u.getName());
ps.setString(2, u.getPassword());
ps.setInt(3, u.getId());
n=ps.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
return n>0;
}
/**
* 用户登录
*/
public User queryById(User u) {
User user =null;
String sql="select * from user where name=? and password=?";
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1,u.getName());
ps.setString(2,u.getPassword());
ResultSet rs = ps.executeQuery();
if(rs.next()){
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setType(rs.getString("type"));
}
}
catch(Exception e){
e.printStackTrace();
}
return user;
}
public List<User> queryUser(Map<String, Object> map) {
StringBuffer sql= new StringBuffer("select * from user where 1 =1");
if(map.get("keyword")!=null && map.get("keyword")!=""){
sql.append(" and id like '"+"%"+map.get("keyword")+"%'"
+"or name like '"+"%"+map.get("keyword")+"%'"
);
}
List<User> list = new ArrayList<User>();
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql.toString());
ResultSet rs = ps.executeQuery();
while(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
list.add(user);
}
}
catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**修改用户界面查询用户**/
public User queryByUserId(User u) {
User user =null;
String sql="select * from user where id=?";
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql);
ps.setInt(1,u.getId());
ResultSet rs = ps.executeQuery();
if(rs.next()){
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setType(rs.getString("type"));
}
}
catch(Exception e){
e.printStackTrace();
}
return user;
}
}
IproductDao.java
package com.jdbc.service;
import java.util.List;
import java.util.Map;
import com.jdbc.bean.Product;
import com.jdbc.bean.User;
/**商品操作接口
* @author
*
*/
public interface IProductDao {
public boolean insert(Product p);
public boolean delete(Product p);
public boolean update(Product p);
public List<Product> queryProduct(Map<String, Object> map);
public Product queryById(Product p);
}
IuserDao.java
package com.jdbc.service;
import java.util.List;
import java.util.Map;
import com.jdbc.bean.User;
/**
* 用户操作接口
* @author
*
*/
public interface IUserDao {
public boolean insert(User u);
public boolean delete(User u);
public boolean update(User u);
public User queryById(User u);
public User queryByUserId(User u);
public List<User> queryUser(Map<String, Object> map);
}
LoginServlet.java
package com.jdbc.servlet;
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 com.jdbc.bean.User;
import com.jdbc.dao.DaoFactory;
import com.jdbc.service.IUserDao;
/**
* 用户登录servlet,处理用户登录
* @author
*
*/
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取login.jsp中用户登录表单中提交的username
String name = request.getParameter("username");
//获取login.jsp中用户登录表单中提交的password
String password = request.getParameter("password");
//实例化一个user,将前台获取的username,password赋值给user实例的属性
User user = new User();
user.setName(name);
user.setPassword(password);
//调用DAO,进行用户登录
IUserDao dao = DaoFactory.getUserDao();
if(dao.queryById(user)!=null){
//登录成功则请求转发至首页
request.getRequestDispatcher("main.jsp").forward(request, response);
}else{
//登录失败则重定向至登录页
//response.sendRedirect("login.jsp");
request.getRequestDispatcher("login.jsp?reginfo=1").forward(request, response);
}
}
public void init() throws ServletException {
}
}
OperatorServlet.java
package com.jdbc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jdbc.bean.Product;
import com.jdbc.dao.DaoFactory;
import com.jdbc.service.IProductDao;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
/**
* 商品管理Servlet,处理商品删除、增加、修改、新增等操作
* @author
*
*/
public class OperatorServlet extends HttpServlet {
public OperatorServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取前台参数,根据参数选择对应的servlet方法进行业务处理
String op = request.getParameter("op");
if("update".equals(op)){//修改商品信息
updateService(request,response);
}else if("del".equals(op)){//删除商品
deleteService(request,response);
}else if("insert".equals(op)){//添加商品
insertService(request,response);
}else if("select".equals(op)){//查询商品
selectService(request,response);
}
}
/**
* 查询商品
* @param request
* @param response
*/
private void selectService(HttpServletRequest request,
HttpServletResponse response) {
IProductDao dao = DaoFactory.getProductDao();
String searchText = request.getParameter("searchText");
Map<String,Object> map = new HashMap<String, Object>();;
map.put("keyword", searchText);
List<Product> list = dao.queryProduct(map);
request.setAttribute("productList", list);
try{
if(list!=null){
request.getRequestDispatcher("product.jsp").forward(request, response);
}else{
response.sendRedirect("main.jsp");
}
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 添加商品
* @param request
* @param response
*/
private void insertService(HttpServletRequest request,
HttpServletResponse response) {
//获取前台传递的三个参数,其中id在数据中使用自增字段,无需手动输入
String name=request.getParameter("name");
String addr=request.getParameter("addr");
double price = Double.parseDouble(request.getParameter("price"));
Product p = new Product(name, addr, price);
IProductDao dao = DaoFactory.getProductDao();
try{
if(dao.insert(p)){
request.getRequestDispatcher("OperatorServlet?op=select").forward(request, response);
}else{
response.sendRedirect("insert.jsp");
}}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 删除商品
* @param request
* @param response
*/
private void deleteService(HttpServletRequest request,
HttpServletResponse response) {
//接收前台传递的待删除商品ID,根据id删除对应商品
//delete from product where id ="id";
int id =Integer.parseInt(request.getParameter("id"));
Product p = new Product();
p.setId(id);
IProductDao dao = DaoFactory.getProductDao();
try{
if(dao.delete(p)){
request.getRequestDispatcher("OperatorServlet?op=select").forward(request, response);
}else{
response.sendRedirect("OperatorServlet?op=select");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改商品
* @param request
* @param response
*/
private void updateService(HttpServletRequest request,
HttpServletResponse response) {
//获取前台更新商品的参数,根据商品id进行更新
//update product set
//product.name ="xxx",product.addr="xxx",product.price="xxx"
//where id ="待删除商品id";
int id = Integer.parseInt(request.getParameter("id"));
String name =request.getParameter("name");
String addr = request.getParameter("addr");
double price = Double.parseDouble(request.getParameter("price"));
Product p = new Product(id, name, addr, price);
IProductDao dao = DaoFactory.getProductDao();
try{
if(dao.update(p)){
request.getRequestDispatcher("OperatorServlet?op=select").forward(request, response);
}else{
response.sendRedirect("update.jsp");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void init() throws ServletException {
}
}
UserServlet.java
package com.jdbc.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jdbc.bean.User;
import com.jdbc.dao.DaoFactory;
import com.jdbc.service.IProductDao;
import com.jdbc.service.IUserDao;
/**
* 用户Servlet,处理用户登录,用户注册等
* @author
*
*/
public class UserServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UserServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取前台参数,根据参数选择对应的servlet方法进行业务处理
String op = request.getParameter("op");
if("register".equals(op)){//用户注册
registerService(request,response);
}else if("select".equals(op)){//查看用户
selectService(request,response);
}else if("del".equals(op)){//删除用户
deleteService(request,response);
}else if("update".equals(op)){//修改用户信息
updateService(request,response);
}
}
/**
* 删除用户
* @param request
* @param response
*/
private void deleteService(HttpServletRequest request,
HttpServletResponse response) {
//接收前台传递的待删除用户ID,根据id删除对应用户
//delete from product where id ="id";
int id =Integer.parseInt(request.getParameter("id"));
User p = new User();
p.setId(id);
IUserDao dao = DaoFactory.getUserDao();
try{
if(dao.delete(p)){
request.getRequestDispatcher("UserServlet?op=select").forward(request, response);
}else{
response.sendRedirect("UserServlet?op=select");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改用户
* @param request
* @param response
*/
private void updateService(HttpServletRequest request,
HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("id"));
String name =request.getParameter("name");
String password=request.getParameter("password");
User p = new User(id,name, password);
IUserDao dao = DaoFactory.getUserDao();
try{
if(dao.update(p)){
request.getRequestDispatcher("UserServlet?op=select").forward(request, response);
}else{
response.sendRedirect("UserServlet?op=select");
}
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 用户注册
* @param request
* @param response
* @throws ServletException
*/
private void registerService(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
//获取前台传递的三个参数,其中id在数据中使用自增字段,无需手动输入
String name=request.getParameter("name");
String password=request.getParameter("password");
String password2 = request.getParameter("password2");
User user =null;
if(password.equals(password2) && password != null && password.length() != 0 && password2 != null && password2.length() != 0) {
user = new User(name,password);
}else {
try {
request.getRequestDispatcher("register.jsp?reginfo=1").forward(request, response);
} catch (IOException e) {
e.printStackTrace();
}
}
IUserDao dao = DaoFactory.getUserDao();
try{
if(dao.insert(user)){
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
response.sendRedirect("register.jsp");
}}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 查询用户
* @param request
* @param response
*/
private void selectService(HttpServletRequest request,
HttpServletResponse response) {
IUserDao dao = DaoFactory.getUserDao();
String searchText = request.getParameter("searchText");
Map<String,Object> map = new HashMap<String, Object>();;
map.put("keyword", searchText);
List<User> list = dao.queryUser(map);
request.setAttribute("userList", list);
try{
if(list!=null){
request.getRequestDispatcher("user.jsp").forward(request, response);
}else{
response.sendRedirect("main.jsp");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void init() throws ServletException {
}
}
insert.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>添加商品</title>
<style>
input{
border:1px solid RGB(99,175,233);
}
</style>
</head>
<body>
<center>
<h2>添加商品</h2>
<form action="OperatorServlet?op=insert" method="post">
名称:<input type="text" name="name"/><br>
产地:<input type="text" name="addr"/><br>
价格:<input type="text" name="price"/><br><br>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
</center>
</body>
</html>
login.jsp
<%@page import="javafx.scene.control.Alert"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>用户登录</title>
<link rel="stylesheet" type="text/css" href="css/login.css" />
</head>
<body>
<div id="login-box">
<h1>用户登录</h1>
<div class="form">
<form action="Login" method="post">
<div class="item" >用户名称:<input type="text" name="username" placeholder="请输入你的账号"></div>
<div class="item">用户密码:<input type="password" name="password" placeholder="请输入你的密码"></div>
<div class="buttom"><input type="submit" value="登录"></div>
<div class="buttom"><input type="reset" value="重置"></div><br>
<a href="register.jsp">注册新用户</a>
</form>
<c:if test="${param.reginfo != null}">
<script type="text/javascript">alert("请检查你的账号或密码是否填写正确!");</script>
</c:if>
</div>
</div>
</body>
</html>
main.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%>">
<link rel="stylesheet" type="text/css" href="css/main.css">
<title>主页</title>
</head>
<body>
<div id="main-box">
<div class="top"></div>
<div class="banner">
<div class="banneruser">当前登录用户为: <a href=""><%=request.getParameter("username") %></a></div>
</div>
<div class="left"><br>
<div class="leftchoose"><a href="OperatorServlet?op=select" target="frame">全部产品</a></div>
<div class="leftchoose"><a href="insert.jsp" target="frame">添加产品</a></div>
<div class="leftchoose"><a href="UserServlet?op=select" target="frame">所有用户</a></div>
</div>
<div class="right"><iframe class="if" name="frame"></iframe></div>
</div>
</body>
</html>
product.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.jdbc.dao.DaoFactory"%>
<%@page import="com.jdbc.service.IProductDao"%>
<%@page import="com.jdbc.bean.Product" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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>
<meta charset="UTF-8">
<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">
<base href="<%=basePath%>">
<link rel="stylesheet" type="text/css" href="css/product.css" />
<title>显示产品信息</title>
<script>
function fun(val){
var r=confirm("确认删除吗?");
if(r){
location.href="OperatorServlet?op=del&id="+val;
}
}
</script>
</head>
<body>
<center>
<div>
<form action="OperatorServlet?op=select" method="post"
class="search">
<input class="productfind"name="searchText"type="text" placeholder="请输入名称或产地">
<input name="查询商品"type="submit" value="查询商品">
</form>
</div>
<table border="1" style="border:2px solid RGB(73,161,225)">
<tr>
<th>编号:</th>
<th>名称:</th>
<th>产地:</th>
<th>价格:</th>
<th colspan="2" width="200">操作</th>
</tr>
<c:forEach var="Product" items="${productList}">
<tr align="center">
<td>${Product.id}</td>
<td>${Product.name}</td>
<td>${Product.addr}</td>
<td>${Product.price}</td>
<td><a href="update.jsp?id=${Product.id}&op=update">修改</a></td>
<td><a href="javascript:void(0);" onclick="fun(${Product.id})">删除</a></td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
register.jsp
<%@page import="javafx.scene.control.Alert"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>
<link rel="stylesheet" href="css/register.css">
<base href="<%=basePath%>">
<title>用户注册</title>
</head>
<body>
<div id="register-box">
<h1>用户注册</h1>
<div class="form">
<form action="UserServlet?op=register" method="post" name=form>
<div class="item">用户名 : <input type="text" name="name"placeholder="请输入你的账号"></div>
<div class="item">密 码 : <input type="text" name="password"placeholder="请输入你的密码"></div>
<div class="item">确认密码:<input type="text" name="password2"placeholder="密码输入需一致"></div>
<div class="buttom"><input type="submit" value="注册"></div>
<div class="buttom"><input type="reset" value="重置"></div><br>
<a href="login.jsp">已注册前往登录</a>
<c:if test="${param.reginfo != null}">
<script type="text/javascript">alert("数据填写错误,请重新输入正确数据!");</script>
</c:if>
</form>
</div>
</div>
</body>
</html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.jdbc.bean.Product"%>
<%@page import="com.jdbc.dao.DaoFactory"%>
<%@page import="com.jdbc.service.IProductDao"%>
<%
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>更新产品</title>
<style>
input{
border:1px solid RGB(99,175,233);
}
}
</style>
</head>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
int id =Integer.parseInt(request.getParameter("id"));
Product p = new Product();
p.setId(id);
IProductDao dao = DaoFactory.getProductDao();
p=dao.queryById(p);
%>
<body>
<center>
<h2>修改产品信息</h2>
<form action="OperatorServlet?op=update" method="post">
<table border="0" >
<tr>
<td>编号:</td>
<td>
<input type="text" name="id" readonly="readonly" value="<%=p.getId() %>"/>
</td>
</tr>
<tr>
<td>名称:</td>
<td>
<input style="color:#7a7a7a" type="text" name="name" value="<%=p.getName()%>"/>
</td>
</tr>
<tr>
<td>产地:</td>
<td>
<input style="color:#7a7a7a" type="text" name="addr" value="<%=p.getAddr()%>"/>
</td>
</tr>
<tr>
<td>价格</td>
<td>
<input style="color:#7a7a7a" type="text" name="price" value="<%=p.getPrice()%>"/>
</td>
</tr>
</table><br>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
</center>
</body>
</html>
updateUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.jdbc.bean.User"%>
<%@page import="com.jdbc.dao.DaoFactory"%>
<%@page import="com.jdbc.service.IUserDao"%>
<%
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>
<meta charset="UTF-8">
<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">
<base href="<%=basePath%>">
<link rel="stylesheet" type="text/css" href="css/user.css">
<title>修改产品</title>
</head>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
int id =Integer.parseInt(request.getParameter("id"));
User u = new User();
u.setId(id);
IUserDao dao = DaoFactory.getUserDao();
u=dao.queryByUserId(u);
%>
<body>
<center>
<h2>修改用户信息</h2>
<form action="UserServlet?op=update" method="post">
<table border="0" >
<tr>
<td>编号:</td>
<td>
<input type="text" name="id" readonly="readonly" value="<%=u.getId() %>"/>
</td>
</tr>
<tr>
<td>姓名:</td>
<td>
<input style="color:#7a7a7a" type="text" name="name" value="<%=u.getName()%>"/>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input style="color:#7a7a7a" type="text" name="addr" value="<%=u.getPassword()%>"/>
</td>
</tr>
</table><br>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
</center>
</body>
</html>
user.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.jdbc.dao.DaoFactory"%>
<%@page import="com.jdbc.service.IUserDao"%>
<%@page import="com.jdbc.bean.User" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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%>">
<link rel="stylesheet" type="text/css" href="css/user.css">
<title>用户信息</title>
<script>
function fun(val){
var r=confirm("确认删除吗?");
if(r){
location.href="UserServlet?op=del&id="+val;
}
}
</script>
</head>
<body>
<center>
<div>
<form action="UserServlet?op=select" method="post"
class="search">
<input style="border-radius: 0; width: 328;" name="searchText"
type="text" placeholder="请输入用户编号或姓名"> <input name="查询用户"
type="submit" value="查询用户">
</form>
</div>
<table border="1" style="border:2px solid RGB(73,161,225)">
<tr>
<th>编号:</th>
<th>用户名:</th>
<th>密码:</th>
<th colspan="2" width="200">操作</th>
</tr>
<c:forEach var="User" items="${userList}">
<tr align="center">
<td>${User.id}</td>
<td>${User.name}</td>
<td>${User.password}</td>
<td><a href="updateUser.jsp?id=${User.id}&op=update">修改</a></td>
<td><a href="javascript:void(0);" onclick="fun(${User.id})">删除</a></td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
login.css
@charset "UTF-8";
body{
background: url('../image/login.jpg');
background-repeat:no-repeat;
background-size:100% auto;
}
#login-box{
width: 30%;
height 300px;
margin: 0 auto;
margin-top: 15%;
text-align:center;
background:#00000060;
padding:20px 50px;
}
#login-box h1{
color:#fff;
}
#login-box.form{
margin-top:50px;
}
#login-box .form .item{
margin-top:15px;
color:#fff;
}
#login-box .form .item input{
width:180px;
font-size:18px;
border:0;
border-bottom:2px solid #ffffff;
padding:5px 10px;
background:#ffffff00;
color: #fff;
}
#login-box .form .buttom input{
font-size:18px;
margin-top:15px;
width:180px;
heigh:60px;
}
product.css
@charset "UTF-8";
a{
text-decoration: none;
}
.productfind{
width:300px;
height: 25px;
}
th{
width: 100;
}
user.css
@charset "UTF-8";
a{
text-decoration: none;
}
th{
width: 100px;
}
input{
border:1px solid RGB(99,175,233);
}
register.css
@charset "UTF-8";
body{
background: url('../image/register.jpg');
background-repeat:no-repeat;
background-size:100% auto;
}
#register-box{
width: 30%;
height 300px;
margin: 0 auto;
margin-top: 10%;
text-align:center;
background:#00000060;
padding:20px 50px;
}
#register-box h1{
color:#fff;
}
#register-box.form{
margin-top:50px;
}
#register-box .form .item{
margin-top:15px;
color:white;
}
#register-box .form .item input{
width:180px;
font-size:18px;
border:0;
border-bottom:2px solid #ffffff;
padding:5px 10px;
background:#ffffff00;
color: #fff;
}
#register-box .form .buttom input{
font-size:18px;
margin-top:15px;
width:180px;
heigh:60px;
}
main.css
@charset "UTF-8";
body{
background: url('../image/main.jpg');
background-repeat:no-repeat;
background-size:100% auto;
}
#main-box{
width:70%;
margin-top: 10%;
text-align:center;
background:blue;
margin: 0 auto;
position: relative;
top:3%;
margin-top: 10px;
}
.top{
background: url('../image/main_top.jpg');
background-size:100% 210px;
width: 100%;
height: 210px;
background-repeat: no-repeat;
}
.banner{
background-color:#dddddd;
float:left;
width:100%;
height:41px;
}
.banneruser{
width:200px;
height:22px;
margin-top: 10px;
margin-left: 2px;
}
.left{
width:15%;
height:390px;
float:left;
background-color:#e7e7e7;
}
.leftchoose{
widht:100px;
height:30px;
margin-top: 20px;
margin-right: 1px;
}
ul{
list-style-type: none;
padding:0 37px;
}
a{
text-decoration: none;
color: #000000;
}
.right{
width:85%;
height:390px;
float:right;
background-color:#e7e7e7;
}
.if{
width:95%;
height:350px;
}
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<welcome-file-list>
<welcome-file>login.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>LoginServletservlet-name>
<servlet-class>com.jdbc.servlet.LoginServletservlet-class>
servlet>
<servlet>
<servlet-name>OperatorServletservlet-name>
<servlet-class>com.jdbc.servlet.OperatorServletservlet-class>
servlet>
<servlet>
<servlet-name>UserServletservlet-name>
<servlet-class>com.jdbc.servlet.UserServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LoginServletservlet-name>
<url-pattern>/Loginurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>OperatorServletservlet-name>
<url-pattern>/OperatorServleturl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>UserServletservlet-name>
<url-pattern>/UserServleturl-pattern>
servlet-mapping>
web-app>