首页 文章 精选 留言 我的

精选列表

搜索[HTAP架构],共10000篇文章
优秀的个人博客,低调大师

jsp/servlet/javaBean三层架构小示例(1)

在java代码中写文档(html),这是servlet;在文档中写java代码,这是jsp;而如何将两者分开,这就是MVC模式了。虽然是java的一小步,却是程序员们生活的一大步哦。 下面给出一个简单的用户登陆功能,采用jsp/servelt/javaBean来实现MVC模式(即经典的模式2)。 模型层: package com.vitamin.DataAccess; import java.sql.*; public class DBbase { String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; String sConnstr = "jdbc:odbc:myDB"; Connection connect = null; ResultSet rs = null; Statement stmt = null; public DBbase() { try { Class.forName(sDBDriver); } catch(ClassNotFoundException ex) { System.err.println(ex.getMessage()); } } public ResultSet executeQuery(String sql) { try { this.connect = DriverManager.getConnection(sConnstr); this.stmt = this.connect.createStatement(); rs = stmt.executeQuery(sql); } catch(SQLException ex) { System.err.println(ex.getMessage()); } return rs; } public int executeUpdate(String sql) { int result = 0; try { this.connect = DriverManager.getConnection(sConnstr); this.stmt = this.connect.createStatement(); result = stmt.executeUpdate(sql); } catch(SQLException ex) { System.err.println(ex.getMessage()); } return result; } } 控制层: package com.vitamin.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.vitamin.DataAccess.*; import java.sql.*; public class Hello extends HttpServlet { /** * Constructor of the object. */ public Hello() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("GBK"); String name = request.getParameter("name"); String pwd = request.getParameter("password"); DBbase myDb = new DBbase(); ResultSet rs = null; int result = 0; String sql = "select count(*) as count from users where username = '"+name+"' and password = '"+pwd+"'"; try { rs = myDb.executeQuery(sql); if(rs.next()) { result = rs.getInt("count"); } } catch(SQLException ex) { ex.printStackTrace(); } if(result>0) { out.println("合法用户"); } else { out.println("非法用户"); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } } Web表示层: <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <% 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>My JSP 'index.jsp' starting page</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <FORM name="form1" method="POST" action="/servletTest/servlet/Hello"> <P> &nbsp; 用户名:&nbsp; &nbsp; <INPUT type="text" name="name" maxlength="20" /></P> <P> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密码 &nbsp;<INPUT type="password" name="password" maxlength="20" /> </P> <P> &nbsp; </P> <P> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<INPUT type="Submit" name="button4" value="确定" /> &nbsp; &nbsp;<INPUT type="Reset" name="button5" value="重置"></P> <P> &nbsp; </P> <P> &nbsp; </P> </FORM> </body> </html> 看过一篇资料说,servlet能够与多个客户机连接,接受多个客户的输入,并将结果广播到多个客户机上,例如可以作为一个多人参与的游戏服务器,这点倒是让我挺吃惊的,不知道如何能够实现哪?如果是真的,那servlet是如何得知客户的详细信息的?难道可以象socket那样?不解。。。 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2006/06/25/435010.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

我的架构之路 — 配置中心(三)— 动态更新配置

