POST后如何修复空白页以将数据插入Java EE中的数据库

时间:2019-07-04 02:02:20

标签: java html jsp

我正在尝试使用Eclipse与tomcat 9.0在servlet ..中创建一个注册页面,单击提交按钮后它将返回空白页面。进入地址栏,我有http://localhost:8080/CamerShop/Inscrption(Servlet名称),我已经尝试了可用的步骤,但是没有走..请帮助...

我尝试了另一个数据库。没有好结果

inscription.jsp

<!-- row -->
            <div class="row">
                <form method="Post" action="Inscription" id="checkout-form"
                    class="clearfix">
                    <div class="col-md-6">
                        <div class="billing-details">
                            <p>
                                Êtes vous client ? <a href="localhost:8080/CamerShop/login">Connexion</a>
                            </p>
                            <div class="section-title">
                                <h3 class="title">Entrez Vos Informations</h3>
                            </div>
                            <div class="form-group">
                                <input class="input" type="text" name="nom"
                                    placeholder="Entrez votre nom...">
                            </div>
                            <div class="form-group">
                                <input class="input" type="text" name="login"
                                    placeholder="Entrez votre login...">
                            </div>
                            <div class="form-group">
                                <input class="input" type="password" name="password"
                                    placeholder="Entrez votre mot de passe...">
                            </div>
                            <div class="form-group">
                                <input class="input" type="email" name="email"
                                    placeholder="Entrez votre email...">
                            </div>
                            <div class="form-group">
                                <input class="input" type="tel" name="telephone"
                                    placeholder="Telephone">
                            </div>
                            <div class="form-group">
                                <input class="input" type="text" name="ville"
                                    placeholder="Ville">
                            </div>

                            <div class="form-group">
                                <input class="input" type="text" name="photo"
                                    placeholder="Photo de profil">
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-light" name="AddUser">Enregistrer</button>
                            </div>
                        </div>
                    </div>

                </form>
            </div>
            <!-- /row -->

Inscription.java(servlet)

package controleur;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import modele.ConnexionDb;

/**
 * Servlet implementation class InscriptionServlet
 */

public class InscriptionServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doGet(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("Tentative de recuperation Recuperation des données du formulaire");
        String nom = req.getParameter("nom");
        String login = req.getParameter("login");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        int telephone = Integer.parseInt(req.getParameter("telephone"));
        String ville = req.getParameter("ville");
        String profil = "client";
        String photo = req.getParameter("photo");
        System.out.println(nom + " " + login + " " + email);

        try {
            ConnexionDb db=new ConnexionDb();
            db.getConnection();

            Connection conx = ConnexionDb.getConnection();
            PreparedStatement pr = (PreparedStatement) conx.prepareStatement(
                    "INSERT INTO `utilisateur` (idUser, nom, login, password, email, telephone, ville, profil, photo) VALUES (default,?,?,?,?,?,?,?,?)");

            pr.setString(1, nom);
            pr.setString(2, login);
            pr.setString(3, password);
            pr.setString(4, email);
            pr.setInt(5, telephone);
            pr.setString(6, ville);
            pr.setString(7, profil);
            pr.setString(8, photo);

            pr.executeUpdate();
            pr.close();

            resp.sendRedirect("login");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Insertion impossible en bd "+ e);
        }

    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>CamerShop</display-name>
  <servlet>
    <servlet-name>InscriptionServlet</servlet-name>
    <servlet-class>controleur.InscriptionServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>InscriptionServlet</servlet-name>
    <url-pattern>/Inscription</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>controleur.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>AccueilServlet</servlet-name>
    <servlet-class>controleur.AccueilServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AccueilServlet</servlet-name>
    <url-pattern>/Accueil</url-pattern>
  </servlet-mapping>
   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

ConnexionDb

package modele;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;

public class ConnexionDb {
    private static String url = "jdbc:mysql://localhost/camershop";

    private static String driverName = "com.mysql.jdbc.Driver";

    private static String username = "root";

    private static String password = "";

    private static Connection con;

    public static Connection getConnection() {
        try {
            Class.forName(driverName);
            try {
                con = (Connection) DriverManager.getConnection(url, username, password);
            } catch (SQLException ex) {
                // log an exception. fro example:
                ex.printStackTrace();
                System.out.println("Erreur lors de la connexion à la base de donnée.");
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            // log an exception. for example:
            System.out.println("Driver SQL Non trouvé.");
        }
        return con;
    }
}


单击提交按钮后,我有空白页(http://localhost:8080/CamerShop/Insription),并且数据没有插入数据库。

1 个答案:

答案 0 :(得分:0)

在准备好的语句中动态设置参数,如下所示:

PreparedStatement pr = (PreparedStatement) conx.prepareStatement(
                    "INSERT INTO `utilisateur` (idUser, nom, login, password, email, telephone, ville, profil, photo) VALUES (?,?,?,?,?,?,?,?,?)");

            pr.setString(1,"default");
            pr.setString(2, nom);
            pr.setString(3, login);
            pr.setString(4, password);
            pr.setString(5, email);
            pr.setInt(6, telephone);
            pr.setString(7, ville);
            pr.setString(8, profil);
            pr.setString(9, photo);

            pr.executeUpdate();
            pr.close();

            resp.sendRedirect("login");