java-web实战简单演练

这是一篇基于java-web开发的一个简单药品管理系统
首先实验内容如下:
第一部分:药品管理模块:
中药(属性至少包含:货号、名称、生产日期、到期时间、进价、售价、数量、生产商)
西药(属性至少包含:货号、名称、生产日期、到期时间、进价、售价、数量、计量单位、生产商)
要求能够实现添加药品,修改药品,删除药品,查找药品(按名称缩写),显示药品的功能。
第二部分:用户注册模块:
管理员与药剂师对商品的权限不同。
第三部分:模块整合
其他功能模块可自行完善。

我们采用的是MVC设计模式来开发
MVC是一个设计模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器。它们各自处理自己的任务:
(控制器Controller)- 负责转发请求,对请求进行处理。
(视图View) - 界面设计人员进行图形界面设计。
(模型Model) - 程序员编写程序应有的功能(实现算法等等)、数据库专家进行数据管理和数据库设计(可以实现具体的功能)。java-web实战简单演练_第1张图片
接下来是我用到的开发工具
window 10系统 ; MyEclipse; TomCat 7.0;Mysql5.7;Sqlyog(Mysql)图形化处理界面
成品图:(我用的是一些很基本的H5元素修饰美化)
java-web实战简单演练_第2张图片

框架图:
java-web实战简单演练_第3张图片
好,介绍到这里,我们接下来就得跟着实验要求来做啦,要求不是很难,相信看完了跟着来肯定会做。
首先新建一个药品类,里面封装着一些变量,注意这里声明的变量应该与数据库中的字段相同

package bean;

public class Drugs {
        private int id;             //声明id
        private int drugid;     //声明货号
        private String name;    //声明药名
        private String productiondate;  //声明生产日期    
        private String expirationdate;  //声明到期日期
        private Double purchaseprice;   //声明进价
        private Double sellingprice;    //声明售价
        private int quantity;           //声明药品数量
        private String measuringunit;   //声明计量单位
        private String producer;        //声明生产商
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public int getDrugid() {
            return drugid;
        }
        public void setDrugid(int drugid) {
            this.drugid = drugid;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getProductiondate() {
            return productiondate;
        }
        public void setProductiondate(String productiondate) {
            this.productiondate = productiondate;
        }
        public String getExpirationdate() {
            return expirationdate;
        }
        public void setExpirationdate(String expirationdate) {
            this.expirationdate = expirationdate;
        }
        public Double getPurchaseprice() {
            return purchaseprice;
        }
        public void setPurchaseprice(Double purchaseprice) {
            this.purchaseprice = purchaseprice;
        }
        public Double getSellingprice() {
            return sellingprice;
        }
        public void setSellingprice(Double sellingprice) {
            this.sellingprice = sellingprice;
        }
        public int getQuantity() {
            return quantity;
        }
        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }
        public String getMeasuringunit() {
            return measuringunit;
        }
        public void setMeasuringunit(String measuringunit) {
            this.measuringunit = measuringunit;
        }
        public String getProducer() {
            return producer;
        }
        public void setProducer(String producer) {
            this.producer = producer;
        }


}

好了,javabean建好后,我们要开始写jsp界面,也就是显示层
在WebRoot根目录下新建login.jsp文件
好了,不给代码我知道说了你们不动手实践也是不懂,希望你们不止是ctrl c + ctrl v

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  

  
<html>  
  <head>  
    <base href="<%=basePath%>">            
    <title>药品管理系统title>
    <style type="text/css">
            /* SELECT W/IMAGE */
            /*SELECT W/DOWN-ARROW*/
            select#selectPointOfInterest
            {
               width                    : 100pt;
               height                   : 20pt;
               line-height              : 20pt;
               padding-right            : 10pt;
               text-indent              : 4pt;
               text-align               : left;
               vertical-align           : middle;
               border                   : 1px solid #94c1e7;
               -moz-border-radius       : 6px;
               -webkit-border-radius    : 6px;
               border-radius            : 6px;
               -webkit-appearance       : none;
               -moz-appearance          : none;
               appearance               : none;
               font-family              : SimHei;
               font-size                : 14pt;
               font-weight              : 500;
               color                    : RGBA(102,102,102,0.7);
               cursor                   : pointer;
               outline                  : none;
            }


            /*LABEL FOR SELECT*/
            label#lblSelect{ position: relative; display: inline-block;}
            /*DOWNWARD ARROW (25bc)*/
            label#lblSelect::after
            {
                content                 : "\25bc";
                position                : absolute;
                top                     : 0;
                right                   : 0;
                bottom                  : 0;
                width                   : 20pt;
                line-height             : 20pt;
                vertical-align          : middle;
                text-align              : center;
                background              : #FFA07A;
                color                   : #FFE4C4;
               -moz-border-radius       : 0 6px 6px 0;
               -webkit-border-radius    : 0 6px 6px 0;
                border-radius           : 0 6px 6px 0;
                pointer-events          : none;
            }
    style>       
    <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">  
      
