1.功能设计
网站管理系统是把一个网站的内容(文字,图片,等等)与网站的组件分离开来,可以将各个页面连接到一起,可以控制页面的显示,最主要的功能(相对于较简陋的后台管理系统而言)即是实现与数据库的交互。作为模板的手机商城有一库四表,其中有是user(用户表)、moblileclassify(产品分类表)、mobileForm(商品表)和orderForm(订单表)这三个表。
(1)用户的增删查
(2)产品分类的增删
(3)产品的增删查
(4)订单的增删查
2.功能实现
首先建立一个后台管理界面的文件Manager.html,界面分为上、左、右三个框面。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <frameset rows="60,*"> <frame src="sm.html"> <frameset cols="200,*"> <frame src="example01.html" name="left"> <frame src="sml.html" name="right"> </frameset> </frameset> </html>效果图如下
左边目录方面根据老师的指示,使用了树形结构菜单。从网上找了个树形菜单的例子界面来直接在里面进行修改。
example01.heml
树形菜单代码部分
<div class="dtree"> <p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p> <script type="text/javascript"> <!-- d = new dTree('d'); d.add(0,-1,'后台操作'); d.add(1,0,'用户管理','sml.html','111','right'); d.add(2,1,'增加用户','inputRegisterMess.jsp','111','right'); d.add(3,1,'用户查询','selectUser.jsp','111','right'); d.add(4,1,'删除用户','deleteUser.jsp','111','right'); d.add(5,0,'产品分类管理','sml.html','','right'); d.add(6,5,'增加分类','addmobileClassify.jsp','111','right'); d.add(7,5,'删除分类','deletemobileClassify.jsp','111','right'); d.add(8,0,'产品管理','example01.html','','right'); d.add(9,8,'增加产品','addmobileForm.jsp','111','right'); d.add(10,8,'产品查询','selectmobileForm.jsp','111','right'); d.add(11,8,'删除产品','deletemobileForm.jsp','111','right'); d.add(12,0,'订单管理','example01.html','','right'); d.add(13,12,'新增订单','addorderForm.jsp','111','right'); d.add(14,12,'订单查询','selectorderForm.jsp','111','right'); d.add(15,12,'删除订单','deleteorderForm.jsp','111','right'); document.write(d); //--> </script> </div>管理用户功能
(1)增加用户
直接调用老师给的源码里的用户注册界面,这里不进行展示。
(2)查询用户
<%@page contentType="text/html;charset=GB2312" %> <%@page import="java.sql.*" %> <html> <body><bgcolor=cyan> <% Connection con; Statement sql; ResultSet rs; try{ Class.forName("com.mysql.cj.jdbc.Driver"); } catch(Exception e){} try{ String uri="jdbc:mysql://localhost:3306/mobileshop?serverTimezone=UTC&characterEncoding=gb2312"; con=DriverManager.getConnection(uri,"root","123456"); sql=con.createStatement(); rs=sql.executeQuery("select logname from user"); out.println("<div align=center backgroud-color=blue>"); out.println("<table border=2>"); while(rs.next()){ out.println("<tr>"); out.println("<td>"+rs.getString(1)+"</td>"); out.println("</tr>"); } out.println("</table>"); out.println("</div>"); con.close(); } catch(SQLException e){ out.println(e); } %>
(3)删除用户
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <html> <body> <div align=center> <form action="deleteUser"method="post"> <br/> 输入要删除的用户名:<input type=text name="username"> <input type=submit name="submit"value="提交"> </form> </div> </body> </html>servlet文件
package myservlet.control; import java.sql.*; import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class deleteUser extends HttpServlet{ public void init(ServletConfig config) throws ServletException { super.init(config); try{ Class.forName("com.mysql.cj.jdbc.Driver"); } catch(Exception e) {} } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { PrintWriter out=response.getWriter(); Connection con; Statement sql; response.setContentType("text/html;charset=GB2312"); response.setCharacterEncoding("gb2312"); String username=request.getParameter("username").trim(); if(username==null) { username=""; } else { out.println("<html><body>"); try { String uri="jdbc:mysql://localhost:3306/mobileshop?serverTimezone=UTC&characterEncoding=gb2312"; con=DriverManager.getConnection(uri,"root","123456"); sql=con.createStatement(); int m=sql.executeUpdate("DELETE FROM user WHERE logname='"+username+"'"); if(m!=0) { out.println("删除成功"); response.sendRedirect("sml.html"); } else { out.println("删除失败,或不存在该用户!"); response.sendRedirect("sml.html"); } out.println("</body></html>"); con.close(); } catch(SQLException e) { out.println(""+e); response.sendRedirect("sml.html"); } } } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } }管理产品分类功能(从这开始只展示实现功能的servlet,JSP页面省略)
(1)增加产品分类
package myservlet.control; import java.sql.*; import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class addmobileClassify extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); try{ Class.forName("com.mysql.cj.jdbc.Driver"); } catch(Exception e) {} } public String handleString(String s) { try{ byte bb[]=s.getBytes("iso-8859-1"); s=new String(bb); } catch(Exception ee){} return s; } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { PrintWriter out=response.getWriter(); Connection con; PreparedStatement sql; response.setContentType("text/html;charset=GB2312"); String id=request.getParameter("id").trim(); String name=request.getParameter("name").trim(); out.println("<html><body>"); if(id==null) { id=""; } if(name==null) { name=""; } int ID=Integer.parseInt(id); out.println("<html><body>"); try { String uri="jdbc:mysql://localhost:3306/mobileshop?serverTimezone=UTC&characterEncoding=gb2312"; con=DriverManager.getConnection(uri,"root","123456"); String str="insert into mobileClassify values(?,?)"; sql=con.prepareStatement(str); sql.setInt(1,ID); sql.setString(2,handleString(name)); int m=sql.executeUpdate(); if(m!=0) { out.println("插入成功"); response.sendRedirect("sml.html"); } else { out.println("插入失败,或已存在!"); response.sendRedirect("sml.html"); } out.println("</body></html>"); con.close(); out.println("</body></html>"); } catch(SQLException e) { out.println(""+e); } } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } }(2)删除分类
package myservlet.control; import java.sql.*; import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class deletemobileClassify extends HttpServlet{ public void init(ServletConfig config) throws ServletException { super.init(config); try{ Class.forName("com.mysql.cj.jdbc.Driver"); } catch(Exception e) {} } public String handleString(String s) { try{ byte bb[]=s.getBytes("iso-8859-1"); s=new String(bb); } catch(Exception ee){} return s; } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { PrintWriter out=response.getWriter(); Connection con; Statement sql; StringBuffer mess=new StringBuffer(); response.setContentType("text/html;charset=GB2312"); String name=request.getParameter("radio"); String message=request.getParameter("message"); message=handleString(message); if(message==null) { message=""; } if(name.equals("id")) { int Message=Integer.parseInt(message); mess.append("DELETE FROM mobileClassify WHERE id='"+Message+"'"); } else if(name.equals("name")) { mess.append("DELETE FROM mobileClassify WHERE name='"+message+"'"); } out.println("<html><body>"); try { String uri="jdbc:mysql://localhost:3306/mobileshop?serverTimezone=UTC&characterEncoding=gb2312"; con=DriverManager.getConnection(uri,"root","123456"); sql=con.createStatement(); int m=sql.executeUpdate(mess.toString()); if(m!=0) { out.println("删除成功"); response.sendRedirect("sml.html"); } else { out.println("删除失败,或不存在该用户!"); } out.println("</body></html>"); con.close(); } catch(SQLException e) { out.println(""+e); } } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } }产品管理
(1)增加产品
package myservlet.control; import java.sql.*; import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class deletemobileClassify extends HttpServlet{ public void init(ServletConfig config) throws ServletException { super.init(config); try{ Class.forName("com.mysql.cj.jdbc.Driver"); } catch(Exception e) {} } public String handleString(String s) { try{ byte bb[]=s.getBytes("iso-8859-1"); s=new String(bb); } catch(Exception ee){} return s; } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { PrintWriter out=response.getWriter(); Connection con; Statement sql; StringBuffer mess=new StringBuffer(); response.setContentType("text/html;charset=GB2312"); String name=request.getParameter("radio"); String message=request.getParameter("message"); message=handleString(message); if(message==null) { message=""; } if(name.equals("id")) { int Message=Integer.parseInt(message); mess.append("DELETE FROM mobileClassify WHERE id='"+Message+"'"); } else if(name.equals("name")) { mess.append("DELETE FROM mobileClassify WHERE name='"+message+"'"); } out.println("<html><body>"); try { String uri="jdbc:mysql://localhost:3306/mobileshop?serverTimezone=UTC&characterEncoding=gb2312"; con=DriverManager.getConnection(uri,"root","123456"); sql=con.createStatement(); int m=sql.executeUpdate(mess.toString()); if(m!=0) { out.println("删除成功"); response.sendRedirect("sml.html"); } else { out.println("删除失败,或不存在该用户!"); } out.println("</body></html>"); con.close(); } catch(SQLException e) { out.println(""+e); } } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } }(2)删除产品
package myservlet.control; import java.sql.*; import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class deletemobileForm extends HttpServlet{ public void init(ServletConfig config) throws ServletException { super.init(config); try{ Class.forName("com.mysql.cj.jdbc.Driver"); } catch(Exception e) {} } public String handleString(String s) { try{ byte bb[]=s.getBytes("iso-8859-1"); s=new String(bb); } catch(Exception ee){} return s; } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { PrintWriter out=response.getWriter(); Connection con; Statement sql; String mess=""; response.setContentType("text/html;charset=GB2312"); request.setCharacterEncoding("gb2312"); String name=request.getParameter("radio"); String message=request.getParameter("message"); if(message==null) { message=""; } if(name.equals("id")) { mess="DELETE FROM mobileForm WHERE mobile_version='"+message+"'"; } else if(name.equals("name")) { mess="DELETE FROM mobileForm WHERE mobile_name='"+message+"'"; } out.println("<html><body>"); try { String uri="jdbc:mysql://localhost:3306/mobileshop?serverTimezone=UTC&characterEncoding=gb2312"; con=DriverManager.getConnection(uri,"root","123456"); sql=con.createStatement(); int m=sql.executeUpdate(handleString(mess)); if(m!=0) { out.println("删除成功"); response.sendRedirect("sml.html"); } else { out.println("删除失败,或不存在该手机信息!"); } out.println("</body></html>"); con.close(); } catch(SQLException e) { out.println(""+e); } } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } }(3)产品查询
<%@page contentType="text/html;charset=GB2312" %> <%@page import="java.sql.*" %> <html> <body><bgcolor=cyan> <% Connection con; Statement sql; ResultSet rs; try{ Class.forName("com.mysql.cj.jdbc.Driver"); } catch(Exception e){} try{ String uri="jdbc:mysql://localhost:3306/mobileshop?serverTimezone=UTC&characterEncoding=gb2312"; con=DriverManager.getConnection(uri,"root","123456"); sql=con.createStatement(); rs=sql.executeQuery("select * from mobileForm"); out.println("<div align=center backgroud-color=blue>"); out.println("<table border=2>"); while(rs.next()){ out.println("<tr>"); out.println("<td>"+rs.getString(1)+"</td>"); out.println("<td>"+rs.getString(2)+"</td>"); out.println("<td>"+rs.getString(3)+"</td>"); out.println("<td>"+rs.getFloat(4)+"</td>"); out.println("<td>"+rs.getString(5)+"</td>"); out.println("<td>"+rs.getString(6)+"</td>"); out.println("<td>"+rs.getInt(7)+"</td>"); out.println("</tr>"); } out.println("</table>"); out.println("</div>"); con.close(); } catch(SQLException e){ out.println(e); } %>订单管理
实现的代码与上面的大同小异,这里就不进行展示了。