使用Java基于MVC模式开发一个简单商品管理系统

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35206244/article/details/79219574
引言:最近在学习MVC、JDBC部分,完成了一个简单的商品管理系统,整理一下,写下来,
也给各位一个参考。有不正确、不恰当的地方,还望指正!
备注:项目在MyEclipse下运行通过,可完成简单的商品增、删、改、查操作。

------------------------更新源码----------------------------

文章已经粘贴了全部代码,另附上完整项目包。包含数据库脚本和数据库驱动jar包。

百度云: https://pan.baidu.com/s/1qocTdlj_ofKMQobl0oYisQ

密码: 435a 

2019年6月20日 17:40:25


-------------------更新记录--------------------

1、读者反馈无法登录,问题原因是,文章6.1章节,登录页面使用的伪代码,页面属性和java bean不对应,无法登录。

现已修改。

注意:本文基于mysql 5.0开发,jdbc驱动包版本要对应。

2019年6月12日 20:52:26

2、针对读者的反馈,重新修改了源码。

2019年6月20日 17:41:53

环境:

 

  • 机器环境:Windows 7 64bit
  • JDK:1.6
  • 数据库:MySql 5.0(界面为SQLyog v10.2)
  • IDE:MyEclipse 9.0 M1
  • 连接数据库jar包:mysql-connector-java-5.1.7-bin.jar
  • 测试浏览器:Chrome、Sougou浏览器、IE11.0、MyEclipse内置浏览器

 

正文:

 

 

此简易的商品管理系统,包括前端、后台和数据库三部分。前端使用jsp页面(使用div+css

布局),后端使用MVC模式,数据库为简单的两张表,用户表和商品表。

 前台布局包括:

top(首栏)、banner(横幅部分)、left(左侧导航栏)、right(内容部分,使用了frame

框架)。

 系统描述:

用户登录后跳转至商品管理页面(图1-1),banner显示当前用户,点击用户名(超链接)

可进入用户管理页,可对用户进行增(注册用户)、删、改(修改用户名、密码等)操作。

航栏有显示产品和添加产品。显示产品页显示全部商品,操作栏可进行更新、删除操作。

 系统前台效果:

图1-1 

系统演示:

 

                                                                              图1-2

数据库表结构:

 

                                                                               图1-3

      项目文件夹组织结构:

 

 

 

                                                                             图1-4

     后台java代码部分:

1、javabean:

1.1、Product.java

 

package com.zjl.bean;

public class Product {
	private int id;
	private String name;
	private String addr;
	private double price;
	public Product() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Product(int id, String name, String addr, double price) {
		super();
		this.id = id;
		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;
	}
	
	
}


 1.2、User.java

 

 

 

 

package com.zjl.bean;

public class User {
	private int id;
	private String name;
	private String password;
	private String type;
	
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	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 + "]";
	}
	
	
}


2、conn

 

2.1 ConnectDatabase.java

 

package com.zjl.conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectDatabase {
	private static final String DRIVER_STRING="com.mysql.jdbc.Driver";
	private static final String URL_STRING="jdbc:mysql://localhost:3306/dbms";
	private static final String USER_STRING="root";
	private static final String PASS_STRING="111";
	
	public static Connection getConnection(){
		Connection connection=null;
		
		try {
			Class.forName(DRIVER_STRING);
connection=DriverManager.getConnection(URL_STRING, USER_STRING, PASS_STRING);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return connection;
	}
	public void realse(Connection conn,PreparedStatement ps,ResultSet rs){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


3、dao层

 

3.1、DaoFactory.java

 

package com.zjl.dao;

import com.zjl.face.IProductDao;
import com.zjl.face.IUserDao;

public class DaoFactory {
	public static IUserDao getUserDao(){
		return new UserDaoImp();
	}
	public static IProductDao getProductDao(){
		return new ProductImp();
	}
}

 3.2、ProductImp.java

 

 

package com.zjl.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zjl.bean.Product;
import com.zjl.bean.User;
import com.zjl.conn.ConnectDatabase;
import com.zjl.face.IProductDao;


public class ProductImp implements IProductDao {
	PreparedStatement ps = null;
	public boolean insert(Product p) {
		// TODO Auto-generated method stub
String sql ="insert into product(id,name,addr,price) values(?,?,?,?)";
		int n =0;
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		ps.setInt(1,p.getId());
		ps.setString(2, p.getName());
		ps.setString(3, p.getAddr());
		ps.setDouble(4, p.getPrice());
		n=ps.executeUpdate();
		}catch (Exception e) {
			// TODO: handle exception
		e.printStackTrace();
		}
		return n>0;
	}

	public boolean delete(Product p) {
		// TODO Auto-generated method stub
		String sql ="delete from product where id="+p.getId();
		int n=0;
		try{
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		n=ps.executeUpdate();
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return n>0;
	}

	public boolean update(Product p) {
		// TODO Auto-generated method stub
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");
		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) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}

	public Product queryById(Product p) {
		// TODO Auto-generated method stub
		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;
	}

}

 

 

 

 

 

 

3.3、UserImp.java

package com.zjl.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.zjl.bean.User;
import com.zjl.conn.ConnectDatabase;
import com.zjl.face.IUserDao;


public class UserDaoImp implements IUserDao {

	PreparedStatement ps = null;
	public boolean insert(User u) {
		// TODO Auto-generated method stub
		int n=0;
String sql="insert into user(id,name,password,type) values(null,?,?,null)";
		try{
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		ps.setString(1, u.getName());
		ps.setString(2, u.getPassword());
		
		n = ps.executeUpdate();
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return n>0;
	}

	public boolean delete(User u) {
		// TODO Auto-generated method stub
		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) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return n>0;
	}

	public boolean update(User u) {
		// TODO Auto-generated method stub
		return false;
	}

	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;
	}

}

 

4、接口

 

4.1、IProductDao.java

 

package com.zjl.face;

import java.util.List;
import java.util.Map;

import com.zjl.bean.Product;
import com.zjl.bean.User;


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);
}

 

 

 

 

 

