Table of Contents
1. Phenomenon and Concept" >1. Phenomenon and Concept
1. Problem
2. Absolute path concept
2. Summary of the meaning of '/' in JavaWeb development" >2. Summary of the meaning of '/' in JavaWeb development
1. The root path of the current Web application
2. The root path of the Web site
Home Java javaTutorial Explanation of the path problem after Servlet jumps to JSP page

Explanation of the path problem after Servlet jumps to JSP page

Jul 17, 2017 pm 01:49 PM
javascript servlet question

1. Phenomenon and Concept

1. Problem

When the Servlet is forwarded to the JSP page, the path of the Servlet is displayed on the browser address bar. , and if the hyperlink of the JSP page is still relative to the address of the JSP page and the Servlet and the JSP page are not in the same folder, there will be a path confusion problem.

2. Absolute path concept

The path relative to contextPath (the context of the current Web application).

Solution: Use absolute paths instead of relative paths for hyperlinks. If / represents the root directory of the site, add contextPath in front of it.

<a href="<%= request.getContextPath() %>/TestServlet">To B</a>
Copy after login

2. Summary of the meaning of '/' in JavaWeb development

1. The root path of the current Web application

'/' represents http://localhost:8080/contextPath/ : the address processed by the Servlet

1) Forwarding: request.getRequestDispatcher("/path /b.jsp").forword(request, response);

2) Mapping Servlet access path in web.xml file

<servlet-mapping><servlet-name>TestServlet</servlet-name><url-pattern>/TestServlet</url-pattern></servlet-mapping>
Copy after login

3) Various custom tags middle'/'.

2. The root path of the Web site

'/' represents http://localhost:8080/: the address processed by the browser

1) Hyperlink: /TestServlet">To B

2) Action in form tag:

/b.jsp">

3) Redirect: response.sendRedirect("/b.jsp");


3. Issues with site root directory and css path (jsp is a server-side program, the address changes, and the relative path to the site root directory is generally used when referencing)
We call a relative path like this /test/.... a relative path relative to the site root directory.
When introducing css into a jsp, if its relative path is relative to the current jsp file, and you forward the jsp in a servlet that is different from the path of the jsp, you will find that the css style does not work at all. . This is because the css path when forwarding in the servlet is the relative path to the servlet instead of the jsp path. So you cannot use such a path in jsp at this time: or HTML relative paths similar to href="one.css" and ../../one.css are relative to the file that references this css (a. jsp) relative path. When forwarding in a servlet, it is a relative path to the servlet. Because the jsp path and the servlet path are different, such a reference must be wrong.
So at this time, you need to use the site root directory, which is the directory relative to http://192.168.0.1/ and starts with "/".
Therefore, the above error should be corrected to a directory relative to the site root directory similar to href="/test/one.css". In this way, after the servlet is forwarded and in the jsp, the relative path is relative to the site root directory, and the defined css style can be used correctly.


Page jump problem:

Forward is high, Redirect is low, because the Redirect process is like this , request1 sent to server, server return back to client, and then

request2 then sent to server. But Forward is only processed on the server side and is transparent to the client side. Since Redirect has two transmissions, it is efficient Low.

Scope:
Because for request.setAttribute(), the survival range of the object it carries is only within the request, so the Redirect method will cause the object carried by the request to be lost.


The above is the detailed content of Explanation of the path problem after Servlet jumps to JSP page. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

How does Java Servlet implement distributed session management? How does Java Servlet implement distributed session management? Apr 16, 2024 pm 02:48 PM

There are two ways to implement distributed session management in JavaServlet: 1. Session replication: Copy session data to each server. 2. Session distribution: Use a centralized storage service to store session data and access it from multiple servers. The specific implementation methods are: session replication configures true in the web. session data.

What are the application scenarios of Java Servlet? What are the application scenarios of Java Servlet? Apr 17, 2024 am 08:21 AM

JavaServlet can be used for: 1. Dynamic content generation; 2. Data access and processing; 3. Form processing; 4. File upload; 5. Session management; 6. Filter. Example: Create a FormSubmitServlet to handle form submission, taking name and email as parameters, and redirecting to success.jsp.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

What are the questions in the Rulong 8 Wine Master exam? What are the questions in the Rulong 8 Wine Master exam? Feb 02, 2024 am 10:18 AM

What are the questions involved in the Yulong 8 Wine Master exam? What is the corresponding answer? How to pass the exam quickly? There are many questions that need to be answered in the Master of Wine Examination activities, and we can refer to the answers to solve them. These questions all involve knowledge of wine. If you need a reference, let’s take a look at the detailed analysis of the answers to the Yakuza 8 Wine Master exam questions! Detailed explanation of answers to questions in the Rulong 8 Wine Master exam 1. Questions about "wine". This is a distilled liquor produced by a distillery established by the royal family. It is brewed from the sugar of sugarcane grown in large quantities in Hawaii. What is the name of this wine? Answer: Rum 2. Question about "wine". The picture shows a drink made from dry ginseng and dry vermouth. It is characterized by the addition of olives and is known as "cockney"

See all articles