Summary of the use of URLs in Javaweb (relative path)

Source: Internet
Author: User
Absolute Path: is the path that starts with the letter, shaped like C:\windows\system32\cmd.exe
relative path:1, "./": Indicates the directory where the file is currently located. 2, ". /": Represents the top level of a directory for a file. 3, "/": Represents the file's first level directory. (Just under the root directory) examples of relative paths:

1:c:\program files\other\index.htm

2:c:\program files\javaweb\test\01.htm

The file path represented above has two absolute paths.

Ask 1:index.htm to link to 01.htm this file, the correct link should be what.

Answer 1: ". / javaweb/test /01.htm "Ask 2:01.htm to link to index.htm this file, what should be in the 01.htm file." Answer 2: ". /.. /other/index.htm Tomcat software and Eclipse software layout for Web projects: as shown in the figure, a project named "Zuoye" deployed in Tomcat

A navigator view of a project named "Zuoye" that is deployed in eclipse as shown in the figure.




Summary: 1, the relative paths we write in the code for the Javaweb project that we published are all based on the Tomcat software. 2, find ". class file (bytecode file) A, Java project in the Eclipse workspace of the project's bin for the root directory to find B, Web projects under Tomcat WebApps of the project web-inf/ Classes directory (can also be seen under the Navigator view, under the project's build file, but actually under the corresponding items under Tomcat WebApps file) 3, find ". Java" file A, the SRC for the project to the root directory to find (generally not find ". Java" file, because there is nothing to use, in addition to copying the code 4, find. HTML files A, Java projects find B, javaweb projects are found under the items in the Tomcat installation path (but can also be seen under WebContent files under Projects in Eclipse, Workspace). But the actual lookup is under the WebApps file in Tomcat under the corresponding items to find 5, the ". Java" file is not found in the Javaweb project, except for ". Class" File 6, where ". Java" and ". Class" files are typically not found in the Java project. . Properties, ". txt", ". jpg" files javaweb A relative path is often used when using URLs, and here are some of the things I've summed up about useful to file paths. 1, HttpServletResponse object redirection: (pseudo code below)

Response.sendredirect ("signin.html");

Response.sendredirect ("/zuoye/signin.html");

Response.sendredirect ("http://localhost:8080/zuoye/signIn.html");

Response.sendredirect ("/zuoye/sign");

Note: First line pseudocode: Write directly the full name of the file that needs to be redirected.

Second line of pseudocode: Start with the "/" plus project name, followed by the path of the file relative to the project name that needs to be redirected

Third line pseudo code: Absolute path

Line four pseudocode: Start with "/" plus the project name, followed by the path annotation for the file relative to the project name : The first three lines of code are the way I have summed up the path of representation.

Note: Redirection can be directed to an HTML file, or to a servlet file.

The following complements a redirect instance of the project name "Test_server_1".

(as shown in the two diagram below, the redirected path does not start with "/", so there is an error.) Please refer to the example given above for the correct format. )


2, Request forwarding: (pseudo code as follows)

Request.getrequestdispatcher ("sign"). Forward (Request,response);

Request.getrequestdispatcher ("jsp/register.jsp"). Forward (request, response);

Request.getrequestdispatcher ("/jsp/register.jsp"). Forward (Request,response);

Note: The path can only be written as a relative path, and this relative path only drops the project name path (the last instance illustrates what the project name path is) and "/" and cannot write a relative path that starts with the "project name path."

The following pseudocode is the wrong one.

Request.getrequestdispatcher ("/zuoye/sign"). Forward (Request,response);

3, JS code to achieve the page jump: (pseudo code as follows)

window.location.href= ' http://127.0.0.1:8080/zuoye/welcome.html ';

Response.getwriter (). Write ("<scripttype= ' text/javascript ' >window.location.href= '/zuoye/sign.html '; </") Script> ");

Note: The first line of pseudo code for the JS file code, which is the absolute path;

The second line of pseudo code is the code in the servlet file, which is relative to the path.

