本地正常的Java Web项目部署至生产服务器后出现404错误求助
Let’s break down what might be going wrong here and fix it step by step:
1. Fix the Critical Typo in Your .htaccess File
First off, there’s a spelling mistake in your .htaccess that’s almost certainly throwing things off. You wrote:
SetHandler Jakar-servlet SetEnv JK_WORKER_NAME ajp13
Change Jakar-servlet to jakarta-servlet (correct spelling and standard lowercase convention):
SetHandler jakarta-servlet SetEnv JK_WORKER_NAME ajp13
Also, double-check that your server’s Apache setup has mod_jk enabled, and that the JK_WORKER_NAME matches the worker defined in your workers.properties file (it’s usually ajp13 by default, but worth confirming).
2. Verify Your Deployment Directory Structure
When you unpacked your WAR file into public_html, make sure the folder structure follows this pattern:
public_html/ └── Test/ ├── WEB-INF/ │ ├── classes/ │ │ └── com/ │ │ └── test/ │ │ └── Test.class │ ├── lib/ │ └── web.xml (if you’re using one) └── [any static files your app uses]
If the unpacked content is directly in public_html instead of a Test subfolder, your URL should drop the /Test/ part and become http:///~(myusername)/HelloWorld.
3. Confirm Context Path and Servlet Mapping
Your servlet uses @WebServlet("/HelloWorld"), which maps it to the root of your application’s context. So:
- If your app’s context path is
Test(the folder name inpublic_html), the URL you tried (http:///~(myusername)/Test/HelloWorld) is correct—but only if the server recognizesTestas a valid web application context. - Some shared hosting environments require deploying apps to a specific directory (like a
webappsfolder under your user account) instead ofpublic_html. Check your host’s guidelines to confirm the right deployment path.
4. Check File Permissions
Make sure the unpacked files and directories have the right permissions so the Tomcat/Apache process can read them:
- The
WEB-INFdirectory and its contents (classes,lib) should have read permissions for the server user (typicallywww-dataorapache). - Skip overly permissive permissions like
777, but ensure the server can access all necessary files.
5. Dig Into Server Logs for Clues
This is one of the most useful steps—check these logs to get specific error details:
- Tomcat’s
localhost.logandlocalhost_access_log.*(usually located intomcat/logsunder your user directory) - Apache’s
error_log(found inapache/logsor a similar location)
These logs will tell you if the server can’t locate the servlet, if there’s an AJP connection failure, or if the context path is misconfigured.
6. Ensure Tomcat’s AJP Connector is Enabled
Open your Tomcat’s server.xml file and confirm the AJP connector isn’t commented out:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
If it’s commented, uncomment it and restart Tomcat. Without this connector, Apache can’t forward requests to Tomcat via AJP.
内容的提问来源于stack exchange,提问作者Jack G.

