Issue
request.getParameter("fname") and request.getParameter("lname") are returning a null value. I checked for typos but I can't find any. Help please.
This is the HTML code with posting to Java Servlet FirstServlet
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>One</title>
</head>
<body>
<form action="FirstServlet" method="post">
Enter FirstName: <input type="text" value="fname">
Enter LastName: <input type="text" value="lname">
<input type="submit" value="Next">
</form>
</body>
</html>
Java Servlet code here
package com;
import java.io.IOException;
import dto.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class FirstServlet
*/
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FirstServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
HttpSession session= request.getSession();
user.setFname(request.getParameter("fname"));
user.setLname(request.getParameter("lname"));
session.setAttribute("user", user);
response.sendRedirect("two.html");
}
}
Solution
It should be name
, not value
.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>One</title>
</head>
<body>
<form action="FirstServlet" method="post">
Enter FirstName: <input type="text" name="fname">
Enter LastName: <input type="text" name="lname">
<input type="submit" value="Next">
</form>
</body>
</html>
Answered By - Spectric