0

I'm trying to save a form info to a mysql database using J2EE and eclipse in Windows 10. I'm having this problem request.getParameter() returns null even though i used the same name in the form. The server shows no errors and I don't know what to do next. Here's my HTML Code.

   <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
        <!DOCTYPE html>
   <html>
   <head>
   <meta charset="UTF-8">
   <title>Insert title here</title>
   </head>
   <body>
   <center>
    <h1>Don</h1>
        <form method="post" action="Fund" enctype="multipart/form-data">
            <table border="0">
                <tr>
                    <td> Titre :  </td>
                    <td><input type="text" name="titre" size="50"/></td>
                </tr>
                <tr>
                    <td> Cause : </td>
                    <td><input type="text" name="cause" size="50"/></td>
                </tr>
                 <tr>
                    <td> Ammount : </td>
                    <td><input type="text" name="ammount" size="50"/></td>
                </tr>
                 <tr>
                    <td> Story: </td>
                    <td><input type="text" name="story" size="50"/></td>
                </tr>
                <tr>
                    <td> Photo: </td>
                    <td><input type="file" name="image" size="50"/></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="Save">
                    </td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

Here's the Fund.java code

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;


/**
 * Servlet implementation class Don
 */
@WebServlet("/Fund")
public class Fund extends HttpServlet {
    
    //database connection settings
    
    private String dbURL = "jdbc:mysql://localhost:3306/userdb";
    private String dbUser = "root";
    private String dbPass = "root";
    
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Fund() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String titre = request.getParameter("titre");
        String cause = request.getParameter("cause");
        String ammount = request.getParameter("ammount");
        String story = request.getParameter("story");
        
        System.out.println(titre);
        
        InputStream inputStream = null;
        
        Part filePart = request.getPart("image");
        
        System.out.println(filePart);
        if(filePart != null) {
            System.out.println("here");
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());
            
            inputStream = filePart.getInputStream();
        }
        
        Connection conn = null;
        String message = null;
        
        try {
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL,dbUser,dbPass);
            
            String querySetLimit = "SET GLOBAL max_allowed_packet=104857600;";  // 10 MB
            Statement stSetLimit = conn.createStatement();
            stSetLimit.execute(querySetLimit);
 
            
            String sql = "INSERT INTO don (title, cause, ammount, story, image) values (?,?,?,?,?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, titre);
            statement.setString(2, cause);
            statement.setString(3, ammount);
            statement.setString(4, story);
            
            if(inputStream != null) {
                statement.setBlob(5, inputStream);
            }
            
            int row = statement.executeUpdate();
            if(row > 0) {
                message ="File uploaded and saved into database";
            }
            
        } catch (SQLException e) {
            message = "ERROR: " + e.getMessage();
            e.printStackTrace();
        }finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            request.setAttribute("Message",message);
            
            getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
        }

    }
}
    
    

Please help me out I don't know what to do thank you

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Check your spelling. Clearly you have misspelt a parameter name omewhere. In this post you have misspelt `ammount` three times, for example. Is this the real code? – user207421 May 11 '21 at 00:28
  • The server does not care about the spelling of a word, as long as it is the same in the posted data and the server code - posting "ammmaound=123" and retrieving is with `request.getParameter("ammmaound")` will work. (This does not mean that spelling is not important, just not apparently the cause of the issue.) @OP: It seems you debugged the server code, is that right? Can you actually see the servlet being invoked? What about the browser console - is the browser actually sending the values you expect? Any special configuration (server, filters, web.xml) that might interfere? – Nikos Paraskevopoulos May 11 '21 at 07:41
  • One more thing: I see you are sending a file in the post; check [this](https://stackoverflow.com/questions/47027116/uploading-files-and-getting-parameters-in-java-webservlet) and most importantly [this](https://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet). – Nikos Paraskevopoulos May 11 '21 at 07:48
  • @NikosParaskevopoulos The mis-spelling strongly suggests that the OP is careless about spelling, which must be consistent between client and server. – user207421 May 11 '21 at 08:37
  • FYI: the term "J2EE" has been renamed to another term in 2006 and the HTML
    tag was deprecated in 1998. Please make sure you're using up to date resources to learn web stuff.
    – BalusC May 11 '21 at 11:04

0 Answers0