Program 1: Java Servlet program to implement a dynamic
HTML using servlet
//[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class post extends GenericServlet
{
public void service(ServletRequest request,
ServletResponse response) throws
ServletException,IOException
{
PrintWriter pw = [Link]();
Enumeration e = [Link]();
while([Link]() )
{
String pname=(String)[Link]();
[Link](pname);
String pvalue=[Link](pname);
[Link](pvalue);
}
[Link]();
}
}
[Link]
<html>
<body>
<center>
<form name="form1" method ="post"
action="[Link]
>
<table>
<tr>
<td><B>Username</td>
<td><input type=textbox name="username"></td>
</tr>
<tr>
<td><B>Password</td>
<td><input type=textbox name="password"></td>
</tr>
</table>
<input type=submit value="submit">
</form>
</body>
</html>
Program 2: Java Servlet program to download a file and
display it on the screen
Program:
import [Link].*;
import [Link].*;
import [Link].*;
public class FileDownloads extends HttpServlet
{
public void doGet(HttpServletRequest
req,HttpServletResponse res)
throws ServletException, IOException {
try {
PrintWriter out = [Link]();
[Link]("text/html");
[Link]("<html><head>Download a File
Example</head></html>");
[Link]("<body>");
[Link]("<pre><a href=\"[Link]
click here </a><br> ");
[Link]("</body></html>");
[Link]();
}
catch(Exception e) {
[Link]([Link]());
}
}
}
Program 3: Java Servlet program to implement and
demonstrate get( ) and post( ) methods
<html>
<body>
<center>
<form name="form1" method=post
action="[Link]
ost">
<B>color:</B>
<select name="color"size="1">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
</select>
<br><br>
<input type=submit value="submit">
</form>
</body>
</html>
import [Link].*;
import [Link].*;
import [Link].*;
public class colorpost extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException
{
String color=[Link]("color");
[Link]("text/html");
PrintWriter pw=[Link]();
[Link]("the selected color is:");
[Link](color);
[Link]();
}
}
import [Link].*;
import [Link].*;
import [Link].*;
public class color extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException
{
String color=[Link]("color");
[Link]("text/html");
PrintWriter pw=[Link]();
[Link]("the selected color is:");
[Link](color);
[Link]();
}
}
<html>
<body>
<center>
<form name="form1"
action="[Link]
>
<B>color:</B>
<select name="color"size="1">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
</select>
<br><br>
<input type=submit value="submit">
</form>
</body>
</html>
Program 4: Java JSP program to print 10 even and 10 odd
numbers
<html>
<head>
<title>Scriptlet for Even odd nos print</title>
</head>
<body>
<h1>10 Even and 10 odd numbers</h1>
<%
[Link]("<b>10 Even numbers starting from 1 are</><br>");
for (int i=1;i <= 20; i++){
if(i%2==0 )
{
[Link]("<br><b> " + i + "</b>");
}
}
[Link]("<br><br><b> 10 Odd Nos starting from 1are
</b><br>");
for (int i=1;i <= 20; i++){
if(i%2!=0 )
{
[Link]("<br><b> " + i + "</b>");
}
}
%>
</body>
</html>
Program 5: Java JSP program to implement verification of a
particular user login and display a welcome page
[Link]
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=UTF-8">
<title>JSP Page</title>
</head>
<body><h1>Hello Users</h1>
<form method="post" action="[Link]">
<p>please enter your username:
<input type="text" name="username">
<br>and password:
<input type="password" name="password">
</p>
<p><input type="submit" value="login"></p>
</form>
</body>
</html>
[Link]
<html>
<head>
<title></title>
<meta http-equiv="Content-Type"
content="text/html;charset=UTF-8">
</head>
<body>
<h1>Forward action test :Login Successful</h1>
<%= [Link]("username") %>
</body>
</html>
Program 6: Write a Java JSP program to find the factorial of
a given number
<html>
<head>
<title>calculate factorial</title>
</head>
<body><p> </p>
<table border="1" cellpadding="0"
cellspacing="0" width="35%"
align="center">
<tr>
<td width="100%">
<p align="center"><b>Calculate factorial</b></p>
<form method="POST" action="[Link]">
<p align="center"><b>Enter Number:</b>
<input type="text" name="num" size="20">
<input type="submit" value="Submit" name="B1"></p>
<p> </p>
</form>
<p> </td>
</tr>
</table></body></html>
JSP file: [Link]
<HTML>
<HEAD>
<TITLE>Using Recursion</TITLE>
</HEAD>
<BODY>
<font size="6" color ="#000080">
Calculate factorial Using Recursion</font><br>
<%
int num=[Link]([Link]("num"));
%>
<%!
int factorial(int n)
{
if (n == 1) {
return n;
}
else {
return n * factorial(n - 1);
}
}
%>
<%
[Link]("The factorial of " +num+" is : " +
factorial(num));
%>
</BODY>
</HTML>
Output:
Program 7: Java Servlet program to implement sendRedirect
method (using HTTP Class)
//[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class DS extends HttpServlet
{
public void doGet(HttpServletRequest
req,HttpServletResponse
res)throws ServletException, IOException
{
PrintWriter out = [Link]();
[Link]("<html><head>SendRedirect()
Example</head></html>");
[Link]("<body><center><font color=red size =
34>MyWeb </ font>");
[Link]("<pre><a href=SS>Search</a> <a
href=DS>greetings</a> </pre>");
[Link]("<table border = 1, width = 100 >");
[Link]("<tr><th>SNO</th><th>[Link]</th><th>Quality
</th></tr>" );
[Link]("<tr><td>1</td><td>J2SE</td><td>10</td></tr>"
);
[Link]("<tr><td>2</td><td>J2EE</td><td>10</td></tr>"
);
[Link]("</table></center></body></body>");
}
}
//[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class SS extends HttpServlet
{
public void doGet(HttpServletRequest
req,HttpServletResponse
res)throws ServletException, IOException
{
[Link]("[Link]
ml");
}
}
Program 8: Java Servlet program to implement sessions
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SessionTracker extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse
res)throws ServletException, IOException
{
[Link]("text/html");
PrintWriter out = [Link]();
HttpSession session = [Link](true);
Integer count = (Integer) [Link]("count");
if (count == null)
{ count = new Integer(1); }
else
{count = new Integer([Link]() + 1); }
[Link]("count", count);
[Link]("<html><head><title>SessionSnoop</title></head
>");
[Link]("<body><h1>Session Details</h1>");
[Link]("You've visited this page " + count +
(([Link]()== 1) ? " time." : "
times.") + "<br/>");
[Link]("<h3>Details of this session:</h3>");
[Link]("Session id: " + [Link]() + "<br/>");
[Link]("New session: " + [Link]() + "<br/>");
[Link]("Timeout: " + [Link]() +
"<br/>");
[Link]("Creation time: " + new
Date([Link]()) + "<br/>");
[Link]("Last access time: " + new
Date([Link]()) + "<br/>");
[Link]("</body></html>");
}
}
Program 9: Java JSP program which uses <jsp:plugin> tag to
run applet
import [Link].*;
import [Link].*;
[Link]
public class applet extends Applet
{
int x1=0, y1=100, x2, y2;
public void init()
{
setBackground ( [Link] );
}
public void paint(Graphics g)
{
if(x1 > 300)
x1=0;
[Link](x1, y1, 30, 30);
x1++;
try{
[Link](100);
} catch(Exception e){}
repaint();
}
}
<html>
<title> Plugin example </title>
<body bgcolor="white">
<h3> Applet : </h3>
<jsp:plugin type="applet" code="[Link]"
codebase="applet" width="300" height="400" >
<jsp:fallback>
Plugin tag OBJECT or EMBED not supported by browser.
</jsp:fallback>
</jsp:plugin>
<p>
<h4>
<font color=red>
Jsp plugin example
</font>
</h4>
</body>
</html>
Test Data:
[Link]