Path problems in Web applications

Source: Internet
Author: User
Tags tomcat server

1. Path problems in Web applications

In a Web application, where a path is involved:

A) within the Web. xml file, the servlet's mapping configuration (servlet-mapping) requires setting the url-pattern:<url-pattern> path </url-pattern>

b) When forwarding within a servlet, you need to specify the path: Request.getrequestdispatcher ("path"). Forward (request, response);

c) When redirecting within a servlet, you need to specify the path: Response.sendredirect ("path");

d) In the Web page, the href attribute of the hyperlink a tag needs to specify the path: <a href= "path" > Hyperlink </a>

e) In a Web page, the Action property of the form form needs to specify the path: <form action= "path" method= "POST" ></form>

Question: How should these paths be written?

Ideas:

1, in the Web application, all involved in the path of the place, to start with "/"

2, think "target resource" is to who use.

Used by the server:/represents the root directory of the current web App (%tomcat%/webapps/webroot)

For the browser:/means under the root directory of the current Web site (webapps) (%tomcat%/webapps/)

3, if the target URL is http://localhost:8080/myweb/target.html, then each path is written as follows:

A) Use of the Tomcat server. <url-pattern>/target.html</url-pattern>

b) Use of the Tomcat server. Request.getrequestdispatcher ("/target.html"). Forward (request, response);

c) Browser use. Response.sendredirect ("/myweb/target.html");

d) Browser use. <a href= "/myweb/target.html" > Hyperlinks </a>

e) Browser use. <form action= "/myweb/target.html" method= "POST" ></form>

2. In the path, "." The meaning and specific location of the representative.

In either a Java project or a Web project, the "." represents the run directory of the Java command. So, in which directory does the Java command run??

In the Java project, many Java training organizations have said that the MyEclipse tool will set the CLASSPATH environment variable to the%project%/bin directory, and the directory in which Java commands are executed is in the%project% directory

In a Web project, the MyEclipse tool needs to start the Tomcat server, so the directory that executes the Java command is in the%tomcat%/bin directory

For example, add the Db.properties file separately in the SRC directory of the Java Project and Web project

In a Java project, you can use file File = new file ("./src/db.properties"), and you can access the Db.properties file because "." Represents the execution directory of the Java command, and MyEclipse executes the Java command in the root directory of the project.

In a Web project, the file File = new file ("./src/db.properties") is not accessible because there is no SRC folder or db.properties file in the%tomcat%/bin directory.

3. Access to resources through GetResource () and getResourceAsStream ()

In both Java projects and Web projects, you can use the following methods:

Class name. Class.getResource ("path"); Before use, you need to start with "/"

Class name. Class.getresourceasstream ("path"); Before use, you need to start with "/"

Class name. Class.getclassloader (). GetResource ("path"); Before use, do not need to start with "/"

Class name. Class.getclassloader (). getResourceAsStream ("path"); Before use, do not need to start with "/"

4. Get Resources for Web apps

Get the Web app's resource file from the ServletContext object:

String Getrealpath (String path)

InputStream getResourceAsStream (String path)

Path problems in Web applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.