4.2、IUserDao.java

 

package com.zjl.face;

import com.zjl.bean.User;

public interface IUserDao {
	public boolean insert(User u);
	public boolean delete(User u);
	public boolean update(User u);
	public User queryById(User u);

}

 

 

 

 

 

5、Servlet

5.1、登录servlet

 

package com.zjl.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.zjl.bean.User;
import com.zjl.dao.DaoFactory;
import com.zjl.face.IUserDao;

public class LoginServlet extends HttpServlet {

	
	public LoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
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");
			
			String name = request.getParameter("userName");
			String password = request.getParameter("userPass");
			
			User user = new User();
			user.setName(name);
			user.setPassword(password);
			
			IUserDao dao = DaoFactory.getUserDao();
			if(dao.queryById(user)!=null){
				//请求转发,可以传递数据
		request.getRequestDispatcher("main.jsp").forward(request, response);
			}else{
				//重定向
				response.sendRedirect("login.jsp");
			}
		
	}

	
	public void init() throws ServletException {
		// Put your code here
	}

}

 

 

 

 

 

5.2、商品管理servlet

 

package com.zjl.servlet;


import java.io.IOException;
import java.io.PrintWriter;
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.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import com.zjl.bean.Product;
import com.zjl.dao.DaoFactory;
import com.zjl.face.IProductDao;

public class OperatorServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public OperatorServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

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");
		
		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);
		}
		
		
	}
	//显示全部商品信息
	private void selectService(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		  IProductDao dao = DaoFactory.getProductDao();
		  Map<String,Object> map = null;

		  try{
		  if(dao.queryProduct(map)!=null){
request.getRequestDispatcher("product.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("main.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}
	//添加商品信息
	private void insertService(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		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.insert(p)){
request.getRequestDispatcher("product.jsp").forward(request, response);
		}else{
			response.sendRedirect("insert.jsp");
		}}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	//删除指定的产品信息
	private void deleteService(HttpServletRequest request,
			HttpServletResponse response) {
		
		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("product.jsp").forward(request, response);
			
		}else{
			response.sendRedirect("product.jsp");
		}
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	//更新商品信息
	private void updateService(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		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("product.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("update.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}

	public void init() throws ServletException {
		// Put your code here
	}

}

 

 

 

 

 

6、前台页面

6.1、用户登录页

 

<%@ 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>
    
  </head>
  
  <body>
    <center>
    	<form action="Login" method="post">
    	用户名:
    	<input type="text" name="name"/><br><br>
    	密  码:
    	<input type="password" name="password"/>
    	<br><br>
    	<input type="submit" value="登录"/>
    	  
    	<input type="reset" value="重置"/> 
    	<a href="regist.jsp">注册</a>
    	</form>
    </center>
  </body>
</html>

 

 

 

 

 

6.2、主页

 

<%@ 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 type="text/css">
    	#box{
    		width:850px;
    		margin:0 auto;
    		
    	}
    	#top{
    	width:850px;
    	height:120px;
    	background-color:RGB(99,175,233);
    	
    	border:2px solid RGB(99,175,233);
    	}
    	#left{
    	widht:160px;	
    	height:498px;
    	float:left;
    	background-color:RGB(99,175,233);
    	text-align: left;
    	border:1px solid RGB(99,175,233);
    	
    	
    	}
    	#banner{
    	float:left;
    		width:850px;
    		height:41px;
    		border:2px solid RGB(99,175,233);
    		
    	}
    	#right{
    	 width:741px;
    	 height:500px;
    	 float:left;
    	     	 
    	}
    	#if{
    		width:744px;
    		height:496px;
    		border:2px solid RGB(99,175,233);
    	
    	}
    	body{
    		widht:100%;
    		height:100%;
    		
    	}
    	a{
    		text-decoration: none;
    		
    	}
    	ul{
    		list-style-type: none;
    		padding:0 20px;
    	}
    	#banneruser{
    		width:160px;
    		height:22px;
    	    margin-top: 9px;
            margin-left: 18px;
    	}
    </style>

  </head>
  
  <body>
  	<div id="box">
    	<div id="top">
    		
    	</div>
    	<div id="banner">
    		
