读配置文件的登陆demo

jsp部分:

<form method="post" action="http://localhost:8080/Demo/loginAction">
    用户登录
    </div>
    <div style=" width:282px; height:42px; background-image:url(images/logindl.png);">
    <input type="text" id="lname" name="username" style="font:16px;  font-size:13px; width:282px; height:40px; float:right; border:none; background:none;" />
    </div>
    <div  style=" width:282px; height:42px; background-image:url(images/loginmm.png); margin-top:10px;">
    <input type="password"  id="lpassword" name="password" style="font:16px;   font-size:13px; width:282px; height:40px; float:right; border:none; background:none;" />
    </div>
    <div  style="width:282px; height:43px; margin-top:16px;">
    <input type="submit" value="登陆" style=" width:282px; height:43px;"  class="button blue" />
    <br />
    <p style="font-size: 20px; color:red"><c:if test="${not empty sessionScope.msg}">
                              ${sessionScope.msg}
          <c:remove var="msg" scope="session"/>
          </c:if></p>
    </form>




java后台部分:

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        
        
        String path="login.properties";
        String basePath =this.getClass().getResource("/").getPath();
        path=basePath+path;
        
        rprotety.load(path);
        String mylog=rprotety.getProperty(username);
        
        
        
        if(password.equals(mylog)){
            request.getRequestDispatcher("/project/chartTwoTest.jsp").forward(request, response);
        }
        else {
            System.out.println("用户名与密码错误");
            request.getSession().setAttribute("msg","用户名或者登陆密码有误!");
            response.sendRedirect(request.getContextPath()+"/AllLogin/login.jsp");
        }
    }



对properties操作的类:

public class readProperties {
     private Properties prop = null;  
     
     public readProperties() {  
            prop = new Properties();  
        } 
     
     //读取相应的路径
        public void load(String path) {  
            FileInputStream stream = null;  
            try {  
                stream = new FileInputStream(path);  
                prop.load(stream);  
                stream.close();  
                stream = null;  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }
        
     //给出key获取key对应的value值   
        public String getProperty(String key) {  
            return prop.getProperty(key);  
        }
        
        
     //进行对property进行键值对的添加,有则修改,无则添加
       public void addData(String key,String value,String path) throws Exception{
           File file=new File(path);
           if(!file.exists()){
               file.createNewFile();
           }
           InputStream fis=new FileInputStream(file);
           prop.load(fis);
           fis.close();//一定要在修改值之前关闭fis   
           OutputStream fos = new FileOutputStream(path); 
           prop.setProperty(key, value);
           prop.store(fos, path);
       }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值