本地J2EE Preview及Tomcat v7.0服务器报HTTP ERROR 503求助
Hey there! Let's figure out why you're hitting that HTTP 503 error when running your Servlet with both the J2EE Preview server and Tomcat 7. A 503 means the server can't handle your request right now—usually due to deployment issues, missing dependencies, or misconfiguration. Let's break down the fixes step by step:
1. Verify Your JDBC Driver is Properly Deployed
The most common culprit here is the MySQL JDBC driver not being accessible to the server:
- For the J2EE Preview server: Eclipse's built-in preview server only loads libraries from your project's
WEB-INF/libfolder. Make sure you've copied themysql-connector-java-x.x.x.jarfile into this directory (not just added it to your Build Path via Eclipse's "Add External JARs"). - For Tomcat 7: You can either place the driver jar in your project's
WEB-INF/lib(recommended for project-specific dependencies) or in Tomcat's ownlibdirectory. If you use the latter, ensure there's no conflicting driver version elsewhere.
2. Fix Your Servlet Mapping
A 503 can also happen if the server can't find your Servlet at all. You didn't share your mapping config, so let's cover both options:
Option A: Using web.xml (traditional approach)
Add this to your WEB-INF/web.xml file:
<servlet> <servlet-name>tryingServlet</servlet-name> <servlet-class>trying</servlet-class> </servlet> <servlet-mapping> <servlet-name>tryingServlet</servlet-name> <url-pattern>/trying</url-pattern> </servlet-mapping>
Option B: Using Annotations (Servlet 3.0+, supported by Tomcat 7)
Add the @WebServlet annotation directly to your Servlet class:
import javax.servlet.annotation.WebServlet; // ... other imports ... @WebServlet("/trying") public class trying extends HttpServlet { // ... your existing code ... }
Now you can access the Servlet via http://localhost:8080/your-project-name/trying (replace your-project-name with your actual project name).
3. Clean Up Your Database Code (Prevent Leaks & Debug Better)
Your current code doesn't close database connections or statements, which can lead to resource leaks that disrupt the server. Plus, you're only printing errors to the console—let's fix both:
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/trying") public class trying extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); try (Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root"); Statement myStmt = myConn.createStatement()) { String sql = "insert into user values ('bandiiii', '123')"; int rowsAffected = myStmt.executeUpdate(sql); System.out.println("Done! Rows inserted: " + rowsAffected); response.getWriter().write("Data inserted successfully!"); } catch( Exception exc) { exc.printStackTrace(); // Send error message to the browser so you can see what's wrong response.getWriter().write("Error inserting data: " + exc.getMessage()); } } }
- The try-with-resources syntax automatically closes
ConnectionandStatementwhen done, preventing leaks. - We added a response to the browser so you can see success/error messages directly on the page, not just the console.
4. Check Server Logs for Hidden Errors
If you're still getting 503s, dig into the server logs—they'll tell you exactly what's broken:
- For Tomcat 7 in Eclipse: Look in
workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/logs(thetmp0folder might be numbered differently). Checklocalhost.logandlocalhost_access_log.*.txtfor exceptions like missing drivers, incorrect database credentials, or Servlet initialization failures. - For J2EE Preview: Check Eclipse's "Console" tab—there should be logs output when the server starts or when your request fails.
Final Quick Checks
- Make sure your MySQL server is running (you can verify with
mysql -u root -pin the terminal). - Confirm your
usertable has exactly two columns matching the values you're inserting (bandiiiiand123), or adjust your SQL to specify columns explicitly (e.g.,insert into user(username, password) values ('bandiiii', '123')—this is safer anyway).
内容的提问来源于stack exchange,提问作者Udana Methmal

