在Java代码(非JSP及Action,Servlet,Controller)中获得WebRoot的物理路径

JavaWeb底层实战:手写Servlet+JSP+MySQL实现MVC购书系统 MVC架构是JavaWeb开发的核心范式,其本质是请求响应链路中职责分离与数据流转的工程实践。理解Servlet生命周期、JSP编译机制、JDBC连接管理及MySQL事务边界,是掌握传统JavaWeb技术栈的关键基础。本文聚焦裸写Servlet而非Spring Boot封装场景,深入解析HTTP请求如何穿透Tomcat容器、触发Controller调度、经Service层事务控制、最终由DAO操作MySQL并渲染JSP视图。涵盖JDK8/Tomcat9/MySQL8环境适配、ThreadLocal连接绑定、 阅读详情

在开发Web方面的应用时, 经常需要获取 服务器中当前WebRoot的物理路径;

如果是Servlet , Action , Controller, 或则Filter , Listener , 拦截器等相关类时, 我们只需要获得ServletContext, 

然后通过ServletContext.getRealPath("/")来获取当前应用在服务器上的物理地址.


如果在类中取不到ServletContext时, 有两种方式可以做到

1) 利用Java的类加载机制

 调用 XXX.class.getClassLoader().getResource(""); 方法来获取到ClassPath , 
然后处理获得WebRoot目录这种方式只能是该class在WebRoot/WEB-INF/classes下才能生效,

 如果该class被打包到一个jar文件中, 则该方法失效。这时就应该用下面一种方式

2) spring框架的思路, 在WEB-INF/web.xml中 , 创建一个webAppRootKey的param,

 指定一个值(默认为webapp.root)作为键值, 然后通过Listener , 或者Filter , 
或者Servlet 执行String webAppRootKey = getServletContext().getRealPath("/"); 
并将webAppRootKey对应的webapp.root 分别作为Key , Value写到System Properties系统属性中。

之后在程序中通过System.getProperty("webapp.root")来获得WebRoot的物理路径 


根据第二种的思路,我们还可以再扩展一下。不过对于在部署在一台服务器中的应用来说,已经足够了。
在线酒店管理系统Java Web实战:JSP+Servlet+MySQL+Tomcat全链路解析 Java Web开发中,JSP与Servlet作为经典的服务端技术组合,承载着从HTTP请求解析到动态页面渲染的完整链路。理解一次浏览器请求如何经过Servlet控制器、JDBC数据库访问和JSP视图呈现,是掌握MVC分层架构的关键。这类技术栈在高校课程设计、毕业设计以及中小型内部管理系统中依然广泛使用,因为其代码结构透明、依赖简单,适合快速实现业务闭环。以在线酒店管理系统为例,它围绕用户、房型、房间、订单四张核心表,借助Tomcat容器完成部署,实现用户预订、后台管理等典型场景。从数据库初始化到JDBC配 阅读详情

相关推荐

基于SSM+JSP+MySQL的健身俱乐部网站毕业设计:搭建到答辩全攻略

在Java Web开发领域,SSM(Spring+Spring MVC+MyBatis)与JSP、MySQL的组合是一套经典的工程化技术栈。其核心原理是通过Spring管理业务对象、Spring MVC处理请求分发、MyBatis完成数据持久化,再借助MySQL存储业务数据,最终由JSP在服务端渲染页面。这套架构的价值在于三层职责清晰、代码可维护性强,尤其适合中小型业务系统的快速落地。在高校毕业设计场景中,健身俱乐部网站恰好覆盖了会员管理、课程预约、私教订单等典型业务,既能体现数据库外键关系,又能展示事务控

weixin_27966471的博客 269

controller中获取当前项目物理绝对路径

用HttpServletRequest request获取 request.getSession().getServletContext().getRealPath(""); 上面马赛克的就是项目名称 转载于:https://www.cnblogs.com/xiaostudy/p/10956401.html...

weixin_30933531的博客 1161

SSM宠物管理系统实战:从源码部署到二次开发避坑指南

在Java Web开发中,SSM框架(Spring、SpringMVC、MyBatis)是经典的三层架构组合,它通过控制反转、依赖注入和持久层映射,将业务逻辑、请求路由与数据访问解耦,是理解企业级Java项目的基础。这种架构广泛应用于高校课程设计和毕业设计场景,尤其在管理类系统中,能快速搭建功能完整的后台应用。本文以一套宠物管理系统为例,详细拆解SSM整合的完整调用链,从Action类的继承关系、Spring容器装配、MyBatis动态SQL到Layui表格对接,讲解如何从源码角度理解项目结构。随后给出My

weixin_33824385的博客 273

servlet得到webroot路径

servlet得到webroot路径 //file:/F:/WorkSpace/Myeclipse_Workspace_1/.metadata/.me_tcat7/webapps/Hello/WEB-INF/classes/ String classessPath = this.getClass().getResource("/").toString(); //F:/WorkSpace/Myeclipse_Workspace_1/.metadata/.me_tcat7/webapps/Hello/WEB-I

wu964521的博客 374

Python量化投资入门教程:从零构建你的第一个交易策略

量化投资教程以数学模型和计算机程序实现超额收益,选用Python工具链完成数据获取、策略构建及回测。通过移动平均线交叉策略示例,展示数据预处理、信号生成、收益计算及可视化全流程,结合收益率、最大回撤和夏普比率评估策略,并建议参数优化与策略融合提升稳健性。

ylmx5201314的博客 2470

STM32F103C8T6板子使用说明

pcb版,把众多元器件集中到一个板子上面管理。

LYC_462857656的博客 2200

jsp和Controller服务器获取路径

