将Java servlet连接到JSP

因此,我正在使用Google App Engine和Cloud SQL开发Java Web应用程序。我在创建注册页面时遇到了麻烦。我的JSP文件中有一个用于注册页面的表单,我希望它调用我的servlet的doPost方法。显然,由于某种原因没有发生。我没有收到任何错误,但是我的数据库没有被更新。我正在遵循此指南:https://cloud.google.com/appengine/docs/standard/java/building-app/handling-form-data

我的JSP中的表单是:

               <form method="POST" action="StudentSignUpServlet">
                    <div class="form-group">
                        <label for="first name"> First Name  </label>
                        <input type="text" class="form-control" name="firstName" id="firstName" aria-describedby="emailHelp" placeholder="ex: John">
                    </div>

                    <div class="form-group">
                        <label for="last Name"> Last Name </label>
                        <input type="text" class="form-control" name="lastName" id="lastName" aria-describedby="emailHelp" placeholder="ex: Smith">
                    </div>

                    <div class="form-group">
                        <label for="age"> Age </label>
                        <input type="text" class="form-control" name="age" id="age" aria-describedby="emailHelp" placeholder="ex: 15">
                    </div>


                    <div class="form-group">
                        <label for="school"> School </label>
                        <input type="email" class="form-control" name="school" id="school" aria-describedby="emailHelp" placeholder="High School">
                    </div>

                    <div class="form-group">
                        <label for="student grade"> Grade </label>
                        <input type="text" class="form-control" name="grade" id="grade" placeholder="ex: 11">
                    </div>

                    <div class="form-group">
                        <label for="emailInput"> Email address </label>
                        <input type="email" class="form-control" name="emailInput" id="emailInput" aria-describedby="emailHelp" placeholder="ex: johnSmith@iWorks.com">
                    </div>


                    <div class="form-group">
                        <label for="Password Input"> Password </label>
                        <input type="password" class="form-control" name="passInput" id="passInput" placeholder="Password">
                    </div>

                    <div class="form-group">
                        <label for="Confirm password"> Confirm Password </label>
                        <input type="password" class="form-control" id="confirmPass" placeholder="Confirm Password">
                    </div>

                    <div class="">
                        <a role="button" type="submit" class="btn btn-outline-primary">Sign Up</a>
                    </div>
                    <br>

                </form>

我的servlet类是:

@WebServlet(name = "StudentSignUpServlet",value = "/StudentSignUpServlet")
public class StudentSignUpServlet extends HttpServlet {

  @Override
  public void doPost(HttpServletRequest req,HttpServletResponse resp)
      throws IOException {

    String firstName = req.getParameter("firstName");
    String lastName = req.getParameter("lastName");
    String school = req.getParameter("school");
    String emailInput = req.getParameter("emailInput");
    String passInput = req.getParameter("passInput");
    String age = req.getParameter("age");
    String grade = req.getParameter("grade");


    // Reuse the pool that was created in the ContextListener when the Servlet started.
    DataSource pool = (DataSource) req.getServletContext().getattribute("my-pool");
    // [START cloud_sql_mysql_servlet_connection]
    // Using a try-with-resources statement ensures that the connection is always released back
    // into the pool at the end of the statement (even if an error occurs)
    try (Connection conn = pool.getconnection()) {


      PreparedStatement studentStmt = conn.prepareStatement(
          "INSERT INTO students (user_name,password,first_name,last_name,age,school,grade) VALUES (?,?,?);");
      studentStmt.setString(1,emailInput);
      studentStmt.setString(2,passInput);
      studentStmt.setString(3,firstName);
      studentStmt.setString(4,lastName);
      studentStmt.setString(5,age);
      studentStmt.setString(6,school);
      studentStmt.setString(7,grade);

      // Finally,execute the statement. If it fails,an error will be thrown.
      studentStmt.execute();

    } catch (SQLException ex) {
      // If something goes wrong,handle the error in this section. 
      resp.setStatus(500);
      resp.getWriter().write("Unable to successfully sign up!");
      System.out.println("Unable to successfully sign up!");// This is not being printed
    }
    // [END cloud_sql_mysql_servlet_connection]

    resp.setStatus(200);
    resp.getWriter().printf("Successfully signed up!");
    System.out.println("Successfully signed up!");// Neither is this being printed for some reason
  }

我试图更新我的web.xml文件以包括下面的代码段,但这没有帮助。

<servlet>
    <servlet-name>StudentSignUpServlet</servlet-name>
    <servlet-class>iworks.StudentSignUpServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>StudentSignUpServlet</servlet-name>
    <url-pattern>/StudentSignUpServlet</url-pattern>
  </servlet-mapping>

任何帮助将不胜感激!

cggsj 回答:将Java servlet连接到JSP

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3155870.html

大家都在问