In this tutorial how to Read the Content of a Form using getParameterValues() method in Advance Java (J2EE) is shown.
Code:
index.html
<html>
<head>
<title>Patient Registration</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form action=”PatientRegistration” method=”POST”>
<center>
<table border=”1″>
<th colspan=”2″><h1> Patient Registration </h1></th>
<tr><td>Name: </td><td><input type=”text” name=”nm”></td></tr>
<tr><td>Date of Birth: </td><td><input type=”text” name=”dt”></td></tr>
<tr><td>Contact No: </td><td><input type=”text” name=”cno”></td></tr>
<tr><td>Address: </td><td><textarea rows=”5″ cols=”50″ name=”add”></textarea></td></tr>
<tr><td>Previous Medical History: </td><td><textarea rows=”5″ cols=”50″ name=”pmh”></textarea></td></tr>
<tr><td>Gender: </td><td><input type=”radio” name=”gen” value=”male”>Male<br>
<input type=”radio” name=”gen” value=”female”>Female</td></tr>
<tr><td>Allergy History: </td><td><input type=”checkbox” name=”ah” value=”solid”>Solid<br>
<input type=”checkbox” name=”ah” value=”liquid”>Liquid<br>
<input type=”checkbox” name=”ah” value=”others”>Others<br>
<input type=”checkbox” name=”ah” value=”none”>None</td></tr>
<tr><td>Habits: </td><td> <input type=”checkbox” name=”hb” value=”chewing”>Chewing<br>
<input type=”checkbox” name=”hb” value=”drinking”>Drinking<br>
<input type=”checkbox” name=”hb” value=”others”>Others<br>
<input type=”checkbox” name=”hb” value=”none”>None</td></tr>
<tr><td colspan=”2″><center><input type=”submit” value=”Submit”></center></td></tr>
</table>
</center>
</form>
</body>
</html>
PatientRegistration.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;
public class PatientRegistration extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(“text/html;charset=UTF-8”);
try (PrintWriter out = res.getWriter()) {
String nm=req.getParameter(“nm”);
String dt=req.getParameter(“dt”);
String cno=req.getParameter(“cno”);
String add=req.getParameter(“add”);
String pmh=req.getParameter(“pmh”);
String gen=req.getParameter(“gen”);
String ah[]=req.getParameterValues(“ah”);
String hb[]=req.getParameterValues(“hb”);
/* 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 PatientRegistration</title>”);
out.println(“</head>”);
out.println(“<body>”);
out.println(“<h1>Patient Details</h1>”);
out.println(“<p>Name: “+nm+”</p>”);
out.println(“<p>Date of Birth: “+dt+”</p>”);
out.println(“<p>Contact No: “+cno+”</p>”);
out.println(“<p>Address: “+add+”</p>”);
out.println(“<p>Patient Medical History: “+pmh+”</p>”);
out.println(“<p>Gender: “+gen+”</p>”);
out.println(“<p>Allergy History: </p>”);
for(String a:ah)
{
out.println(a+”<br>”);
}
out.println(“<p>Habits: </p>”);
for(String h:hb)
{
out.println(h+”<br>”);
}
out.println(“</body>”);
out.println(“</html>”);
}
}
}