Vous êtes sur la page 1sur 15

PROGRAM No.

1
Create a servlet called ThreeParams that reads form parameters named param1,
param2, and param3 and places their values in a bulleted list. Create an HTML
form that collects user input and sends it to this servlet. Sample is shown in
following figure
Use a package titled as you first name.

figure A

figure B

Convert your parameter to Integer format and use them to calculate the
simple interest considering tour parameters as principal, rate and time
respectively.

Solution:
Prerana Tokas

Page 1

Passing Parameters
index.html
<HTML>
<HEAD>
<TITLE>MCA 355 ECJ LAB PROGRAM No. 1</TITLE>
</HEAD>
<BODY>
<H1>PASSING PARAMETERS </H1><br>
<H3> FIRST HTML PAGE TO NAVIGATE!</H3>
<A HREF="input.html">Click here to input parameters!</A>
</BODY>
</HTML>

input.html
<HTML>
<HEAD>
<TITLE>Form Parameters</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">Form Parameters</H1>
<FORM ACTION="s" method="GET">
First Parameter: <INPUT TYPE="TEXT"
NAME="param1"><BR>
Second Parameter: <INPUT TYPE="TEXT"
NAME="param2"><BR>
Third Parameter: <INPUT TYPE="TEXT"
NAME="param2"><BR>
<CENTER><INPUT TYPE="SUBMIT"></CENTER>
</FORM>
</BODY>
</HTML>

Tokish.java
Prerana Tokas

Page 2

package tokish_pckg;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Tokish extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println(" <html> <body> ");
out.println("<br>Parameters Reading<br>");
out.println(

"<UL>\n" + " <LI><B>param1</B>: "


+ req.getParameter("param1")

+ "\n" +
" <LI><B>param2</B>: "
+ req.getParameter("param2") +
"\n" +
" <LI><B>param3</B>: "
+ req.getParameter("param3") +
"\n" +
"</UL>\n");
out.println(" </body> </html> ");
}
}

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>TokishSCS</servlet-name>
<servlet-class>tokish_pckg.Tokish</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TokishSCS</servlet-name>
<url-pattern>/s</url-pattern>
</servlet-mapping>
</web-app>

Simple Interest Calculation


Prerana Tokas

Page 3

index.html
<HTML>
<HEAD>
<TITLE>MCA 355 ECJ Lab Program 01</TITLE>
</HEAD>
<BODY>
<H1>SIMPLE INTEREST CALCULATION</H1>
<A HREF="si.html">Click here for Simple Interest Calculation!
</A>
</BODY>
</HTML>

si.html
<HTML>
<HEAD>
<TITLE>Calculating Simple Interest</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">Enter values here!</H1>
<FORM ACTION="si" method="GET">
Enter Principle: <INPUT TYPE="TEXT" NAME="p"><BR>
Enter Rate: <INPUT TYPE="TEXT" NAME="r"><BR>
Enter Time: <INPUT TYPE="TEXT" NAME="t"><BR>
<CENTER><INPUT TYPE="SUBMIT"></CENTER>
</FORM>
</BODY>
</HTML>

Simple.java
package simple_interest;

Prerana Tokas

Page 4

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Simple extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println(" <html> <body> ");
out.println("<br>Parameters Reading<br>");
out.println( "<UL>\n" + " <LI><B>param1</B>: " +
req.getParameter("p") + "\n" +
" <LI><B>param2</B>: +
req.getParameter("r") + "\n" +
" <LI><B>param3</B>: " +
req.getParameter("t") + "\n" +
"</UL>\n");
si=(Int.parse32(p)+Int.parse32(r)+Int.parse32(t))/100;
out.println(" Simple Interest:="+si);
out.println(" </body> </html> ");
}
}

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>Simple</servlet-name>
<servlet-class>simple_interest .Simple</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Simple</servlet-name>
<url-pattern>/si</url-pattern>
</servlet-mapping>
</web-app>

PROGRAM No.2
Prerana Tokas

Page 5

Sometimes it is necessary to provide initial configuration information for