head>  
  <body bgcolor=#FFE4C4> 
    <center>  
    <br><br><br><br><br><br><br><br><br>
    <font face="楷体" size="8" color="#CD5C5C" >欢迎进入药品管理系统登录界面 O(∩_∩)Ofont><br><br>
    <%    
    String flag = request.getParameter("errNo");    
    try{  
         if(flag!=null)  
            out.println("楷体\" size=\"4\" color=\"#FF4500\" >用户名不存在或密码或权限值错误,请重新输入。");
    }catch(Exception e){  
        e.printStackTrace();  
    }  
   %> <br> <br> 
    <form action = "loginCh.jsp" method="post">  
      <table width="400" height = "240" border="5" bordercolor="#FFA07A">   
        <tr>   
          <th><font face="楷体" size="5" color="#CD5C5C" >账  户:font>th>  
          <td><input type="text" name="name"  value = "请输入用户名" style="color:FFA07A; background-color:white;" maxlength = "16" onfocus = "if(this.value == '请输入用户名') this.value =''">  
          
          
          楷体" size="5" color="#CD5C5C" >密  码:font>th>  
          <td><input type="password" name="password" style="color:FFA07A; background-color:white;" maxlength = "20">td>  
        tr> 
        <tr>  
          <th><font face="楷体" size="5" color="#CD5C5C" >权  限:font>th>  
          <td><input type="text" name="quanxian" style="color:FFA07A; background-color:white;" maxlength = "20">td>  
        tr>  
        <tr>  
          <td colspan = "2" align = "center">  
            <button type="submit" style="background: #FFFACD;border-radius:10px;padding:10px">
            <img src="images/l3.png" alt="" width="15" ><font face="楷体" size="5" color="#CD5C5C" >登陆:font>button>
            <button type="reset" style="background: #FFFACD;border-radius:10px;padding:10px">
            <img src="images/jg.png" alt="" width="15" ><font face="楷体" size="5" color="#CD5C5C" >重置:font>button><br><br>        
            <label id="lblSelect">
            <select onchange="window.location=this.value;" id="selectPointOfInterest" title="Select points of interest nearby">                
                <option value="login.jsp">普通用户option>
                <option value="login1.jsp">管理员option>
                <option value="login2.jsp">超级管理员option>                 
            select>
        label>
           td>  
        tr>
         <tr style="height: 10px;background:CD5C5C" align="center"><th colspan="5"><font face="楷体" size="4" color="#CD5C5C" ><a href="regist.jsp">还没有账号?点击注册a>font>tr>  
      table>  
    form>  
  center>  
  body>  
html>  

java-web实战简单演练_第4张图片
好咯,登陆界面有了,是不是要有登陆验证呢?
那必须的,接下来我们可以用servlet,也可以用jsp来实现登陆验证,那么这里涉及到了几个方面,首先要连接数据库,可以用jdbc也可以使用JNDI连接池连接数据库,我这里使用的是JNDI,通过login界面的action跳转到登陆验证界面,然后通过用户输入账号和密码返回mysql数据库查询是否有这一账号和密码,以及是否符合,相同则通过,不同则提示erro,我这里是在jsp实现的逻辑判断
好了,话不多说,新建loginCh.jsp界面:

<%@ page language="java" import="java.util.*,java.sql.*,java.net.*" pageEncoding="utf-8"%>  
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.Context"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.naming.NamingException"%>
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
  
