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类)
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); } } }
如标题所示,request.getParameter每次都返回null。我将sysout语句放在requestParameter之后,它显示空值。任何帮助将不胜感激。抱歉给您带来不便,我是新来的。
Servlet中的参数名称需要在JSP中匹配它们
所以
String FirstName = request.getParameter("firstName");
应该
String FirstName = request.getParameter("NewFirstName");