Servlets. Configuration information for a Servlet may consist of a string or a set
of string values included in the Servlet's web.xml declaration.
You define initialization attributes for servlets in the Web application
deployment descriptor, web.xml, in the init-param element of the servlet
element, using param-name and param-value tags. The web.xml file is located
in the WEB-INF directory of your Web application.
Create a web application to provide implementation to above according to your
assumption for any such four parameters.
Use a package titled as you first name.
Solution:
index.html
<HTML>
<HEAD>
<TITLE>MCA 355 ECJ Lab Program 2</TITLE>
</HEAD>
<BODY>
<H1>Initial Parameter</H1>
</BODY>
</HTML>

web.xml
<web-app>
<servlet>
<init-param>
<param-name>name</param-name>
<param-value>DKES School of Computer Science</paramvalue>
</init-param>
<init-param>
<param-name>established</param-name>
<param-value>2003</param-value>
</init-param>
<init-param>
<param-name>add</param-name>
<param-value># Lodhi Estate</param-value>
</init-param>
<servlet-name>GetInitParameter</servlet-name>
Prerana Tokas

Page 6

<servlet-class>Tokish_pckg.GetInitParameter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetInitParameter</servlet-name>
<url-pattern>/GetInitParameter</url-pattern>
</servlet-mapping>
</web-app>

GetInitParameter.java
package Tokish_pckg;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetInitParameter extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter( );
out.println("A Student have the following record : ");
Enumeration enm =
getServletConfig().getInitParameterNames();
while (enm.hasMoreElements( ))
{
out.print(enm.nextElement( ) + " ");
}
out.println("");
out.println("\nName: "
+
getServletConfig( ).getInitParameter("name"));
out.println("Established : "
+
getServletConfig( ).getInitParameter("established"));
out.println("Address : "
+
getServletConfig( ).getInitParameter("add"));
out.println("Program: "
+
getServletConfig( ).getInitParameter("phNo"));
}
}

PROGRAM No.3
Prerana Tokas

Page 7

Create a servlet that shows per-client access counts, that shows basic
information about the client's session. When the client connects, the servlet
uses request.getSession either to retrieve the existing session or, if there is no
session, to create a new one. The servlet then looks for an attribute called
accessCount of type Integer. If it cannot find such an attribute, it uses 0 as the
number of previous accesses. This value is then incremented and associated
with the session by setAttribute. Finally, the servlet prints a small HTML table
showing information about the session.
Use a package titled as you first name.

Solution
index.html
Prerana Tokas

Page 8

<HTML>
<HEAD>
<TITLE>MCA 355 ECJ Lab Program 03</TITLE>
</HEAD>
<BODY>
<H1>Program on User Session</H1>
<A HREF="session.html">Proceed</A>
</BODY>
</HTML>

session.html
<HTML>
<HEAD>
<TITLE>User Session</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">Maintaining User Session</H1>
<FORM ACTION="unique" method="GET">
<CENTER><INPUT TYPE="SUBMIT"></CENTER>
</FORM>
</BODY>
</HTML>

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>ShowSession</servlet-name>
<servlet-class>Tokish_pckg.ShowSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowSession</servlet-name>
<url-pattern>/unique</url-pattern>
</servlet-mapping>
</web-app>

ShowSession.java
package Tokish_pckg;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Prerana Tokas

Page 9

import java.util.*;
/** Servlet that uses session tracking to keep per-client
* access counts. Also shows other info about the session.
*/
public class ShowSession extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws
ServletException, IOException
{
response.setContentType("text/html");
HttpSession session = request.getSession();
String heading;
Integer accessCount = (Integer)session.getAttribute("accessCount");
if (accessCount == null)
{
accessCount = new Integer(0);
heading = "Welcome, Newcomer";
}
else
{
heading = "Welcome Back";
accessCount = new Integer(accessCount.intValue()+1);
}
// Integer is an immutable data structure. So, you
// cannot modify the old one in-place. Instead, you
// have to allocate a new one and redo setAttribute.
session.setAttribute("accessCount", accessCount);
PrintWriter out = response.getWriter();
String title = "Session Tracking Example";
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" + "<HEAD><TITLE>" + title +
"</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" + "<CENTER>\n" +
"<H1>" + heading + "</H1>\n" +
"<H2>Information on Your Session:</H2>\n" +
"<TABLE BORDER=1>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
" <TH>Info Type<TH>Value\n" +
"<TR>\n" +
" <TD>ID\n" +
Prerana Tokas

Page 10

" <TD>" + session.getId() + "\n" +


"<TR>\n" +
" <TD>Creation Time\n" +
" <TD>" +
new Date(session.getCreationTime()) + "\n" +
"<TR>\n" +
" <TD>Time of Last Access\n" +
" <TD>" +
new Date(session.getLastAccessedTime()) +"\n" +
"<TR>\n" +
" <TD>Number of Previous Accesses\n" +
" <TD>" + accessCount + "\n" +
"</TABLE>\n" +
"</CENTER></BODY></HTML>");
}
}

PROGRAM No.4
Create a servlet to accumulating a list of user data, which
maintains a basic list of items that each user has purchased.
Hence create an application that uses a simple ArrayList (the
Java 2 platform's replacement for Vector) to keep track of the
items each user has purchased. In addition to finding or
Prerana Tokas

Page 11

creating the session and inserting the newly purchased item


(the value of the newItem request parameter) into it, this
application outputs a bulleted list of whatever items are in the
"cart" (i.e., the ArrayList).
Create an HTML form that collects values of the newItem
parameter and submits them to the servlet. Following figure A
shows the result of the form; figure A and figure B show the
results of the servlet before the order form is visited and after
it is visited several times, respectively.
Use a package titled as you first name.

figure A
figure B

figure C

Solution:
index.html
<HTML>
<HEAD>
<TITLE>MCA 355 ECJ Lab Program 04</TITLE>
</HEAD>
Prerana Tokas

Page 12

<BODY>
<H1>Use of ArrayList</H1>
<a href="http://localhost:8080/SESSIONPGM01/ShowItems">Show
Items</a>
</BODY>
</HTML>

OrderForm.html
<HTML>
<HEAD>
<TITLE>Order Form</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H1>Order Form</H1>
<FORM ACTION="ShowItems">
New Item to Order:
<INPUT TYPE="TEXT" NAME="newItem"
VALUE="Yacht"><P>
<INPUT TYPE="SUBMIT" VALUE="Order and Show All
Purchases">
</FORM>
</CENTER>
</BODY>
</HTML>

web.xml
<web-app>
<servlet>
<servlet-name>ShowItems</servlet-name>
<servlet-class>Tokish_pckg.ShowItems</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowItems</servlet-name>
<url-pattern>/ShowItems</url-pattern>
</servlet-mapping>
</web-app>

ShowItems.java
package Tokish_pckg;
Prerana Tokas

Page 13

import
import
import
import

java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;

/** Servlet that displays a list of items being ordered.


* Accumulates them in an ArrayList with no attempt at
* detecting repeated items. Used to demonstrate basic
* session tracking.
*/
public class ShowItems extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
HttpSession session = request.getSession();
ArrayList previousItems =
(ArrayList)session.getAttribute("previousItems");
if(previousItems == null)
{
previousItems = new ArrayList();
session.setAttribute("previousItems", previousItems);
}
String newItem = request.getParameter("newItem");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Items Purchased";
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>" + title + "</H1>");

synchronized(previousItems)
{
if(newItem != null)
{
previousItems.add(newItem);
}
if(previousItems.size() == 0)
{
out.println("<I>No items</I>");
Prerana Tokas

Page 14

}
else
{
out.println("<UL>");
for(int i=0; i<previousItems.size(); i++)
{
out.println("<LI>"+(String)previousItems.get(i));
}
out.println("</UL>");
}
}
out.println("</BODY></HTML>");
}
}

Prerana Tokas

Page 15

Vous aimerez peut-être aussi