<html>  
  <head>  
    <base href="<%=basePath%>">  

    <title>登陆成功title>  

    <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">  
      

  head>  
  <body>  
    <%      //接收用户名和密码    
            String username = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");    
            String password = request.getParameter("password"); 
            String quanxian = request.getParameter("quanxian");             
            //接收从login界面输入的数据并且声明为user和password对象
            Context c = new InitialContext();
            DataSource ds = (DataSource)c.lookup("java:comp/env/jdbc/drug");
            Connection conn = ds.getConnection(); //取得连接           
            //此处采用预编译,而不用Statement,可以高效的多此执行查询语句
            PreparedStatement pStmt = conn.prepareStatement("select * from admin where username = '" + username + "' and password = '" + password + "' and jurisdiction = '" + quanxian + "'");  
              ResultSet rs = pStmt.executeQuery();  
              //重定向判断跳转到哪一界面
                if(rs.next()){  
                session =request.getSession();
                session.setAttribute("username", username);
                session.setAttribute("quanxian", quanxian);
                    response.sendRedirect("Yesskip.jsp?username="+URLEncoder.encode(username)); //解决乱码   

                }else{  
                    response.sendRedirect("login.jsp?errNo");//密码不对返回到登陆    
                }  
     rs.close();  
     pStmt.close();  
     conn.close();  
     %>  
  body>  
html>  

好了,登陆验证也实现了,那么用户是不是就可以登陆了,那么到这里时候,我想到了一个问题,如果不登陆,直接使用url路径跳转的话,可不可以呢?我一试才发现原来也可以跳转,那这个系统毫无安全性可言了,写个登陆也只是一叶障目,于是这里我们要用到一个servlet元素filter来实现过滤器,实现登陆验证,对登陆的用户放行,对于未登陆想直接进来的小调皮则直接禁止跳转并且跳转到登陆界面。
好,那接下来就要写过滤器了,新建一个servlet包,一个Loginfiler类,上面我以及给出了登陆验证代码,但是这里我们要注意,我们在实现过滤器的时候要理解过滤器的原理,这里就不细细讲了,自行找度娘
注意:首先我们要在登陆验证里面加入一个session值,用于接收用户登陆状态信息,如果登陆成功,则new一个session值,保存用户的用户名,然后放行,对于未登陆的则session值为空,因此禁止登陆,跳转到登陆界面。

java-web实战简单演练_第5张图片

package login;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Loginfilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        //response.setHeader("pragma", "no-cache");  
        //response.setHeader("cache-control", "no-cache");  
        //response.setIntHeader("expires", 0);  
        // 获得在下面代码中要用的request,response,session对象
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        servletResponse.setHeader("Pragma", "No-cache");//禁止缓存
        servletResponse.setHeader("Cache-Control","no-cache");
        servletResponse.setHeader("Expires", "0");
        HttpSession session = servletRequest.getSession();
        // 获得用户请求的URI
        String path = servletRequest.getRequestURI();
        // System.out.println(path);
        // 从session里用户号信息
        String username = (String) session.getAttribute("username");
        // 登陆页面无需过滤
        if (path.indexOf("/login.jsp") > -1
                || path.indexOf("/loginCh.jsp") > -1
                || path.indexOf("/login1.jsp") > -1
                || path.indexOf("/regist.jsp") > -1
                || path.indexOf("images/") > -1
                || path.indexOf("/checkRegister.jsp") > -1
                || path.indexOf("/login2.jsp") > -1
                || path.indexOf("/loginroot.jsp") > -1
                || path.indexOf("/LoCheck.jsp") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        // 判断如果没有取到游客信息,就跳转到登陆页面
        if (username == null || "".equals(username)) {
            // 跳转到登陆页面
            System.out.println("111111");

            servletResponse.sendRedirect("login.jsp");
        } else {
            // 已经登陆,继续此次请求
            chain.doFilter(request, response);
        }
    }

    public void destroy() {
        // TODO Auto-generated method stub
    }
}

过滤代码不难,实现起来也很方便,但我们要注意几点,首先我们不能过滤掉登陆界面和登陆验证,不然陷入死循环,一直在登陆界面过滤不走,
上面代码有介绍,取消过滤的代码,用path.indexof
servlet代码实现后,我们还要配置web.xml,如下:
java-web实战简单演练_第6张图片
这是什么意思呢,就是当我们访问登陆界面时,过滤器开始判断是否登陆成功由
login找到 然后由filter-class 跳转到login.Loginfilter这个类找到Loginfilter这个类,再由这个类的逻辑根据session进行判断


好了,过滤器实现后,就要进行逻辑块实现了,对于药品的增删改查和一些功能的完善,这里我就不讲了,人有点懒,最近又有点小忙,我把整个工程和数据库文件都打包上传到这里了,有兴趣的老铁们可以下载
http://download.csdn.net/download/coldfire_cx/10188377

你可能感兴趣的:(java,web)