gpt4 book ai didi

java - JSP中request.getParameter返回null

转载 作者:行者123 更新时间:2023-12-01 12:27:13 24 4
gpt4 key购买 nike

表单.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");

关于java - JSP中request.getParameter返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26231999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com