Simple program to demonstrate accessing
form data in servlets. Factorial
of a number
// Form to submit number to server
<!--
html forms -->
<html>
<head>
<title>HTML
Forms and Servlets</title>
<style>
label
{
font-family:vivaldi;
font-size:x-large;
letter-spacing:2px;
line-height:40px;
}
</style>
</head>
<body>
<form method="GET"
action="Fact">
<center>
<h2>HTML and
Servlets</h2></br>
<label>Enter
Number </label>
<input type="text"
Name="Num" tabindex=1/>
</br>
</br>
<button class="btn" type="button "
>Calculate!!! </button>
</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 Fact 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();
int
N = Integer.parseInt(req.getParameter("Num"));
int
f = 1;
//writing html in the stream
pw.println("<html><body>");
for(int i=1;i<=N;i++)
f = f*i;
pw.println("<H2>Factorial of "+N+" is "+ f
+"</H2>");
pw.println("</body></html>");
//closing the stream
pw.close();
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu