Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example analysis of ServletContext.getRealPath () input parameters that start with "/"

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you ServletContext.getRealPath () input parameters to start with "/" example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

ServletContext.getRealPath () is calculated from the current folder where servlet is stored in tomcat.

The input parameter of ServletContext.getRealPath () should start with "/"

For example, there is a servlet called UploadServlet, and the absolute path after it is deployed under tomcat is as follows:

"C:\ ProgramFiles\ apache-tomcat-8.0.3\ webapps\ UploadServlet"

that,

ServletContext.getRealPath ("/") returns "C:\ ProgramFiles\ apache-tomcat-8.0.3\ webapps\ UploadServlet"

ServletContext.getRealPath ("/ attachment") returns "C:\ ProgramFiles\ apache-tomcat-8.0.3\ webapps\ UploadServlet\ attachment"

ServletContext.getRealPath ("attachment") causes NullPointerException

Parameter getRealPath is passed in

When using ServletContext.getRealPath (), the parameter passed in is the relative path from the folder where servlet is currently deployed in tomcat, starting with "/", otherwise the path will not be found, resulting in NullPointerException

In the process of solving the problem, it was found that the eldest brother had made a similar record of ServletContext.getRealPath, but he did not agree with him. The results I have observed are:

ServletContext.getRealPath ("/") returns "C:\ ProgramFiles\ apache-tomcat-8.0.3\ webapps\ UploadServlet"

ServletContext.getRealPath ("/") + "xxx" returns "C:\ ProgramFiles\ apache-tomcat-8.0.3\ webapps\ UploadServletxxx"

ServletContext.getRealPath ("/") + "/ xxx" returns "C:\ ProgramFiles\ apache-tomcat-8.0.3\ webapps\ UploadServlet\ xxx"

That is, the string returned by getRealPath () ends without a "/"

Notice that under Windows, during the Eclipsedebug process, you see that the string returned by getRealPath () actually looks like this:

"C:\\ ProgramFiles\ apache-tomcat-8.0.3\\ webapps\\ UploadServlet\\ attachment"

The first "\" in "\" is an escape character, you know.

The above is all the content of the article "sample Analysis of ServletContext.getRealPath () input parameters starting with" / ". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report