包含两方面的含义:一、变化的配置能够下发到客户端.二、客户端能够方便地处理这些变化的配置。下面会讲普通监听器回调方式和spring 注解到field的方式。 配置下发 1)客户端间隔20秒(可配)去数据库拉取配置,sql传递的参数有配置名称、环境、上一次最大更新时间2)取到配置后,合并更新本地备份。3)通过监听器的方式通知配置变化。 #监听器接口 public interface ConfigUpdateListener { void update(Properties properties); } #启动时添加监听器 public void addConfigUpdateListener(ConfigUpdateListener configUpdateListener) { configUpdateListeners.add(configUpdateListener); } #配置变化,调用监听器进行通知 for (ConfigUpdateListener configUpdateListener : configUpdateListeners) { configUpdateListener.update(changedProperties); } spring 注解方式绑定成员变量 通过@ConfigValue注解来实现变量绑定,当配置变化的时候会同时更新这个变量。这种方式可以用于一起开关切换、降级处理的场景。 @ConfigValue("baseUrl") private String baseUrl; 实现原理:1)从配置中心获取配置。2)通过BeanPostProcessor 遍历每个spring bean,找出所有包含@ConfigValue注解的field,然后设置值为配置中心的属性值,同时把记录field到Map对象,以后更新配置时就不用想办法遍历了。这里有一个严格处理原则,如果配置中心找不到相应key或值赋值时候报异常,终止程序,保证安全性。其实代码对非spring的、static 成员变量也支持,只是需要程序启动时候指定包含这些static变量的类名列表。 public class PropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer implements BeanPostProcessor { private Map<String,List<BeanField>> configMap= new ConcurrentHashMap<String,List<BeanField>>(); @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { ConfigInjectSupport.inject(mergedProperties,bean.getClass(),bean,configMap); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } public static void inject(Properties properties, Class clazz, Object bean, Map<String,List<BeanField>> configMap){ Field[] fields = clazz.getDeclaredFields(); if (null != fields) { for (Field f : fields) { // 如果 bean ==null(非spring),则必须是静态方法 if(bean==null && !Modifier.isStatic(f.getModifiers())) continue; ConfigValue configValue = f.getAnnotation(ConfigValue.class); if (configValue != null) { String key = configValue.value(); f.setAccessible(true); Object value = properties.get(key); if (value != null) { try { f.set(bean, converter.convertIfNecessary(value, f.getType(), f)); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }else{ RuntimeException e= new RuntimeException("can not find config. key="+key+", class="+clazz+", field="+f.getName()); e.printStackTrace(); throw e; } List<BeanField> list = configMap.get(key); if (list == null) { list = new ArrayList<BeanField>(); configMap.put(key, list); } BeanField beanField=new BeanField(); beanField.setBean(bean); beanField.setField(f); list.add(beanField); } } } clazz = clazz.getSuperclass(); } 3)加入监听器,接收变化的配置,更新field值 public static void addConfigUpdateListener(final Map<String,List<BeanField>> configMap) { DefaultConfigClient.addConfigUpdateListener(new ConfigUpdateListener() { @Override public void update(Properties properties) { for (Map.Entry<Object, Object> e : properties.entrySet()) { List<BeanField> beanFields = configMap.get(e.getKey().toString()); if (beanFields != null) { Object value = properties.get(e.getKey()); for (BeanField beanField : beanFields) { try { ConfigValue configValue = beanField.getField().getAnnotation(ConfigValue.class); if(configValue.updatable()) { ConfigInjectSupport.setFieldValue(beanField, value); } } catch (IllegalAccessException ex) { ex.printStackTrace(); } } } } } }); } public static void setFieldValue(BeanField beanField,Object value) throws IllegalAccessException { Field f=beanField.getField(); f.set(beanField.getBean(), ConvertUtils.convert(value,f.getType())); }

优秀的个人博客,低调大师

区块链(Block Chain) Hyperledger Fabric 1.0 技术架构详解

Hyperledger Project 由Linux基金会创办于2015年10月,是一个开源的区块链研发孵化项目,致力于提供可协同开发以区块链为底层的分布式账本。旗下的Fabric项目目标为打造一个提供分布式账本解决方案的平台。 区块链的现状 比特币可以说是区块链的第一个实践项目,而对于区块链的介绍资料却大多“言简意赅”,说到区块链大家相对熟悉的几个词可能是“数据不可篡改”、“公开链”、“分布式数据”、“共识机制”等,但是对于这种技术本身来说悲哀的是大多数参与比特币的用户并不是相信该技术的可靠性,而更多的是因为平台其拥有巨大的用户量和交易金额。在此我想先简单说下我对区块链的理解及其适合的应用场景,让读者更容易明白区块链是什么,能做什么,然后再一一阐述区块链是如何从技术上去解决实际业务中的问题。 业务上所期望解决的问题——信用问题 首先从比

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码