简体   繁体   中英

request.getParameter returns null in JSP

Form.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form</title>
<body bgcolor="#FFFFFF" text="#000000">

<h1>Please enter your details</h1>

<form name="RegistrationForm" action="NewUser" method="post">
    <table cellspacing="5" cellpadding="5" border="1">
        <tr>
            <td align="right">First Name:</td>
            <td><input type="text" name="NewFirstName"></td>
        </tr>
        <tr>
            <td align="right">Last Name:</td>
            <td><input type="text" name="NewLastName"></td>
        </tr>
        <tr>
            <td align="right">Email Address:</td>
            <td><input type="text" name="EmailAddress"></td>
        </tr>
        <tr>
            <td align="right">Phone Number:</td>
            <td><input type="text" name="Phone Number"></td>
        </tr>
        <tr>
            <td align="right">Semester</td>
            <td><input type="text" name="Semester"></td>
        </tr>

    </table>
    <input type="submit" value="Submit">
</form>

</body>
</html>

NewUser.java(Servlet Class)

package com.seria.quiz;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

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

/**
 * Servlet implementation class NewUser
 */
@WebServlet("/NewUser")
public class NewUser extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewUser() {
        super();
        // 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
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        Connection conn = null;

        try {
            String FirstName = request.getParameter("firstName");
            System.out.println("Your firstname: " + FirstName);
            String LastName = request.getParameter("lastName");
            System.out.println("Your LastName: " + LastName);
            String Emailid = request.getParameter("email");
            String PhoneNumber = request.getParameter("phoneNumber");
            String Semester = request.getParameter("semester");

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/seriaquiz", "root", "root");
            PreparedStatement pst = (PreparedStatement) conn
                    .prepareStatement("insert into formdetails(firstName,lastName,email,phoneNumber,semester) values(?,?,?,?,?)");// try2
                                                                                                                                    // the
                                                                                                                                    // name
            pst.setString(1, FirstName);
            System.out.println("Your firstname1: " + FirstName);
            pst.setString(2, LastName);
            System.out.println("Your LastName1: " + LastName);
            pst.setString(3, Emailid);
            pst.setString(4, PhoneNumber);
            pst.setString(5, Semester);

            int i = pst.executeUpdate();
            String msg = " ";
            if (i != 0) {
                msg = "Record has been inserted";
                pw.println("<font size='6' color=blue>" + msg + "</font>");

            } else {
                msg = "failed to insert the data";
                pw.println("<font size='6' color=blue>" + msg + "</font>");
            }
            pst.close();
        } catch (Exception e) {
            pw.println(e);
        }

    }

}

As the title says request.getParameter is returning null every time. I put sysout statements after requestParameter, and it shows null value. Any help will be appreciated. Sorry for any inconvenience, I'm new here.

The parameter names in servlet need to match them in the JSP

so

String FirstName = request.getParameter("firstName");

should be

String FirstName = request.getParameter("NewFirstName");

您要输入错误的参数,您应该始终要求输入与HTML中所述相同的ID /名称

request.getParameter("NewFirstName");

From w3.org section 17.2 Controls

Users interact with forms through named controls .

A control's "control name" is given by its name attribute. The scope of the name attribute for a
control within a FORM element is the FORM element.

When a form is submitted for processing, some controls have their name paired with their current
value and these pairs are submitted with the form.


Example of control is your

<input type="text" name="NewFirstName">

So when the form gets submitted, it contains a control name=value eg

NewFirstName=YourFirstName

In your JSP, you should do access it using the the control name

String FirstName = request.getParameter("NewFirstName");


Reference: http://www.w3.org/TR/html401/interact/forms.html

Get the exact value of a form parameter try this....

String FirstName = request.getParameter("NewFirstName");

because ..

you specify name attribute by 'NewFirstName'...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM