Servlet program

23
Ex.No:1 Current Date And Time Aim :- To write a Servlet that will print the current date and time. Algorithm :- Steps-1. Create a new servlet. Steps-2. Import the HTTPServlet, util and text packages. Steps-3. In the processRequest() method declare a object of Date class and DateFormat class. Steps-4. Using the format() function print the date. Steps-5. Compile& Run the program..Current Date will be displayed on the browser. 1

Transcript of Servlet program

Ex.No:1 Current Date And Time

Aim:-

To write a Servlet that will print the current date and time.

Algorithm:-

Steps-1. Create a new servlet.

Steps-2. Import the HTTPServlet, util and text packages.

Steps-3. In the processRequest() method declare a object of Date class and DateFormatclass.

Steps-4. Using the format() function printthe date.

Steps-5. Compile& Run the program..CurrentDate will be displayed on the browser.

1

CODING:-

Servlet Code:-

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.Date;

import java.text.DateFormat;

public class MyDate extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

Print Writer out = response.getWriter();

Date d=new Date();

DateFormat df=DateFormat.getInstance();

out.println("<html>");

2

out.println("<head>");

out.println("<title>My Date</title>");

out.println("</head>");

out.println("<body>");

out.println("My Current Date And Time Is");

out.println(df.format(d));

out.println("</body>");

out.println("</html>");

out.close();

}

}

3

OUTPUT:-

Current Date And Time:-

4

Ex.No:2 Generating Multiplication Table

5

Aim:-

To write a servlet that displays multiplication table for the number given by user.

Algorithm:-

Step-1. Create a HTML file that displays a single text box and a Submit button.

Step-2. Create a new servlet.

Step-3. Run the servlet and get its URL, put the URL of the servlet in the forms action attribute.

Step-4. In the processRequest methodof servlet get the value entered in the HTML form by the user using getParameter() method.

Step-5. Convert the value into integervalue and generate multiplication table.

Steps-6. Compile the Servlet.

Step-7. Run the HTM file enter any value and submit, multiplication table for thevalue will be printed on browser.

6

CODING:-

(i) Servlet Code:-

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class Multiplication extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

Print Writer out = response.getWriter();

String s=request.getParameter("t1");

int i;

int a=Integer.parseInt(s);

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet Multiplication</title>");

7

out.println("</head>");

out.println("<body>");

out.println("<h1> Multiplication Table for :" + s + "</h1>");

out.println("</body>");

out.println("</html>");

for(i=1;i<=10;i++)

{

out.print("<br>"+i+"*"+a+"="+(i*a));

}

out.close();

}

}

(ii) HTML Code:-

<html>

<head>

<title>Multiplication Table</title>

</head>

<body><center>

8

<form method ="get" action="http://localhost:8084/Multi/Multiplication">

Enter the Number

<input type="text" name="t1">

<input type="submit" name="button" value="submit">

</form></center>

</body>

</html>

OUTPUT:-

Multiplication Table:-

9

Ex.No:3 Sum Of Digits

10

Aim:-

To write a servlet that displays the sum of the digits for the number given by user

Algorithm:-

Step-1. Create a HTML file that displays a single text box and a Submit button.

Step-2. Create a new servlet.

Step-3. Run the servlet and get its URL, put the URL of the servlet in the forms action attribute.

Step-4. In the processRequest methodof servlet get the value entered in the HTML form by the user using getParameter() method.

Step-5. Convert the value into integervalue and write the logic for finding the sumof the digits.

Steps-6. Compile the servlet.

Step-7. Run the HTM file enter any value and submit, sum of the digits for the value will be printed on browser.

11

CODING:-

(i) Servlet Code:-

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class Sum extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

Print Writer out = response.getWriter();

String n=request.getParameter("number");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet Sum</title>");

out.println("</head>");

out.println("<body>");

12

int j=1,i,m=0;

int n1=Integer.parseInt(n)*j;

do

{

i=n1%10;

m=m+i;

n1=n1/10;

}

while(n1>0);

out.println("The Sum Of Digit Is"+m);

out.println("</body>");

out.println("</html>");

out.close();

}

}

(ii) HTML Code:-

<html>

<head>

<title></title>

</head>

13

<body>

<form method="get" action="http://localhost:8084/sum/Sum">

Enter The Number

<Input type="text" name = "number">

<input type ="submit" name="button" value="submit">

</form>

</body>

</html>

OUTPUT:-

(i) Sum Of Digits:-

14

(ii) Sum Of Digits:-

15

Ex.No:4 String Manipulation

16

Aim:-

To Write a Servlet that counts the vowels, Consonants and Special Characters in a given string

Algorithm:-

Step-1. Create a HTML file that displays a single text box and a Submit button.

Step-2. Create a new servlet.

Step-3. Run the servlet and get its URL, put the URL of the servlet in the forms action attribute.

Step-4. In the processRequest methodof servlet get the value entered in the HTML form by the user using getParameter() method.

Step-5. Count the vowels, consonants and special characters by extracting each character from the string and comparing them to the respective subsets.

Step-6. Print the total no from each subset.

Steps-7.Compile the Servlet.

Step-8. Run the HTM file enter any value and submit, sum of the digits for the value will be printed on browser.

17

CODING:-

(i) Servlet Code:-

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class CountVowels extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

Print Writer out = response.getWriter();

String s1=request.getParameter("t1");

int v=0,c=0,i,j,digit=0;

char a[]=new char [5];

a[0]='a';

a[1]='e';

a[2]='i';

18

a[3]='o';

a[4]='u';

for(i=0;i<s1.length();i++){

for(j=0;j<5;j++){

if(s1.toLowerCase().charAt(i)==a[j]){

v=v+1;

}

}

}

for(i=0;i<s1.length();i++){

if(Character.isLetter(s1.charAt(i)))

{

c=c+1;

}

}

for(i=0;i<s1.length();i++){

if(Character.isDigit(s1.charAt(i)))

{

digit=digit+1;

}

}

19

int m=c-v;

int temp=v+m+digit;

int spl=s1.length()-temp;

out.println("Vowels\n"+v);

out.println("\n\nConsonants\n"+m);

out.println("Digits"+digit);

out.println("Special Character"+spl);

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet CountVowels</title>");

out.println("</head>");

out.println("<body>");

out.println("</body>");

out.println("</html>");

out.close();

}

}

(ii) HTML Code:-

<html>

<head>

<title>Print Vowels</title>

20

</head>

<body>

<form method="get" action="http://localhost:8084/Program/CountVowels">

Enter The String :

<input type="text" name="t1">

<input type="submit" name="button" value="Result">

</form>

</body>

</html>

OUTPUT:-

(i) String Manipulation:-

21

(ii) String Manipulation:-

22

23