Controller:服务器路径 // 获得项目的路径 ServletContext sc = request.getSession().getServletContext(); // 上传位置 String path = sc.getRealPath("/img") + "/"; // 设定文件保存的目录 jsp 一:获取路径:访问路径 jsp中访问后台需要...

Zsysu_it的博客 3438

Linux 常用命令大全(2025 最新版)

掌握这些命令,你已能应对80% 的 Linux 日常操作!更多高级技巧(如crontab定时任务、iptables防火墙配置)可参考手册(man 命令名)或官方文档。实践是学习命令的最佳方式,建议在虚拟机或云服务器中多动手操作!

2302_79600390的博客 2010

java学习-Controller获取当前项目路径

String path = request.getServletContext().getRealPath("");//获取项目动态绝对路径  

阿泷的学习日志 6508

游戏开发实战:5种AOI算法对比(九宫格 vs 灯塔 vs 十字链表)

本文深入对比了游戏开发中五种主流AOI算法(九宫格、灯塔、十字链表等)的性能与适用场景,帮助开发者根据项目规模选择最优解决方案。通过详细的技术分析和实战数据,揭示不同算法在万人同屏等极端场景下的表现差异,为MMORPG等大型多人在线游戏提供关键性能优化指南。

weixin_29268385的博客 484

三维点云转深度图

点云投影过程是将三维点云数据转化为二维图像坐标的关键步骤,它的实现涉及到相机内外参数的计算,包括相机的内部参数(如焦距和主点)和外部参数(如相机位置和方向)。具体来说,可以通过将每个点的三维坐标转换为相机坐标系下的坐标,然后使用相机内部参数和外部参数来计算在二维图像中的坐标。接着计算每个点的深度值,将深度值映射到像素值上,最后创建深度图像并将深度值赋给对应的像素值。这里需要注意的是,在将深度值映射到像素值上时,需要将像素值转换为 uchar 类型,否则在将深度值赋给像素值时会出现类型不匹配的错误。

点云兔子的博客 3182

MVC中获取当前URL、controller、action

一、URL的获取很简单,ASP.NET通用: 【1】获取完整url(协议名+域名+虚拟目录名+文件名+参数) stringurl=Request.Url.ToString(); ...

congquangan1125的博客 517

canoe安装完成开启之后因为VectorDeviceParams30_64.dll报错崩溃打不开...如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值。

**My Coding Family** 1119

java非jsp_在Java代码(非JSP及Action,Servlet,Controller)中获得WebRoot的物理路径

在开发Web方面的应用时, 经常需要获取 服务器中当前WebRoot的物理路径;如果是Servlet , Action , Controller, 或则Filter , Listener , 拦截器等相关类时, 我们只需要获得ServletContext,然后通过ServletContext.getRealPath("/")来获取当前应用在服务器上的物理地址.如果在类中取不到ServletCont...

weixin_34463228的博客 199

079、LVGL基础控件:消息框(Msgbox)

《LVGL消息框控件深度解析与避坑指南》 文章深入剖析了LVGL消息框控件的核心特性和使用技巧。消息框本质是带按钮的模态容器,而非独立窗口。作者分享了创建消息框的正确姿势、标题/内容设置方法、安全的按钮事件处理方式(避免直接调用close函数导致栈溢出),并详细介绍了模态/非模态实现、样式自定义、动态更新等实用技巧。针对常见问题如内存管理、按钮数量限制、动画效果等给出了专业建议,特别强调了消息框作为用户交互关键环节的优化重要性,建议通过异步删除、合理父对象选择等方式提升稳定性。文章包含大量实际项目经验总结,

m0_50546716的博客 158

SAP GUI 经典事务在 Fiori Launchpad 中的穿越之道:原理、配置与实践指南

在许多企业的 S/4HANA 或 ECC 系统里,经典 SAP GUI 事务依旧承载着关键业务。如何把这些老将自然地嵌入到 Fiori Launchpad,让用户在统一入口里无缝切换新旧 UI,是项目中经常遇到的主题。本文结合社区博文、官方文档与实际项目经验,系统梳理这一过程的技术原理、配置步骤、性能优化以及常见坑点,并给出贴近中国读者的案例解析与改进建议。阅读完毕,你将不仅知道怎么做,还会理解。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 628

MyEclipse及Tomcat的配置

文章来源:http://www.cnblogs.com/smyhvae/p/4134921.html 【开发环境】物理机版本:Win 7旗舰版(64位)jdk版本:jdk1.8.0_20MyEclipse版本:10.7.1Tomcat版本:apache-tomcat-7.0.57一、Servlet的基本概念:Servlet是sun提供的一种动态web资源开发技术.本质上就是一段java小程序.可以

啦啦啦的博客 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Java web项目中获取WebRoot绝对目录

具体获取方式: /** * * @return WebRoot目录的绝对路径 */ public static String getWebRootAbsolutePath() { String path = null; String folderPath = Path.class.getProtectionDomain().getCodeSource() ...

wanggp 1469

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

获得WebRoot的物理路径

webroot物理路径获取

hi_soyo的博客 637

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

SpringBoot 中的mapper,service,controller,model

最近一次更新时间:2019-8-6 springboot 只是组装了spring和springmvc。 目录直通车 一、SSM(Spring+SpringMVC+MyBatis)架构 1、 SSM的工作原理 (1) SSM系统架构 (2) SSM程序执行流程 2、 MSCM是什么及其作用 (1) controller (2) model (3) service (4) ma...

DJun的博客 2万+
上一篇: Java web 项目读取src或者tomcat下class目录下的xml文件或者properties文件
下一篇: 数据库分库分表(sharding)系列
JavaAlpha
博客等级 码龄18年 910粉丝 511原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址