<div id="banneruser">当前用户:<a href="#"><%=request.getParameter("userName") %></a></div>
    	</div>
    	<div id="left">
    	<ul>
    		<li>
    			<a href="OperatorServlet?op=select" target="frame">显示产品</a>
    		</li>
    		<li>
    			<a href="insert.jsp" target="frame">添加产品</a>
    		</li>
    		
    	</ul>    
    	</div>
    	
    	<div id="right">
    		<iframe id="if" name="frame"></iframe>
    	</div>
   </div>
  </body>
</html>

 

 

 

 

 

6.3、商品展示页

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zjl.dao.DaoFactory"%>
<%@page import="com.zjl.face.IProductDao"%>
<%@page import="com.zjl.bean.Product" %>

<%
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="style/bootstrap.css"/>-->
   <script>
   	function fun(val){
   		var r=confirm("确认删除吗?");
   		if(r){
   			location.href="OperatorServlet?op=del&id="+val;
   		}
   	}
   </script>
   <style>
   a{
    		text-decoration: none;
    		
    	}
   </style>
   
  </head>
  
  <body>
  
    <center>
    	<table border="1" style="border:2px solid RGB(73,161,225)">
    		<tr>
    			<th width="100">编号</th>
    			<th width="100">名称</th>
    			<th width="100">产地</th>
    			<th width="100">价格</th>  		
    			<th colspan="2" width="200">操作</th>	
    		</tr>
    		<%
    		IProductDao dao = DaoFactory.getProductDao();
    		 Map<String,Object> map = null;
    		List<Product> list = dao.queryProduct(map);
    		for(Product p:list){
    		%>
    		<tr>
    			<td><%=p.getId() %></td>
    			<td><%=p.getName() %></td>
    			<td><%=p.getAddr() %></td>
    			<td><%=p.getPrice() %></td>
    			<td><a href="update.jsp?id=<%=p.getId()%>&op=update">更新</a></td>
    	<td><a href="javascript:void(0);" οnclick="fun(<%=p.getId() %>)">删除</a></td>
    		</tr>
    		<%} %>
    	</table>
    </center>
  </body>
</html>

 

 

 

 

 

6.4、商品更新页

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zjl.bean.Product"%>
<%@page import="com.zjl.dao.DaoFactory"%>
<%@page import="com.zjl.face.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>
   	 	<input type="submit" value="提交"/>  
   	 	<input type="reset" value="重置"/>
   	 </form>
   	</center>
  </body>
</html>

 

 

 

 

 

6.5、商品添加页

 

<%@ 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="id"/><br>
   			名称:<input type="text" name="name"/><br>
   			产地:<input type="text" name="addr"/><br>
   			价格:<input type="text" name="price"/><br>
   			<input type="submit" value="提交"/>  
   			<input type="reset" value="重置"/>
   		</form>
   	</center>
  </body>
</html>

 

 

 

 

 

7、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">
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.zjl.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>OperatorServlet</servlet-name>
    <servlet-class>com.zjl.servlet.OperatorServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>OperatorServlet</servlet-name>
    <url-pattern>/OperatorServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

文末:

 

时间仓促,系统很简单,主要功能涉及到了数据库的增、删、改、查等操作,Servlet的配置及

使用,MVC思想的应用,DIV+CSS简单布局等。可以给初学者一个简单的参考,也是对自己

学习的一个记录。欢迎各位浏览,不吝赐教!

 

 

 

展开阅读全文

没有更多推荐了,返回首页