Use the getRequestDispatcher method in the servlet program to jump to the jsp page failure solution


foreword

In the servlet program, use the getRequestDispatcher method to jump to the jsp page and report an error. First of all, we need to understand the difference and meaning of the paths parsed between the server and the browser.


1. The relative path and absolute path in the web, the different meanings of the / slash in the web

1. Relative path and absolute path in Web

Paths in JavaWeb are divided into relative paths and absolute paths

The relative path is:

.   一个点表示当前目录
. . 两个点表示上一级目录
资源名 表示当前目录/资源名

The absolute path is:

http://ip:port/工程路径/资源路径

Absolute paths or base+relative paths are used in actual development

2. Different meanings of /slash in the Web

In the web, / slash is an absolute path
/ if the slash is parsed by the browser, the address is: http://ip:port/

例如<a href="/">斜杠</a>

/ slash If the address parsed by the server is http://ip:port/ project path

例如1、<url-pattern>/servlet1</url-pattern>
2、servletContext.getRealPath(“/”);
3、request.getRequestDispatcher(“/”);注意这个方法,直接默认进入工程路径下面,哪怕直接request.getRequestDispatcher(“”)里面没有具体路径也直接进入工程路径。

insert image description here
For example, as written in this picture, I filled in the complete absolute path. After accessing, it prompts that
insert image description here
there is "/" in front of http, indicating that when this method is parsed, it enters the project path by default.

Special case: response.sendRediect("/"); Send the slash to the browser for parsing. get http://ip:port

2. Solution to the problem

Several situations where the problem occurs

It should be noted that we must use absolute paths and cannot use relative paths. servlet.java is in the src directory. Here I set the Java directory as my source directory, and jsp is in the webapp directory, and there is no relative relationship with each other;
insert image description here

My purpose is to jump from the servlet program Action.java to the info.jsp file. I set the path like this at the beginning
insert image description here

So this kind of error has appeared, why? The getRequestDispatcher method is parsed by the Tomcat server. At this time, the parsed path obviously cannot go to the test directory, so I changed it to . ./info.jsp At this time, running the
insert image description here
server becomes this
insert image description here
description file It has been found, but there is a problem in the server. There is no relative relationship between these two files. Let's try using the absolute path, and the
insert image description here
result of the operation is:
OK!
insert image description here

Summarize

Learn from this author https://blog.csdn.net/Java_stud/article/details/80551681
Study hard and make progress every day!

Guess you like

Origin blog.csdn.net/inspireT/article/details/124330979