Simple program to demonstrate accessing
form data in servlets.
// Form to submit data to server
<!-- html forms -->
<html>
<head>
<title>HTML Forms</title>
<style>
label
{
display:inline-block;
width:35%;
padding-left:100px;
text-align: left;
font-family:vivaldi;
font-size:x-large;
letter-spacing:2px;
line-height:40px;
font-weight:bold;
}
textarea { vertical-align: top; }
select { vertical-align: top; }
.btn {width: 100px; height: 30px;}
</style>
</head>
<body>
<form method="POST" action="parameterdata">
<fieldset>
<legend>Personal information : </legend>
<label>First Name : </label>
<input type="text" Name="FN" tabindex=1/>
<label>Last Name : </label>
<input type="text" Name="LN" tabindex=2/>
</fieldset>
<fieldset>
<legend>Login details : </legend>
<label>User Name : </label>
<input type="text" Name="UN" tabindex=2/>
<label>Password : </label>
<input type="password" Name="PWD" tabindex=5/>
<label>Mobile Number : </label>
<input type="number" Name="MN" tabindex=4/>
<label>Address : </label>
<textarea Name="ADR" rows="5" cols="25" tabindex=3/> </textarea>
<label>Gender : </label>
<input type="radio" NAME="GENDER" VALUE="M" />Male
<input type="radio" NAME="GENDER" VALUE="F"/>Female
<label>Hobbies : </label>
<input type="checkbox" NAME="CB1" VALUE = "S"/>Sports
<input type="checkbox" NAME="CB1" VALUE = "M"/>Music
<input type="checkbox" NAME="CB1" VALUE = "R"/>Reading
</fieldset>
<br/>
<center>
<button class="btn" type="submit" >Confirm </button>
<button class="btn" type="reset" >Clear </button>
<br/><br/>
</center>
</form>
</body>
</html>
// Servlet to access form data and generate
response to client browser
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class parameterdata extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
//setting the content type
res.setContentType("text/html");
//create the stream to write the data
PrintWriter pw=res.getWriter();
String Name = (String)req.getParameter("FN")
+" "+(String)req.getParameter("LN");
//writing html in the stream
pw.println("<html><body><h2>");
pw.println("<center>");
pw.println("User details<br/><hr/>");
pw.println("Name : "+Name+"<br/>");
pw.println("User Name : "+(String)req.getParameter("UN")+"<br/>");
pw.println("Password : "+(String)req.getParameter("PWD")+"<br/>");
pw.println("Mobile No. : "+(String)req.getParameter("MN")+"<br/>");
pw.println("Address : "+(String)req.getParameter("ADR")+"<br/>");
pw.println("Gender : "+(String)req.getParameter("GENDER")+"<br/>");
String H[] = req.getParameterValues("CB1");
pw.println("Hobbies selected : "+String.join(", ",H));
pw.println("</center>");
pw.println("</h2></body></html>");
//closing the stream
pw.close();
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu