JDBC/JSP新手求助:在线巴士预订系统页面跳转失败
Hey there! As someone who's fumbled through early JSP and JDBC projects myself, I totally get how frustrating this kind of page jump snag can be. Let's break down the most common fixes step by step:
1. First, Audit Your "Start Booking" Button in index.jsp
This is almost always the root of the problem. Let's cover the two most common button setups:
If you're using a form submission:
- Double-check the
<form>tag'sactionattribute. Ifindex.jspandregistration.jsplive in the same folder, the action should beaction="registration.jsp". Ifregistration.jspis in a subdirectory (likepages/registration.jsp), adjust the path accordingly. Watch out for leading slashes—action="/registration.jsp"points to the server root, not your project's root, which is a super common rookie mistake. - Make sure your button is a submit button: use
<button type="submit">Start Booking</button>or<input type="submit" value="Start Booking">. A plain<button>withouttype="submit"won't trigger the form by default.
If you're using a link-styled button:
- Verify the
<a>tag'shreffollows the same path rules. For example:<a href="registration.jsp" style="padding: 8px 14px; background: #2563eb; color: white; text-decoration: none;">Start Booking</a>
2. Test registration.jsp Directly
Type the full URL of registration.jsp into your browser (e.g., http://localhost:8080/your-project-name/registration.jsp). If it doesn't load:
- A 404 error means your file path is wrong—double-check where you placed
registration.jspin your project structure. - A 500 error means
registration.jsphas syntax bugs (like malformed JSP tags, missing imports, or broken Java code). Fix those first, then try the jump again.
3. Check Your Server Logs for Clues
Pull up your Tomcat (or whatever server you're using) logs. Look for:
- 404 entries: confirms a path issue.
- 500 entries: points to errors in
registration.jspor server-side logic. - Any warnings related to your app's deployment.
4. Prep for Database Storage (Once the Jump Works)
Since you mentioned saving form data later, here's a quick heads-up to avoid future headaches:
- Separate logic from view: Don't cram all your JDBC code into JSPs. Use a Servlet to handle form submissions, validate inputs, and talk to the database. This keeps your code clean and way easier to debug.
- Use
PreparedStatementinstead ofStatement: It prevents SQL injection and is more reliable. Example snippet for inserting data:// Inside your Servlet's doPost method String passengerName = request.getParameter("fullName"); String email = request.getParameter("email"); // Assume you have a valid DB connection (use connection pools for production!) String insertSql = "INSERT INTO passengers (full_name, email) VALUES (?, ?)"; PreparedStatement pstmt = conn.prepareStatement(insertSql); pstmt.setString(1, passengerName); pstmt.setString(2, email); pstmt.executeUpdate(); - Reuse DB connections: Use a connection pool (like HikariCP) instead of creating a new connection every time—this avoids resource leaks and boosts performance.
Quick Example of a Working index.jsp Form
Start with this simple, correct setup to test:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Bus Booking Home</title> </head> <body> <h1>Welcome to Bus Booking System</h1> <form action="registration.jsp" method="POST"> <button type="submit">Start Booking</button> </form> </body> </html>
Get the jump working first, then build out the rest step by step. You've got this!
内容的提问来源于stack exchange,提问作者R. Kulkarni

