1

I need to get value of dropdown list, radio button and a check box. I am using request.getParameter() method but it's returning null value.

JSP Code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form action="trial.jsp"  method="POST" enctype="multipart/form-data">
        <table>
            <tr>
                <td class="entry">
                    Highest Qualification :
                </td>
                <td class="entry">
                    <select name="mon">
                        <option value="01">01</option>
                        <option value="02">02</option>
                        <option value="03">03</option>
                        <option value="04">04</option>
                        <option value="05">05</option>

                    </select>
                    <input type="submit" value="Submit" name="submit" />
                </td>
            </tr>
            <tr>
                <td class="entry">
                    <input type="radio" name="gender" value="Male" />Male
                    <input type="radio" name="gender" value="Female" />Female
                </td>
            </tr>
        </table>
    </form>
   </body>
</html>

Servlet Code:

public class NewServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String bdate = null;
        bdate = request.getParameter("mon");
        System.out.print("This is" + bdate);
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Md Altaf
  • 13
  • 2
  • 7

2 Answers2

2

The problem is that your <form> has enctype="multipart/form-data". You should only add that attribute in <form> when trying to upload a file. Since in your current page you don't need to upload a file, just remove it and everything will work perfectly:

<form action="trial.jsp"  method="POST" >
    <table>
        <tr>
            <td class="entry">
    <!-- rest of your HTML code -->
</form>

In case you want/need to add file upload functionality to your current page, then please refer to How to upload files to server using JSP/Servlet? in order to solve these problems (I won't reinvent the wheel in this answer).

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • i have just given a part of code here, in my full jsp page, there is an option to upload an image, thats why i have used the multipart, but how to deal with that? – Md Altaf May 03 '14 at 17:54
  • Check the link I've posted at the bottom about **how to upload files to server using JSP/Servlet**. It is not for decoration purposes... – Luiggi Mendoza May 03 '14 at 18:10
1

For Dropdown List:

<form action="sample_servlet" method="post">
  <select name="item">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>
  <input type="submit" value="Submit">
</form>

In your sample_servlet, write like this request.getParameter("item"); or

if(request.getParameter("item")!=null)
{
   selectedItem=Integer.ParseInt(request.getParameter("item"));
}

For Radio Button:

In jsp your code maybe

<input type="radio" name="dish" value="Indian"> Indian</input>

in your servlet page

  String radio = request.getParameter("dish"); 
JohnRose
  • 409
  • 5
  • 10