Android客户端连接Servlet遇FileNotFoundException问题求助
Hey there, let's tackle this issue step by step. Since your HelloWorld Servlet works perfectly, we can eliminate basic connectivity problems between your Android app and GlassFish. The problem is almost certainly tied to the configuration, deployment, or access path of your GetList Servlet. Here's how to diagnose and fix it:
1. Double-Check web.xml Servlet Mapping
First, compare the web.xml entries for HelloWorld and GetList to spot any discrepancies. Servlet paths are case-sensitive, and even a tiny typo will cause a 404 (which Android translates to FileNotFoundException).
Example Correct Configuration
For HelloWorld (which works):
<servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>com.yourpackage.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping>
For GetList, verify:
- The
servlet-classuses the full, correct package path of your GetList Servlet class - The
url-patternmatches exactly what you're using in your Android code (e.g., if your app calls/GetList, the pattern shouldn't be/getlistor/GetListServlet) - No extra slashes or typos in either the servlet name or mapping
2. Verify GetList Servlet Deployment Status
Sometimes NetBeans might fail to compile or package the GetList Servlet correctly. Let's check:
- Check compiled class files: Navigate to your GlassFish domain's deployment directory (usually
glassfish/domains/domain1/applications/[your-web-app-name]/WEB-INF/classes/) and confirm the GetList Servlet's.classfile exists in the correct package folder. - Check NetBeans build logs: Look for compilation errors in the NetBeans output window—if GetList failed to compile, it won't be included in the WAR file.
- Check GlassFish server logs: Open
glassfish/domains/domain1/logs/server.logand search for entries related to GetList. If there was a deployment error (like a missing dependency or invalid class), it'll show up here.
3. Validate Android Client Request URL
Even a small mistake in the URL can break things. Compare the URL used for HelloWorld and GetList:
- Ensure the base URL (IP address, port, web app context path) is identical. For example:
- HelloWorld URL:
http://192.168.1.100:8080/MyWebApp/HelloWorld - GetList URL should be:
http://192.168.1.100:8080/MyWebApp/GetList
- HelloWorld URL:
- Avoid using
localhostin the Android emulator—replace it with10.0.2.2(for Android Studio emulators) or your host machine's actual IP address (since you got HelloWorld working, this might already be correct, but double-check anyway).
4. Check for File Operations Inside GetList Servlet
Wait—sometimes the FileNotFoundException isn't about the Servlet itself, but a file the Servlet is trying to read. If your GetList code accesses local files (like a data file, config file, or resource), make sure:
- The file path is correct (use relative paths based on the web app root, e.g.,
getServletContext().getResourceAsStream("/WEB-INF/data/list.txt")instead of hardcoded absolute paths) - The file is included in your WAR file (check NetBeans project structure to ensure it's marked for deployment)
- Look at the full stack trace in your Android error log—if the exception originates from a line like
new FileInputStream("myfile.txt"), that's the culprit, not the Servlet's accessibility.
5. Test GetList Directly via Browser
The quickest way to isolate server-side vs client-side issues is to access GetList directly from your desktop browser. Enter the full URL (e.g., http://localhost:8080/MyWebApp/GetList):
- If you get a 404 error: The problem is on the server side (mapping or deployment issues)
- If the Servlet loads correctly: The issue is in your Android code (check request method—e.g., HelloWorld uses GET but GetList requires POST, or missing request headers)
Final Tip
If you're using annotations instead of web.xml for Servlet mapping (e.g., @WebServlet("/GetList")), make sure the annotation is present on your GetList Servlet class and the path matches your Android request.
内容的提问来源于stack exchange,提问作者Luke9321

