In this tutorial how to Read the Content of a Form using getParameterNames() method in Advance Java (J2EE) is shown.
Code:
index.html
<html>
<head>
<title>Student Registration Form</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form action=”StudentRegistration” method=”POST”>
<center>
<table border=”1″>
<th colspan=”2″><center><h1>Student Registration Form</h1></center></th>
<tr><td>Name: </td><td><input type=”text” name=”nm”></td></tr>
<tr><td>Address: </td><td><textarea cols=”50″ rows=”5″ name=”add”></textarea></td></tr>
<tr><td>Contact No: </td><td><input type=”text” name=”cno”></td></tr>
<tr><td>Email Id: </td><td><input type=”text” name=”eid”></td></tr>
<tr><td>Date of Birth: </td><td><input type=”text” name=”dt”></td></tr>
<tr><td colspan=”2″><center><input type=”submit” value=”Submit”></center></td></tr>
</table>
</center>>
</form>
</body>
</html>
StudentRegistration.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
public class StudentRegistration extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(“text/html;charset=UTF-8”);
try (PrintWriter out = res.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println(“<!DOCTYPE html>”);
out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Servlet StudentRegistration</title>”);
out.println(“</head>”);
out.println(“<body>”);
out.println(“<h1 align=center>Student Registration</h1> \n”+”<table border=1 align=center> \n”+ “<tr> \n”+”<th>Parameter Name <th>Parameter Value(s)”);
Enumeration paramNames=req.getParameterNames();
while(paramNames.hasMoreElements())
{
String paramName=(String)paramNames.nextElement();
out.println(“<tr><td>”+paramName+”</td>”);
String[] paramValues=req.getParameterValues(paramName);
for(String pv:paramValues)
{
out.println(“<td>”+pv+”</td></tr>”);
}
}
out.println(“</table>”);
out.println(“</body>”);
out.println(“</html>”);
}
}
}