Farouk Taher Farouk Taher - 4 years ago 134
Java Question

request.getParameter() returns null in servlet except for one parameter

i'm trying to get the values from the form submitted, I tried everything I could find on the internet, but I still getting null when i'm using request.getParameter() it only returns the value of "bulkname" and all the other variables gives me null, I do not know why.

This form should take the values from the user and submit it to the servlet, so I can insert the values and the file into the database.

This is my code.

bulkCheckServlet.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package source;
 
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.time.LocalDateTime;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
 
/**
 *
 * @author Farouk
 */
@WebServlet(name = "bulkCheckServlet", urlPatterns = {"/bulkCheckServlet"})
@MultipartConfig
public class bulkCheckServlet extends HttpServlet {
 
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
String bulkname = request.getParameter("bulkname");
String quota = request.getParameter("quota");//Integer.parseInt(request.getParameter("quota"));
System.out.println(quota);
String provider = request.getParameter("provider");
 
InputStream inputStream = null; // input stream of the upload file
 
// obtains the upload file part in this multipart request
Part filePart = request.getPart("file");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
 
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
 
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
 
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/smsglobal", "root", "123456789");
 
// constructs SQL statement
String sql = "INSERT INTO smsbulk (id,quota,provider,bulkname,date,file) values (1,?,?,?,?,?);";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, quota);
statement.setString(2, provider);
statement.setString(3, bulkname);
statement.setString(4, LocalDateTime.now().toString());
System.out.println(sql);
 
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(5, inputStream);
}
 
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
 
// forwards to the message page
getServletContext().getRequestDispatcher("/Campaign.jsp").forward(request, response);
}
}
 
/**
  * Handles the HTTP <code>POST</code> method.
  *
  * @param request servlet request
  * @param response servlet response
  * @throws ServletException if a servlet-specific error occurs
  * @throws IOException if an I/O error occurs
  */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}


test.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
<form action="bulkCheckServlet" method="post" enctype="multipart/form-data">
Provider
<br/>
<input type="text" id="provider" name="provider"/>
<br/>
<br/>
Bulk Name
<br/>
<input type="text" name="bulkname" id="bulkname"/>
<br/>
<br/>
Quota
<br/>
<input type="text" name="quota" id="quota"/>
<br/>
<br/>
File
<br/>
<input type="file" name="file" id="file"/>
<br/>
<input type="submit" value="Submit" />
<br/>
</form>
</body>
</html>

Answer Source

Replace your servlet with following code.May this fully help you.

/*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package source;

    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.time.LocalDateTime;

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

    /**
     *
     * @author Farouk
     */
    @WebServlet(name = "bulkCheckServlet", urlPatterns = {"/bulkCheckServlet"})
    @MultipartConfig
    public class bulkCheckServlet extends HttpServlet {

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

                String bulkname = request.getParameter("bulkname");
                String quota = request.getParameter("quota");//Integer.parseInt(request.getParameter("quota"));
                System.out.println(quota);
                String provider = request.getParameter("provider");

                InputStream inputStream = null; // input stream of the upload file

                // obtains the upload file part in this multipart request
                Part filePart = request.getPart("file");
                if (filePart != null) {
                    // prints out some information for debugging
                    System.out.println(filePart.getName());
                    System.out.println(filePart.getSize());
                    System.out.println(filePart.getContentType());

                    // obtains input stream of the upload file
                    inputStream = filePart.getInputStream();
                }

                Connection conn = null; // connection to the database
                String message = null;  // message will be sent back to client

                try {
                    // connects to the database
                    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/smsglobal", "root", "0168308330");

                    // constructs SQL statement
                    String sql = "INSERT INTO smsbulk (id,quota,provider,bulkname,date,file) values (?,?,?,?,?,?);";
                    PreparedStatement statement = conn.prepareStatement(sql);
                    statement.setString(1, quota);
                    statement.setString(2, provider);
                    statement.setString(3, bulkname);
                    statement.setString(4, LocalDateTime.now().toString());
                    System.out.println(sql);

                    if (inputStream != null) {
                        // fetches input stream of the upload file for the blob column
                        statement.setBlob(5, inputStream);
                    }

                    // sends the statement to the database server
                    int row = statement.executeUpdate();
                    if (row > 0) {
                        message = "File uploaded and saved into database";
                    }
                } catch (SQLException ex) {
                    message = "ERROR: " + ex.getMessage();
                    ex.printStackTrace();
                } finally {
                    if (conn != null) {
                        // closes the database connection
                        try {
                            conn.close();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }
                    // sets the message in request scope
                    request.setAttribute("Message", message);

                    // forwards to the message page
                    getServletContext().getRequestDispatcher("/Campaign.jsp").forward(request, response);
                }
        }

        /**
         * Handles the HTTP <code>POST</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download