Article directory
1. Reproduce the error
I remember that when I used idea
to write Java web
a project, Tomcat
the following errors were often caused by the configuration:
That is The requested resource [/] is not available
the problem.
2. Analysis errors
It is catching up with the recent ChatGPT
fire, so I use it ChatGPT
to solve my problem, as follows:
The requested resource is not available
The requested resource may be unavailable due to a variety of reasons.It may have been removed, changed names, or moved to a different location. Please try searching for the resource again or contact the website’s administrator to find out more information.
Unfortunately, ChatGPT
given the reply in English, we might as well translate it into Chinese:
The requested resource is not available
The requested resource might not be available for various reasons. It may have been deleted, changed its name, or moved to a different location. Please try searching for the resource again or contact the webmaster for more information.
It can be clearly seen from the translation results: 请求的资源不可用
, that is, the problem mentioned on the Internet 404
.
3. Fix bugs
Since it is said that the requested resource is unavailable, I make the following modifications to ensure the availability of the requested resource.
Below, modify based on local projects and online projects.
3.1 Local projects
- The download directory we found first
tomcat
, mine is inD:\Software\tomcat8.5
, enterbin
the directory, as shown in the following figure:
- Find and double-click
startup.bat
the file to start ittomcat
, as shown in the figure below:
[Remarks] If you are using linux
a system (for example Ubuntu
, Apple Computer, etc.), after terminal
entering bin
the directory in , enter it sh startup.sh
to start tomcat
.
Tomcat
After successful startup, can you see its default interface as follows:
If this interface appears, there tomcat
is no problem.
- Click the drop-down box in the upper right corner, select
Edit configuration
, and create a new oneTomcat local server
, as shown in the following figure:
- Then click
Deployment
.
After clicking, there is probably nothing, and +
a dialog box like this will pop up by clicking on the right.
Find your Tomcat
installation directory, there is a folder webapps
under the folder , as shown below:ROOT
Just select ROOT
it, click ok
it, and Deployment
the configuration is completed.
- last point
Tomcat
run
If you jump to the default page normally, it means that there is no problem with your basic configuration and external dependencies (Tomcat is installed correctly, and Java and Maven dependencies are correct) .
On the Internet, it is said that the wrong URL
path will be reported if the path is changed randomly 404
, and it is also said that deployment
the path must be URL
consistent with the path.
If you have no problem with the above configuration, you can verify it yourself to see what kind of situation will report 404
an error.
Let me first talk about the conclusions after observation:
If you want to change it, don't change it URL
, but modify the path deployment
under it Application context
, as follows:
If you change it, URL
it will be changed automatically. In the end, the names behind their two paths will be consistent, as long as they are consistent.
Don't change it first URL
, if you change it first URL
, the corresponding ones Application context
will not be changed later, and mistakes will easily occur 404
.
3.2 Online Projects
If you have no problem testing the local project, you can test your actual project as follows.
At this moment, you should know Application context
what to pay attention to when writing.
If it is not the above configuration problem, there is a problem with the path configuration in your project code.
If you Maven
create a new one Java Web
, it will most likely be such a path structure:
Generally, this web.xml
configuration error causes 404
the error.
First, Tomcat
start the server to see if the project home page is displayed normally:
-
If it cannot be displayed normally, check
web.xml
whether your home page address is written correctly. -
If the home page is displayed normally, but the jump page cannot be displayed normally, it is likely that your
form
path configuration is wrong.
Let me give a simple example, for example, in index.jsp
, you want to do a form submission and jump, as follows:
If you jump to a static page, check whether your action
path configuration is correct.
If it is a dynamic page, for example, if you want to submit a certain form result to servlet
, don't write it according to the relative path.
At this time, you need web.xml
to perform corresponding url_pattern
and mapping
configuration, but such configuration is likely to make some mistakes, leading to 404
the problems you encountered.
Therefore, I strongly recommend that you use annotations for configuration instead of configuration and web.xml
configuration .url_pattern
mapping
Assuming that I LoginServlet
configure this, I only need to LoginServlet
add @WebServlet
annotations in it, as shown in the following code:
@WebServlet(urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
With this configuration, the above form
form will be submitted and redirected normally without 404
errors.
If the above methods cannot solve your problem, you can refer to the following methods to solve your problem.
4. Other solutions to this error
- Directories cannot be referenced.
You can Eclipse
check 包资源管理器(Package Explorer)
the location where the file is stored.
Since the content under the META-INF
and WEB-INF
folders is not released to the public, if you quote a file with these two directories, it is definitely not allowed. The following URL address is wrong:
http://localhost:8081/testProject/WEB-INF/index.html
-
URL
Input error, the troubleshooting method is as follows:-
First check
URL
whetherIP
the address and port number are written correctly. -
Second, check whether the context path is correct, for example
Project -> Properties -> MyElipse -> Web -> Web Context-root
, check whether the path name is written correctly. -
Finally, check that the file name is written correctly.
-
-
Web
Application not deployed -
Tomcat
web.xml
problems in the
If, your web
application has multiple jsp
pages, when you click on web
the application's virtual root directory, it may appear 404错误
.
At this point, you only need to modify Tomcat
the server web.xml
, as shown in the following code:
<!-- 将false修改为true -->
<init-param>
<param-name>list</param-name>
<param-value>false</param-value>
</init-param>
If you can't access the file in 6.0.18
version error
: always , you can make a copy of the file in .The requested resource () is not available.
/myapp/*.jsp
/ROOT
build.xml
copy
myapp
【Note】Replace everything in it ROOT
with myapp
, and it should be OK.
-
WEB-INF
There must be several fixed folders and files below-
web.xml
Theweb app
configuration file -
lib
The library files that shouldweb app
be used -
classes
Store the compiledservlet
-
Please pay attention to these names, I used to classes
write them as class
, and I have not solved the problem after checking for errors.
Therefore, you must be careful when writing these, or you will not waste more energy to check for errors.
- If you are running
servlet(.class)
a file instead of.jsp
a file, you need toweb.xml
add the following fields to :
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
Among them, TestServlet
change the file name you want to run.
[Note] web.xml
is WEB-INF
the following.
struts.xml
Configuration error
Maybe your Action
value is wrong, or the link URL
is wrong.
For example, commons-lang3-3.1.jar
the file goes to WEB-INF/lib
the directory, struts2
the latest web
development package is as follows:
Of course, if yours is maven
a project, you can directly pom.xml
import jar
the package in it, as shown below:
<!--apache配置开始-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!--apache配置结束-->
- Check if multiple
java
services are started for the same project
Sometimes, if the previous service is not closed in the background, and you restart it, an error may be reported 404
.