Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null values getting in servlet using request.getParameter from jsp [duplicate]

Tags:

java

jsp

servlets

I have a jsp form as below,

<form action="../Registration" enctype="multipart/form-data">
    <label> First Name:</label>
    <input type="text" class="large-field" name="firstname">

    <label> Last Name:</label>
    <input type="text" class="large-field" name="lastname">

    <label> Gender:</label>
    <label class="radio">
    <input type="radio" name="gender"  value="Male">
        Male
    </label>
    <label class="radio">
    <input type="radio" name="gender" value="Female">
        FeMale
    </label>


    <label> Address :</label>
    <input type="text" class="large-field" name="address">


    <label> City:</label>
    <input type="text" class="large-field" name="city">


    <label> College:</label>
    <select class="large-field" name="college">
    <option value=""> --- Please Select --- </option>
    <option value="XYZ">XYZ</option>

    </select>

    <label> Branch:</label>
    <select class="large-field" name="branch">
        <option value=""> --- Please Select --- </option>
        <option value="ABC">ABC</option>
    </select>
    <br />
    <label> Mobile Number:</label>
    <input type="text" class="large-field" name="mobilenumber">
    <br />

    <label> Email_ID:</label>
    <input type="text" class="large-field" name="email">
    <br />
    <label> Password:</label>
    <input type="password" class="large-field"  name="password">
    <br />
    <label> Re-Enter Password:</label>
    <input type="password" class="large-field"  name="repassword">
    <br />
    <label> Profile Picture:</label>
    <input type="file" name="file">
    <br />

    <button class="btn btn-primary">Continue</button>
</form>

Then this calls my servlet which is coded as below:

package Client_Controller;

import CommonData.ComData;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

import java.util.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.RequestDispatcher;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;


public class Registration extends HttpServlet {

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

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            String f_name = request.getParameter("firstname");
            String l_name = request.getParameter("lastname");
            String gender = request.getParameter("gender");
            String address = request.getParameter("address");
            String city = request.getParameter("city");
            String college = request.getParameter("college");
            String branch = request.getParameter("branch");
            String mobile = request.getParameter("mobilenumber");
            String email = request.getParameter("email");
            String password = request.getParameter("password");
            String filePath = request.getParameter("file").toString();

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/table", "root", "root");
            s = con.createStatement();
            s.execute("insert into tblmembers(first_name,last_name,sex,address,city,college_name,branch,mobile,email_id,password) " +
                    "values('" + f_name + "','" + l_name + "','" + gender + "','" + address + "','" + city + "','" + college + "','" + branch + "','" + mobile + "'," + email + ",'" + password + "')");

            out.write("Suceess");
        } catch (Exception e) {
            out.write("" + e);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

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

        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

When data is inserted all values are getting null. I have tried lots of things but not working so need some help. I using bootstrap as front end.

like image 679
Ronak Joshi Avatar asked Apr 30 '14 14:04

Ronak Joshi


People also ask

Can we use request getParameter in JSP?

getParameter() – Passing data from client to JSP The familiarity of the getParameter() method in getting data, especially form data, from a client HTML page to a JSP page is dealt with here. The request. getParameter() is being used here to retrieve form data from client side.

What is the use of getParameter () getParameterNames () getParameterValues ()?

getParameter() method to get the value of a form parameter. getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getParameterNames() − Call this method if you want a complete list of all parameters in the current request.

What is getParameter () method explain with example?

getParameter is a function name in JSP which is used to retrieve data from an HTML/JSP page and passed into the JSP page. The function is designated as getParameter() function. This is a client-side data retrieval process. The full function can be written as request. getParameter().

What is getParameter () method?

getParameter(java.lang.String name) Returns the value of a request parameter as a String , or null if the parameter does not exist.


2 Answers

When using enctype="multipart/form-data" you cannot retrieve parameters using plain request.getParameter. Looks like you're using Servlet 2.5 or prior, so you need to parse the request using a third party library that process it by knowing the enctype. This can be easily done using Apache Common FileUpload library.

Note that this problem should not arise if using Servlet 3.0 or newer.

More info:

  • How to upload files to server using JSP/Servlet?

Also, you have to add method="POST" to your current form to make it work. You cannot upload files using GET request.

like image 163
Luiggi Mendoza Avatar answered Sep 22 '22 23:09

Luiggi Mendoza


Don't know why but it worked for me when I provided 'name' attribute to input text.

My Old code which returned null in Servlet:

<input id="closure" type="text" size="25"><a
                        href="javascript:NewCal('closure','ddmmyyyy')"><img
                            src="drawables/cal.gif" width="16" height="16" border="0"
                            alt="Pick a date"></a>

Just putting name="closure" worked for me. Now it perfectly returns value of this input text into servlet.

<input id="closure" name="closure" type="text" size="25"><a
                        href="javascript:NewCal('closure','ddmmyyyy')"><img
                            src="drawables/cal.gif" width="16" height="16" border="0"
                            alt="Pick a date"></a>

And I am getting value of this input text in Servlet as follows:

String closure = request.getParameter("closure");
like image 35
Kapil Jituri Avatar answered Sep 21 '22 23:09

Kapil Jituri