java web开发中常用的协议的使用和java-web 常见的缓冲技术
一、DNS协议
作用将域名解析为IP 类似于我们只需要知道中央一台,中央二台,而不需要知道它的频率,方便记忆。
java dns 域名解析协议实现
1 域名解析,将域名可转换为ip地址
InetAddress也可以通过使用getAddress()来获得IP地址,但是它的返回值是一个4个字节的数组。
因此尽管getAddress()在获得IP方面是有用的,但却不适于用来输出。
package dns;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class NsLookup { public static void main(String[] args){
InetAddress address=null;
try {
address = InetAddress.getByName(args[0]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println(args[0]+": "+address.getHostAddress()); //args[0]是执行程序时写的参数,
InetAddress localhost=null;
try {
localhost = InetAddress.getLocalHost(); //本地地址
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("localhost:ip address "+localhost.getHostAddress());
System.out.println("localhost:主机名: "+localhost.getHostName());
/* * 在开始使用RS232端口通讯之前,我们想知道系统有哪些端口是可用的,以下代码列出系统中所有可用的RS232端口
*/
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
}
}
执行时命令行参数如果输入:www.sina.com
执行结果如下:
www.sina.com: 121.194.0.209
localhost:ip address 59.64.158.214
localhost:主机名: bupt
COM1
COM2
还有一个域名可能对应不止一个ip地址,一下程序时列举出sina域名下的所有ip
package dns;
import java.net.InetAddress;
import java.net.UnknownHostException;
//有时一个域名会包含不止一个IP地址,比如微软的Web服务器,这是为了保持负载平衡。
//InetAddress提供了一种可以得到一个域名的所有IP地址的方法
public class NsLookup2 {
static public void main(String[] args) {
try {
String name = args[0];
InetAddress[] addresses = InetAddress.getAllByName(name);
for (int i = 0; i < addresses.length; i++) {
System.out.println(name + "[" + i + "]: "+ addresses[i].getHostAddress());
}
} catch (UnknownHostException uhe) {
System.err.println("Unable to find: " + args[0]);
} } }
执行结果: www.sina.com[0]: 121.194.0.208
www.sina.com[1]: 121.194.0.209
www.sina.com[2]: 121.194.0.210
www.sina.com[3]: 121.194.0.203
www.sina.com[4]: 121.194.0.205
www.sina.com[5]: 121.194.0.206
二、TCP/IP协议
IP 是用来查找地址,对应网际互连层,
IP 负责计算机之间的通信。
IP 负责在因特网上发送和接收数据包。
TCP用来规范传输规则的对应传输层。tcp/ip只是一套规则,并不能具体工作,就像程序中的接口,socket是这套协议的具体实现。
TCP 用于从应用程序到网络的数据传输控制。
TCP 负责在数据传送之前将它们分割为 IP 包,然后在它们到达的时候将它们重组。
三、HTTP协议
http是应用层的协议,tcp/ip接受到数据之后通过htp协议来解析。
HTTP 负责 web 服务器与 web 浏览器之间的通信。
HTTP 用于从 web 客户端(浏览器)向 web 服务器发送请求,并从 web 服务器向 web 客户端返回内容(网页)。
http协议中的报文结构很重要。
1:request message 首行,头部和主体 首行包括请求类型(get,head,post,put,delete),urL 和http版本
2:response 首行包括状态行,状态码(1XX, 2xx成功状态吗,3xx重定向状态吗,4xx:客户端状态吗,为请求到资源,5xx服务器端错误码,500内部错误),简短原因,http版本
四、servlet
是j2ee标准的一部分,是java web 开发的标准,
servlet 是对接收到的数据进行处理并生成结果。
五、SMTP - 简易邮件传输协议(Simple Mail Transfer Protocol)
SMTP 用于电子邮件的传输。
六、IMAP - 因特网消息访问协议(Internet Message Access Protocol)
IMAP 用于存储和取回电子邮件。
七、FTP - 文件传输协议(File Transfer Protocol)
FTP 负责计算机之间的文件传输。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
开发中常使用缓存中间件:redis、memcahed、ehcache;
* 缓存:其实就是内存中的一块空间.可以使用缓存将数据源中的数据拿到,存入到内存中.后期获得数据的话 从缓存中进行获得.
* 常见欢送有以下几种
1.EHCache :是Hibernate常使用的二级缓存的插件.
参考https://elim.iteye.com/blog/2123030
a、引入依赖
<!-- Spring Boot 缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache 坐标 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
b、开启基于注解的缓存 @EnableCaching

c、创建Ehcache的配置文件
文件名:ehcache.xml
位置:src/main/resources/ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="java.io.tmpdir" />
<!--defaultCache:echcache 的默认缓存策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap" />
</defaultCache> <!-- 自定义缓存策略 -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
d、创建application.properties文件(mysql与springDateJpa相关配置)
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo01?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.cache.ehcache.config=ehcache.xml

MySql在高版本需要指明是否进行SSL连接,这里因为自己的数据库安装的是mysql8.0所以需要加上这个东西,如果用的是mysql5点几版本的朋友可以自行忽略…
SSL协议,当前版本为3.1(SSL3.1就是TLS1.0)。它已被广泛地用于Web浏览器与服务器之间的身份认证和加密数据传输.它位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持
package com.test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.App;
import com.entity.Users;
import com.service.UsersService; /**
* UsersService测试
*
* @author Administrator
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class UsersServiceTest {
@Autowired
private UsersService usersService; @Test
public void addUser() {
Users users=new Users();
users.setId(1);
users.setAddress("苏站路");
users.setAge(18);
users.setName("张三");
usersService.saveUsers(users);
System.out.println("执行完毕");
}
@Test
public void testFindUserById() {
//第一次查询
System.out.println(this.usersService.findUserById(1));
//第二次查询
System.out.println(this.usersService.findUserById(1));
}
}
测试结果

第二次测试‘’
这里我们将之前在 UserServiceImpl里面的 @Cacheable(value = “users”)取消注释
再次启动项目测试
第三次测试
修改UsersServiceImpl里面findUserByPage的方法指定为pageable.pageSize,默认的是pageable
修改测试类
新增方法testFindUserByPage
@Test
public void testFindUserByPage() {
Pageable pageable = new PageRequest(0, 2);
// 第一次查询
System.out.println(this.usersService.findUserByPage(pageable).getTotalElements()); // 第二次查询
System.out.println(this.usersService.findUserByPage(pageable).getTotalElements());
测试结果

第四次测试(清除缓存:@CacheEvict)
首先在UsersServiceTest中添加测试方法testFindAll,代码如下
@Test
public void testFindAll() {
//第一次查询
System.out.println(this.usersService.findUserAll().size());
//添加用户
Users users = new Users();
users.setId(4);
users.setAddress("苏站路");
users.setAge(18);
users.setName("张三4");
usersService.saveUsers(users);
//第二次查询
System.out.println(this.usersService.findUserAll().size());
}
测试结果一:

由于缓存机制的存在,所以数据库的元素改变了,但是我们用的却还是之前缓存的内容,很显然这不合适。如何解决呢,我们修改UsersServiceImpl,在saveUsers上面填上注解@CacheEvict(value=“users”,allEntries=true)
@Override
//@CacheEvict(value="users",allEntries=true) 清除缓存中以users缓存策略缓存的对象
@CacheEvict(value="users",allEntries=true)
public void saveUsers(Users users) {
this.usersRepository.save(users);
}
再次测试,结果如下:

2.Memcache :
3.Redis :
java web开发中常用的协议的使用和java-web 常见的缓冲技术的更多相关文章
- 依赖注入及AOP简述(十)——Web开发中常用Scope简介 .
1.2. Web开发中常用Scope简介 这里主要介绍基于Servlet的Web开发中常用的Scope. l 第一个比较常用的就是Application级Scope,通常我们会将一 ...
- Python Web开发中的WSGI协议简介
在Python Web开发中,我们一般使用Flask.Django等web框架来开发应用程序,生产环境中将应用部署到Apache.Nginx等web服务器时,还需要uWSGI或者Gunicorn.一个 ...
- WEB开发中常用的正则表达式
在计算机科学中,正则表达式用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在WEB开发中,正则表达式通常用来检测.查找替换某些符合规则的字符串,如检测用户输入E-mai格式是否正确,采集符 ...
- WEB开发中常用的正则表达式集合
在计算机科学中,正则表达式用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在WEB开发中,正则表达式通常用来检测.查找替换某些符合规则的字符串,如检测用户输入E-mai格式是否正确,采集符 ...
- Python Web开发中,WSGI协议的作用和实现原理详解
首先理解下面三个概念: WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server ...
- web开发中常用的技术体系
HTML html(HyperText Markup Language)超文本标记语言"超文本"就是指页面内可以包含图片.链接.程序等非文字元素. 超文本标记语言的结构包括&q ...
- 【java】开发中常用字符串方法
java字符串的功能可以说非常强大, 它的每一种方法也都很有用. java字符串中常用的有两种字符串类, 分别是String类和StringBuffer类. Sting类 String类的对象是不可变 ...
- Web开发中常用的状态码
在HtttpServletResponse类中有关于状态码的描述. static int SC_ACCEPTED Status code (202) indicating that a request ...
- Web开发中常用的定位布局position
定位布局就是为开发提供了更好的布局方式,可以根据需求给相应的模块设定相应位置,从而使界面更佳丰富,代码更佳完美. position是CSS中非常重要的一个属性,通过position属性,我们可以让元素 ...
随机推荐
- css画三角的原理
当我们设置一个div其width与height为100px,并且设置其四边框的宽度为100px,且分别设置其颜色后,我们可以看到如下的一张图片 此时如果设置这个div的height为0的话,其他不变, ...
- node npm 总结
是nodejs的软件包管理器,用于node插件管理 npm install <name> [-g] [--save -dev] name:安装模块的名称 -g:全局安装 --save:将保 ...
- DDK编写64位驱动时加入x64汇编的方法
上篇讲了如何在编写x64应用程序时加入x64汇编,这里来说说如何在编写x64驱动时加入x64汇编. 一.在asm文件中单独编写功能函数 比如要实现一个64位的加法函数,原型如下: ULONG64 my ...
- web前端安全机制问题全解析
摘要 web前端安全方面技术含有的东西较多,这里就来理一理web安全方面所涉及的一些问题 目录[-] 摘要 web前端安全方面技术含有的东西较多,这里就来理一理web安全方面所涉及的一些问题 web安 ...
- Event Handling Guide for iOS--(三)---Event Delivery: The Responder Chain
Event Delivery: The Responder Chain 事件传递:响应链 When you design your app, it’s likely that you want to ...
- NC文件的处理【netcdf】
NC是气象领域数据的标准格式之一. 能够更好的存储格点数据. 下面为测试NC文件的读写. git:https://git.oschina.net/ipnunu/nctest pom.xml <p ...
- PTA 2-1 列出连通集【DFS+BFS基础】
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...
- 收集一些Unity插件
MCS Male 系列,人形角色插件,表情+体型 Mecanim Control Mecanim Control is a coding tool made that allow for a wide ...
- Ironpython 安装numpy包
http://pytools.codeplex.com/wikipage?title=NumPy%20and%20SciPy%20for%20.Net https://www.enthought.co ...
- pycharm命令行快捷启动
打开 本用户目录下的.bashrc文件 vim .bashrc 在末尾添加一行 alias pycharm="the-path-to-pycharm.sh" 最后保存退出 然后更新 ...