4, JSP implementation of timed refresh: (real code below)

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script type= "Text/javascript" >
var num = 2;
function Time () {
if (num ==0) {
* * window.location.href= "/sign_jsp_el_jstl/jsp/welcome.jsp"; */
<%
Response.setheader ("Refresh", "3;/sign_jsp_el_jstl/jsp/welcome.jsp");
%>
}
document.getElementsByTagName ("Font") [0].innerhtml= num;
num--;
}
SetInterval ("Time ()", 1000);
</script>
<body>
</body>

Attention:

The red section of the code above is the two most common ways, and the next one is listed in the other way (you just need to write the file name and not start with "/")

Response.setheader ("Refresh", "3;welcome.jsp");

The following is the wrong way:

Response.setheader ("Refresh", "5;${pagecontext.request.contextpath}/jsp/welcome.jsp");

Note: Request forwarding and timing refresh, the essence of the implementation is the same, but the performance is not the same, one can realize the function of timing.

5, Hyperlink label: (pseudo code as follows)

<a href= "/ssign/sign.html" > Login </a>

<a href= "http://127.0.0.1:8080/ssign/sign.html" > Login </a>

Note: The path to the calling HTML file can either write an absolute path or find its relative path relative to the project name in the workspace (Java projects are found in Workspace, Web projects are found in Tomcat) 6, form: (Pseudocode below)

<form action= "/zuoye/register" method= "Get" >

<form action= "Register" method= "POST" >

Note: The path here is the servlet file path, you can write the path of the HTML file, you can refer to the above two lines of code for handwriting.

Attention:

When using the second method, the current file is correct in the root directory (seen directly under the WebContent file in Eclipse), but it is not an error if it is not in the root directory.

The reason is because the second way automatically complements the path to the file relative to the root directory.

For example: The relative path of the register file we need to access here is "/zuoye/register", and if our file is under Webcontent/html, the system will complement the second path as "/zuoye/html/register", Then you don't like to see the 404 pages coming up ...

7, Javaweb project in the Web.xml configuration file: (Specific configuration information as follows)

<servlet-class>com.wq.servlet.Servlet_register</servlet-class>

Note: Right-click the Java class name to choose "Copy qualified name" to get the fully qualified name of this class (fully qualified name: Package name + class name)

8, Drive interface path: (Specific path below)

Driverclass=com.mysql.jdbc.driver

Note: The path to the driver interface is the path to the driver interface class in the jar package (Mysql-connector-java-5.1.7-bin.jar) 9, the path to the MySQL database: (MySQL path)

Url=jdbc:mysql://localhost:3306/test

Url=jdbc:mysql://127.0.0.1:3306/test

Note: localhost can be replaced with any IP address of the computer where the MySQL database can be connected.

10, class Path: (pseudo code as follows)

InputStream in =jdbc_util.class.getresourceasstream ("/com/wq/config/db.properties");

Attention:

getResourceAsStream ("/..."):

"/": Represents the root directory, must be "/" as the beginning of the line ....

In the Java project, SRC is the root directory

In the Javaweb project, web-inf/classes is the root directory

Attention:

The ". Properties" file in the Javaweb project can only use the method of the classpath and cannot use the stream.

11. External link CSS file:

<link href= ". /css/a.css "rel=" stylesheet "type=" Text/css "/>"

12, External link js file:

<script type= "Text/javascript" src= ". /js/mydate.js "> </script>

13, Picture label:

14. The imported jar package path in the Javaweb project:

The jar package in the Web project is placed under this path.

"\webcontent\web-inf\lib"
Summary: The above summarized several uses, except some special exception cannot use absolute path, all can use absolute path (above some did not write absolute path)

Additional two points: (Method of obtaining project name path and project full path in JSP)

JSP expression:
<%=request.getcontextpath ()%>: Project name Path (/zuoye)

<%=request.getrealpath ("/")%>: Project full path (C:/...../zuoye) (the middle path is represented here with five dots)

El expression:

${pagecontext.request.contextpath}: Project name path












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.