There are many ways to solve the error of The requested resource [/] is not available in the whole network

1. Reproduce the error

I remember that when I used ideato write Java weba project, Tomcatthe following errors were often caused by the configuration:

insert image description here

That is The requested resource [/] is not availablethe problem.

2. Analysis errors

It is catching up with the recent ChatGPTfire, so I use it ChatGPTto solve my problem, as follows:

insert image description here

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, ChatGPTgiven 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

  1. The download directory we found first tomcat, mine is in D:\Software\tomcat8.5, enter binthe directory, as shown in the following figure:

insert image description here

  1. Find and double-click startup.batthe file to start it tomcat, as shown in the figure below:

insert image description here

[Remarks] If you are using linuxa system (for example Ubuntu, Apple Computer, etc.), after terminalentering binthe directory in , enter it sh startup.shto start tomcat.

  1. TomcatAfter successful startup, can you see its default interface as follows:

insert image description here

If this interface appears, there tomcatis no problem.

  1. Click the drop-down box in the upper right corner, select Edit configuration, and create a new one Tomcat local server, as shown in the following figure:

insert image description here

  1. 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 Tomcatinstallation directory, there is a folder webappsunder the folder , as shown below:ROOT

insert image description here

insert image description here

Just select ROOTit, click okit, and Deploymentthe configuration is completed.

  1. last point Tomcatrun

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) .

insert image description here

On the Internet, it is said that the wrong URLpath will be reported if the path is changed randomly 404, and it is also said that deploymentthe path must be URLconsistent 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 404an 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 deploymentunder it Application context, as follows:

insert image description here

If you change it, URLit 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 contextwill 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 contextwhat 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 Mavencreate a new one Java Web, it will most likely be such a path structure:

insert image description here

Generally, this web.xmlconfiguration error causes 404the error.

First, Tomcatstart the server to see if the project home page is displayed normally:

  1. If it cannot be displayed normally, check web.xmlwhether your home page address is written correctly.

  2. If the home page is displayed normally, but the jump page cannot be displayed normally, it is likely that your formpath 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:

insert image description here

If you jump to a static page, check whether your actionpath 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.xmlto perform corresponding url_patternand mappingconfiguration, but such configuration is likely to make some mistakes, leading to 404the problems you encountered.

Therefore, I strongly recommend that you use annotations for configuration instead of configuration and web.xmlconfiguration .url_patternmapping

Assuming that I LoginServletconfigure this, I only need to LoginServletadd @WebServletannotations 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 formform will be submitted and redirected normally without 404errors.

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

  1. Directories cannot be referenced.

You can Eclipsecheck 包资源管理器(Package Explorer)the location where the file is stored.

Since the content under the META-INFand WEB-INFfolders 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
  1. URLInput error, the troubleshooting method is as follows:

    • First check URLwhether IPthe 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.

  2. WebApplication not deployed

  3. Tomcatweb.xmlproblems in the

If, your webapplication has multiple jsppages, when you click on webthe application's virtual root directory, it may appear 404错误.

At this point, you only need to modify Tomcatthe 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.18version error: always , you can make a copy of the file in .The requested resource () is not available. /myapp/*.jsp/ROOTbuild.xmlcopymyapp

【Note】Replace everything in it ROOTwith myapp, and it should be OK.

  1. WEB-INFThere must be several fixed folders and files below

    • web.xmlThe web appconfiguration file

    • libThe library files that should web appbe used

    • classesStore the compiledservlet

Please pay attention to these names, I used to classeswrite 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.

  1. If you are running servlet(.class)a file instead of .jspa file, you need to web.xmladd 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, TestServletchange the file name you want to run.

[Note] web.xmlis WEB-INFthe following.

  1. struts.xmlConfiguration error

Maybe your Actionvalue is wrong, or the link URLis wrong.

For example, commons-lang3-3.1.jarthe file goes to WEB-INF/libthe directory, struts2the latest webdevelopment package is as follows:

insert image description here

Of course, if yours is mavena project, you can directly pom.xmlimport jarthe package in it, as shown below:

<!--apache配置开始-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>
<!--apache配置结束-->
  1. Check if multiple javaservices 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.

Guess you like

Origin blog.csdn.net/lvoelife/article/details/129080248