0

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");


}

  }
Spectric
  • 30,714
  • 6
  • 20
  • 43
srgent
  • 41
  • 3
  • For a form to submit a value to a server, each input field needs to have a `name` attribute. For example: ``. Then access it using `request.getParameter("firstName")`. – andrewJames May 02 '21 at 00:45

1 Answers1

1

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>
Spectric
  • 30,714
  • 6
  • 20
  • 43