-1

The following snippet submits a caption to the database. After filling in the text , i click submit but to my surprise always a null goes to the table. I have metioned the servlet code and the html design below:

<form method="post" action="Handler" enctype="multipart/form-data">
        <table>
            <tr>
                <td> <strong> Leave a caption </strong>  </td>
                <td> <input type="text" name="caption box" size="40" /></td>
            </tr>

            <tr colspan="2">
                <td> <input type="submit" value="submit caption"/> </td>
            </tr>
        </table>
    </form>

Servlet

String caption = request.getParameter("caption box"); // get the caption from the caption field
HandleCaption hc = new HandleCaption(caption,emailOfTheUser,fileName);
hc.SubmitCaptionToTheDatabase(); 

Class

public class HandleCaption {
private String Caption = null;
private String UserEmail = null;
private String NameOfThe = null;

public HandleCaption(String caption,String email,String filename) {
    Caption = caption;
    UserEmail = email;
    NameOfThe = filename;
}

public void SubmitCaptionToTheDatabase() {
    try {
        Context context = new InitialContext();
        DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/DS");
        Connection connection = ds.getConnection();
        String sqlQuery = "insert into CAPTIONS values ('" + UserEmail + "','" + NameOfThe + "','" + Caption + "')";
        PreparedStatement statement = connection.prepareStatement(sqlQuery);
        int x = statement.executeUpdate();
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

}

I tried printing the value the text-field returned in the servlet which even the printed null. Why the text-field returns null ?

saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

2

i'm not sure why this happens but has happened to me once or twice.. i your Caption class Handle Caption, declare it as

private String Caption = "";
private String UserEmail = "";
private String NameOfThe = "";

i know it looks like a dumb answer to the question as in constructor you are actually referring to the value that is passed but i have experienced this and thats the solution for me.please try and reply!!

Update: sorry for adressing the question wrongly its due to encoding type, will read about why its happening.. but just remove encoding type and it works..tried on sample code

Update : multipart/form-data encoded requests are indeed not by default supported by the Servlet API prior to version 3.0. The Servlet API parses the parameters by default using application/x-www-form-urlencoded encoding. When using a different encoding, the request.getParameter() calls will all return null.

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
Priyanshu Jha
  • 575
  • 5
  • 11
  • read the question. I told that the vaule is null before i send it to helper class – saplingPro Apr 27 '12 at 07:16
  • its Due to Enctype.. i still have to find the reason but i made a sample code.. you repove the encoding type from the form tag and it works..i copy pasted your code and made a sample program – Priyanshu Jha Apr 27 '12 at 07:40
  • yes now it works! But i have to use _multipart encoding_. Is there a way out ? – saplingPro Apr 27 '12 at 08:02
  • http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824 please refer to article. for fetching the value using this encoding you will need to add commons-fileupload.jar. the question has a beautiful answer.. please see to it – Priyanshu Jha Apr 27 '12 at 08:04
  • and if possible than please + the answer..!! – Priyanshu Jha Apr 27 '12 at 08:05
  • I did it that is the reason you are having 1 from -1 – saplingPro Apr 27 '12 at 08:10
1

Your name captionbox of field shouldn't contain space.

<input type="text" name="captionbox" size="40" />

remove spaces from it and also from servlet side.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • I dont think it is a problem ,will u pls elobarate? – Balaswamy Vaddeman Apr 27 '12 at 06:48
  • WHAT ! and why do you think that ? – saplingPro Apr 27 '12 at 06:50
  • 3
    And that's also the [W3C recommendation](http://www.w3.org/TR/html4/types.html#type-cdata): "`ID` and `NAME` tokens must begin with a letter (`[A-Za-z]`) and may be followed by any number of letters, digits (`[0-9]`), hyphens (`"-"`), underscores (`"_"`), colons (`":"`), and periods (`"."`)." – Maehler Apr 27 '12 at 06:50
  • Can you make sure that you are receiving the captain in servlet. try to print out it. also try to print the query which is being built before submitting it to database. – Adil Soomro Apr 27 '12 at 07:00
  • I wrote in my question that it reurns null – saplingPro Apr 27 '12 at 07:05
  • @Balaswamy: try not to use space and . in request variable. – Rakesh Juyal Apr 27 '12 at 07:11
  • 1
    @grassPro in accordance with my earlier comment, I am afraid you have just changed the request variable name from `caption box` to `captionbox` in your html but not in your servlet. I don't see any reason why you will get `null` provided your request variable should not have and . – Rakesh Juyal Apr 27 '12 at 07:14
  • @Rakesh Juyal I changed in servlet too, but the same thing. NULL – saplingPro Apr 27 '12 at 07:21
  • 1
    ALSO I AM UNABLE TO USE SPACEBAR IN THAT TEXT FIELD. I AM WORKING ON SOMEBODY'S ELSE DESIGN, WHO HAS USED LOTS OF JQUERY AND JAVASCRRIPT. WHEN I TAP FOR SPACE,INSTEAD THE PHOTOS ON THE PAGE MOVE FURTHER ! – saplingPro Apr 27 '12 at 07:24