1、需要下载unzip
地址:http://gnuwin32.sourceforge.net/packages/unzip.htm
下载exe版本
2、安装后将bin目录下的unzip.exe文件放在C:\Windows目录下
3、命令行执行
unzip test.zip
就能解压了
1、需要下载unzip
地址:http://gnuwin32.sourceforge.net/packages/unzip.htm
下载exe版本
2、安装后将bin目录下的unzip.exe文件放在C:\Windows目录下
3、命令行执行
unzip test.zip
就能解压了
j2ee:Java to Platform, Enterprise Edition (是由Sun、IBM等厂商所主导,协同许多厂商共同参与所制定出的规范,以企业及企业间之运算为导向的JAVA平台环境)
简单点说j2ee就是为企业开发服务的一套框架机制。
至于j2ee怎么读?
J two EE
有的发音不标准的也读成“贼吐衣衣”,😅
主要的报错信息:
No result defined for action com.tyibs.action.shop.MemberAction and result input
首先是HTML页面:
1 2 3 4 5 6 7 |
<form id="uploadAttachmentForm" action="member!ufile.action" method="post" enctype="multipart/form-data"> <input type="hidden" name="id" value="'+inspectZJWorkId+'" /> <p>提示:已经上传过的附件可以点击相应连接下载或查看,没有上传的可以点击按钮上传。</p> <p>'+wbhtStr+'上传维保合同:<input type="file" name="wbhtPath" onchange="" /></p> <p>'+zgzsStr+'上传安全管理员资格证书:<input type="file" name="zgzsPath" onchange="" /></p> <p>'+bgStr+'上传限速器检验报告:<input type="file" name="bgPath" onchange="" /></p> </form> |
然后是action页面
1 2 3 4 5 6 7 8 9 |
private File wbhtPath; //这个是对应的上传的文件 private String wbhtPathFileName;// 这个是上传的文件名,注意它们的命名规则 private String wbhtPathContentType;// 这个是上传的文件属性,也要注意规则,excel文件例如:application/vnd.ms-excel private File zgzsPath; private String zgzsPathFileName; private String zgzsPathContentType; private File bgPath; private String bgPathFileName; private String bgPathContentType; |
具体的处理方法:
1 |
ps:String formatName= this.getWbhtPathFileName().substring(this.getWbhtPathFileName().lastIndexOf(".")+1);// 获取文件后缀的方法 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * 上传附件 * @param servletContext * @param basePath 上传路径 * @param localFile 本地文件 * @param newfilename 保存的新文件名 * @param formatName 文件后缀 * @return 保存好的文件路径 */ public static String copyFile(ServletContext servletContext,String basePath, File localFile, String newfilename, String formatName) { if (localFile == null) { return null; } String destImagePath = basePath + newfilename + "." + formatName; File destImageFile = new File(servletContext.getRealPath(destImagePath)); File destImageParentFile = destImageFile.getParentFile(); if (!destImageParentFile.isDirectory()) { destImageParentFile.mkdirs(); } try { FileUtils.copyFile(localFile, destImageFile); } catch (IOException e) { e.printStackTrace(); } return destImagePath; } |
在struts.xml里配置相应的文件上传参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<interceptors> <interceptor name="securityInterceptor" class="com.tyibs.interceptor.SecurityInterceptor" /> <interceptor name="trimInterceptor" class="com.tyibs.interceptor.TrimInterceptor" /> <interceptor name="memberVerifyInterceptor" class="com.tyibs.interceptor.MemberVerifyInterceptor" /> <interceptor-stack name="shopStack"> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-ms-bmp,image/png,image/x-png,application/pdf,application/msword,application/excel,application/vnd.ms-excel,text/xml,text/html,application/vnd.ms-excel</param> <param name="maximumSize">10485760</param> <!-- 10M 10*1024*1024 --> </interceptor-ref> <interceptor-ref name="securityInterceptor" /> <interceptor-ref name="trimInterceptor" /> <interceptor-ref name="baseStack" /> </interceptor-stack> </interceptors> |
Servlet的生命周期分为三部分
初始化:Web容器加载servlet,调用init()方法进行初始化。
处理请求:当请求到达时,运行其service()方法。Service()会根据表单的请求类型调用相对doGet()方法或者是doPost()方法。
销毁:当服务器关闭时,调用distroy()方法销毁servlet。
get一般用于从服务器上获取数据,post一般用于向服务器传送数据。
get的请求参数是拼接在url后面,可以在浏览器地址栏看见,post是放在http包的包体中。
get方法提交的数据只能是文本,大小不能超过1024个字节,而post不仅可以提交文本还有二进制文件。所以要上传文件,就使用post请求方式。
jsp的本质就是一个servlet 他会被容器转化成servlet,在编译成class文件,调用jspService()处理请求。
jsp注重的是页面展示,servlet注重的是流程控制和事务处理
HttpServletRequest:封装了与请求相关的信息
HttpServletResponse:封装了与响应相关的信息
a.有setAttribute,没有setParameter方法
b.getParameter获取到的值只能是字符串,不可以是对象,而getAttribute获取到的值是Object类型的。
c.getParameter获取from表单传递过来的值;getAttribute只能获取使用setAttribute设置的值。
request:包含用户端请求的信息
response:包含服务器传回客户端的响应信息
session:与请求有关的会话期
pageContext:管理网页属性
application:服务器启动时创建,服务器关闭时停止,为多个应用程序保存信息
out:向客户端输出数据
config:servlet的架构部件
page:指网页本身
exception:针对错误页面才可使用
page:一个页面
request::一次请求
session:一次会话
application:服务器从启动到停止。
(动态包含使用使用page属性实现。)
(静态包含使用<%@include file=””>使用file属性实现。)
Include指令用于把另外一个页面包含到当前页面中,在转换成servlet的时候包含进去的。
动态include用动作实现,它总会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。
静态include用伪码实现,不会检查所包含的文件变化,使用于包含静态页面
请求转发与重定向
请求转发:服务器内部跳转,浏览器地址栏的地址不变,重定向:告诉浏览器,发送二次跳转。
请求转发:可以共享request里面的数据,重定向:不能共享数据。
请求转发:只能在同一个项目跳转,重定向:可以跨域访问。
请求转发:效率高,重定向:效率低。
jsp:include:在页面被请求的时候引入一个静态或动态的文件。
jsp:useBean:实力化一个对象。
jsp:setProperty:设置对象的属性。
jsp:getProperty:获取对象的属性。
jsp:forward:请求转发。
response.sendRedirct( “path” ):请求重定向
页面的路径是相对路径。sendRedirect可以将页面跳转到任何页面,不一定局限于本web应用中。 跳转后浏览器地址栏变化。
request.getRequestDispatcher(“path”).forward( request,response ) :请求转发
Servlet页面跳转的路径是相对路径。forward方式只能跳转到本web应用中的页面上。
跳转后浏览器地址栏不会变化。
使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribute
在请求到达servlet之前,执行过滤器,过滤器决定是否执行(可以进行权限控制),可以提前设置一些参数(比如在执行action前设置统一字符集)。
当你在执行一个方法,在这个方法前或方法后执行一些方法(防止重复提交)。
对特定的事件进行监听,当产生这些事件的时候,会执行监听器的代码,分为生命周期监听器和数据变化监听器(统计在线人数(监听session)、网站的访问量(request))。
JSP标准标签库(JSTL)是一个JSP标签集合,它封装了核心库
c标签,fmt格式化标签,fn字符串处理标签,sql标签,xml标签。Jstl还提供了实现自定义标签功能,方便我们的页面控件编写。
Jsp修改page指令 <%@ page isThreadSafe=”false”%>,将编译后的servlet实现SingleThreadModel接口。
EL表达式全名是Expression Language
EL表达式一般操作的是作用域(page、request、session、application)中的属性,使用${标识符}.
对jvm的堆的最大值修改为可用内存的80%(修改catalina.bat的相关参数)。
禁用DNS查询(直接使用IP地址访问)。
修改最大线程连接数。
设置超时机制。
1.将web项目文件拷贝到webapps目录中。
2.在tomcat中的conf目录下修改server.xml文件,在host节点中添加标签。并指向项目所在路径。
3.在tomcat中conf\Catalina\localhost目录下添加一个xml文件 文件名可用随意取,并写入一个标签,并指向项目所在路径。
4.用tomcat在线后台管理器,一般tomcat都打开了,直接上传war就可以
MongoDB是世界上最受欢迎的NoSQL数据库之一。 系统的简单性使其易于调整和维护。
当我们在Web服务中使用MongoDB之类的数据库时,我们往往会忽略其中的性能部分。当数据库扩展到一个很大的容量时,它可能会变慢。 原因可能是基础架构设置或系统配置不正确(如无索引等)。 如果您的Web服务器和数据库服务器在不同的系统上运行,那么延迟也会成为问题。 这些都会影响服务的响应时间,从而影响整个应用程序的性能。
为了解决这些问题,我们应该在系统中引入数据库缓存。
缓存是计算机科学中的一种技术,用于将经常访问的数据存储在临时存储区(称为缓存)中,以减少硬盘驱动器的读写次数。 我们的计算机有一个缓存,浏览器有一个缓存,Web服务器有一个缓存,本文也是从缓存中检索的。缓存无处不在,这种技术可以使您的Web应用程序的性能飞速增长。
用户请求您的服务时。 Web服务器首先读取或写入数据库,然后返回响应。 对于缓存,服务器首先检查缓存副本是否存在,如果存在,它将从缓存中返回数据,而不是询问数据库。 它节省了时间和数据库的计算量。
让我们构建一个简单的网络服务应用程序,该应用程序公开一个API以从MongoDB数据库中读取数据。
让我们开始一个新的项目。
1 |
mkdir mongoCacheDemo && cd mongoCacheDemo |
然后运行此命令创建一个Node项目。
1 |
npm init --y |
让我们下载依赖项。
1 |
npm i --S mongodb redis express |
app.js代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const express = require('express'); const router = express.Router(); const app = express(); const models = require('./models'); router.get('/', (req,res) => { res.send('Hello'); }); router.get('/articles/:id',async (req,res) => { // return article from database try { const articleData = await models.getArticle(parseInt(req.params.id)); res.json({error: false, message: 'success', data: articleData}); } catch(e) { res.json({error: true, message: 'Error occurred during processing'}); } }); app.use('/', router); app.listen(process.env.PORT || 3000, () => { console.log(`App is listening at ${process.env.PORT || 3000}`); }); |
models.js代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
const mongo = require('mongodb'); // connect to MongoDB var dbo = null; mongo.connect('mongodb://localhost:27017/codeforgeek', { useNewUrlParser: true }, (err, db) => { if (err) { process.exit(0); } dbo = db.db('codeforgeek'); console.log('connected to the database'); }); function getArticle(id) { return new Promise((resolve, reject) => { dbo.collection('posts').find({ id: id }).toArray((err, articleData) => { if(err) { return reject(err); } resolve(articleData); }); }); } module.exports = { getArticle: getArticle }; |
运行代码并检查其工作情况。
1 |
node app.js |
结果
添加Redis Cache让我们在代码中添加Redis缓存。
更新后的 models.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
const mongo = require('mongodb'); const redisClient = require('redis').createClient; const redis = redisClient(6379, 'localhost'); // connect to MongoDB var dbo = null; mongo.connect('mongodb://localhost:27017/codeforgeek', { useNewUrlParser: true }, (err, db) => { if (err) { console.log(chalk.red(err)); process.exit(0); } dbo = db.db('codeforgeek'); console.log('connected to the database'); }); redis.on("connect", () => { console.log('connected to Redis'); }); function getArticle(id) { return new Promise((resolve, reject) => { redis.get(id,(err, reply) => { if(err) { console.log(err); } else if(reply) { resolve(reply); } else { dbo.collection('posts').find({ id: id }).toArray((err, articleData) => { if(err) { return reject(err); } if(articleData.length > 0) { // set in redis redis.set(id, JSON.stringify(articleData)); } resolve(articleData); }); } }); }); } module.exports = { getArticle: getArticle }; |
在getArticle函数中代码中,我们正在使用ID参数检查缓存是否存在。 如果确实存在,我们将不做进一步处理就返回数据。 如果缓存不存在,我们将在MongoDB中查找该数据并将其添加到缓存中(如果找到)。逻辑很简单并且可以正常工作。再次运行该应用程序并检查工作情况。
缓存存在多长时间? 当有新的或更新的数据时会发生什么? 引入缓存时,我们需要解决这样的问题。保留缓存的时间取决于系统。 我将缓存保留了24小时,然后再次对其进行了重建。 根据数据的更新频率,您可能需要1个小时或100个小时。更新数据时,我们只需更新Redis缓存或从Redis中删除条目即可让系统重建缓存。
上面显示的代码库是基本的缓存系统。 不建议将所有内容都放在缓存中,这也会减慢系统速度。 我们只应将这些数据放入对“最受欢迎的文章”有很高要求的缓存中。Redis内置了各种缓存策略,我建议使用LRU(最近最少使用)缓存策略。 此策略将删除最近最少使用的缓存项。启动Redis时,可以将算法作为参数传递。
1 |
redis-server -maxmemory-policy allkeys-lru |
在本文中,我们研究了缓存及其如何在构建高性能系统中使用。 数据库缓存是一个了不起的概念,越来越多的系统都在使用缓存。