Вы находитесь на странице: 1из 44

JAVA PRACTICALS

1. Implement a bank account


Bank.java
package BankBranch;

import java.util.Scanner;

import java.util.ArrayList;

class Bank extends AccountHolder{

public static void main(String args[]){

int i = 0;

Scanner ac= new Scanner(System.in);

int choice,again,again2,accounter;

ArrayList<AccountHolder> ar = new <AccountHolder> ArrayList();

do{

System.out.println("1. SIGN UP");

System.out.println("2 LOG IN");

accounter = ac.nextInt();

switch( accounter){

case 1: {

AccountHolder p1 = new AccountHolder(++i);

ar.add(p1);

System.out.println("New Accoutn created : Account no. : " + i);

}break;

case 2: {

if( i == 0){

System.out.println("No account present. SIGN UP FIRST")

else

{
System.out.println("Enter Account No. : ");

int id = ac.nextInt();

AccountHolder pp = ar.get(id-1);

do{

System.out.println("1. CHECK BALANCE ");

System.out.println("2. DEPOSIT ");

System.out.println("3. WITHDRAW ");

System.out.println(" ENTER CHOICE--- ");

choice= ac.nextInt();

switch(choice)

case 1: {

int b = pp.getBalance();

System.out.println(" BALANCE: " + b);

}break;

case 2: {

System.out.println(" ENTER AMOUNT YOU WANT TO DEPOSITE: ");

int d = ac.nextInt();

pp.Deposit(d);

} break;

case 3: {

System.out.println(" ENTER AMOUNT YOU WANT TO WITHDRAW: ");

int W = ac.nextInt();

int rupees = pp.withdraw(W);

System.out.println("You have recieved this much ammount: " + rupees);

} break;

default: System.out.println("Enter a valid option");break;

System.out.println("Want to do again ? : 1/0");

again2 = ac.nextInt();

}while( again2 == 1);


}

}break;

default: System.out.println("Enter valid input");

System.out.println("Want to do again ? : 1/0");

again = ac.nextInt();

}while(again == 1);

public Bank(int m) {

super(m);

AccountHolder.java
package BankBranch;

public class AccountHolder {

int balance,Acc_no;

AccountHolder(int m){

balance = 0;

Acc_no = m;

void Deposit(int amount){

balance = balance + amount;

int withdraw(int amount){

if(balance >= amount){

balance = balance - amount;

TaxDeduction(amount);

System.out.println("Remaining balance : " + balance);

return amount;
}

else

if(balance != 0){

int b = balance;

balance = 0;

return b;

else

System.out.println("Insufficient Balance");

return -1;

int getBalance(){

return balance;

void TaxDeduction(int amount){

if(amount >= 50000 && amount <70000){

balance = balance - (amount*2)/100;

else if(amount >=70000 && amount < 80000)

balance = balance - (amount*3)/100;

else if( amount >= 80000)

balance = balance - (amount*5)/100;

}
Output:
2. Implement an abstract class stack with methods push, pop , display for two classes :
StaticStack and DynamicStack .
DynamicStack.java
package sdstack;

import java.util.ArrayList;

public class DynamicStack extends Stack {


private int top;
private ArrayList<Integer> a=new ArrayList<>();
public DynamicStack()
{
top=-1;
}
@Override
public void push(int x)
{
top++;
a.add(x);
}
@Override
public int pop()
{
if(top==-1)
{
System.out.println("Stack Underflow ");
return -1;
}
else{
top--;
System.out.print("Element Popped : ");
return a.remove(a.size()-1);
}
}
public void display()
{
for(int i=0;i<=top;i++)
System.out.print(a.get(i)+" ");
}
}

SDstack.java
package sdstack;
import java.util.Scanner;
public class SDStack {
public static void main(String[] args) {
int ch,op,sd;
Scanner IN=new Scanner(System.in);
System.out.println("Choose Stack :\n1.Static Stack\n2.Dynamic Stack
");
sd=IN.nextInt();
switch(sd)
{
case 1: StaticStack s;
System.out.print("Enter the size of the stack ");
int n=IN.nextInt();
s=new StaticStack(n);
do{
System.out.println("Static Stack of size"+n);
System.out.println("1.Push");
System.out.println("2.Pop");
ch=IN.nextInt();
switch(ch)
{
case 1: System.out.print("Enter the element to
be pushed ");
int elt=IN.nextInt();
s.push(elt);
s.display();
break;
case 2:System.out.println(s.pop());
s.display();
break;
}
System.out.print("Exit(0)/Menu(1)");
op=IN.nextInt();
}while(op==1);
break;
case 2:DynamicStack d=new DynamicStack();
do{
System.out.println("Dynamic Stack ");
System.out.println("1.Push");
System.out.println("2.Pop");
ch=IN.nextInt();
switch(ch)
{
case 1:System.out.print("Enter the element to
be pushed ");
int elt=IN.nextInt();
d.push(elt);
d.display();
break;
case 2:System.out.println(d.pop());
d.display();
break;
}
System.out.print("Exit(0)/Menu(1)");
op=IN.nextInt();
}while(op==1);
break;
default: System.out.println("Invalid Input");
break;
}
}
}

Stack.java
package sdstack;

public abstract class Stack {


abstract void push(int x);
abstract int pop();
}
StaticStack.java
package sdstack;
public class StaticStack extends Stack{
private int top;
private int size;
private int a[];
public StaticStack(int n)
{
size=n;
top=-1;
a=new int[n];
}
@Override
public void push(int x) {
if(top>=size-1){
System.out.println("Stack overflow");
return;
}
else {a[++top]=x;
System.out.println("Element pushed into the stack ");

}
}

@Override
public int pop() {
if(top==-1)
System.out.println("Stack Underflow");
else {
System.out.println("Element Popped : ");
return a[top--];
}
return -1;
}
public void display()
{
for(int i=0;i<=top;i++)
{
System.out.print(a[i]+" ");
}

}
}
Ouput:
JAVASCRIPT PRACTICALS

1. Create a student registration form .Create functions to perform the following checks:
a.) Roll number is a 7 digit numeric value.
b.) Name should be an alphabetical value.
c.) DOB entered in dd/mm/yy format and should be displayed in words.
d.) Check on non-empty fields.
<html>

<head>

<title>STUDENT REGISTRATION FORM</title>

<script ="JavaScript">

//alert("hello");

function check_name()

var letters = /^[A-Za-z]+$/;

var c= document.getElementById("1").value;

if(!c.match(letters)) {

document.getElementById("0").value=('Username must have alphabet characters only');

else

document.getElementById("0").value="";

function check_roll() {

var no = document.getElementById("1").value;

if (no.length!=7 ) {

document.getElementById("1").value=("Roll no must be of 7 digits" );

else

document.getElementById("1").value="";

function check_date( ){

var db = new Date( );


//alert(db);

var weekdays=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];

var
mm=['January','February','March','April','May','June','July','August','September','October','November','Decemb
er'];

var day = db.getDay();

var month=db.getMonth();

alert("Date is : "+weekdays[day]+" ,"+mm[month]+" "+db.getDate() +" "+db.getFullYear());

</script>

</head>

<body>

<center><b> STUDENT RESGISTRATION FORM</b></center><br><br>

<form name=f1 style="text-align: center">

Name:<input type="text" id="0" onclick="check_name" ><br><br>

Roll no: <input type="number" id="1" onclick="check_roll"><br><br>

DOB :<input type="date" name="dob" id ="3" <br><br>

<input type="submit" name="submit" onclick="check_date( )">

</form>

</body>

</html>

Output:
2. Implement a static password protection
<html>

<head>

<title>Login Form</title><br>

<script type="text/javascript">

function check()

var id = document.getElementById("1").value;

var pass = document.getElementById("2").value;

if((id=="admin") && (pass=="password123"))

alert("Login Sucessful!!!\nWelcome "+id);

else

alert("ID not found");

</script>

</head>

<body>

Login id : <input type="text" id="1"/><br>

Password : <input type="password" id="2"/><br>

<input type="button" value="submit" onclick="check()"/>

</body>

</html>

Output:
3. Write a javascript to sort an array using bubblesort. Take the number of elements in
array form the user.
<html>

<head>

<title>BUBBLE SORT</title>

<script type="text/javascript">

function Sort_N(){

var n = document.getElementById("1").value;

var str = document.getElementById("2").value;

arr = str.split(",");

arr2 = arr.map(Number);

var n = arr2.length;

for(var i=0; i < n; i++){

for(var j=1; j < (n-i); j++){

if(arr2[j-1] > arr2[j]){

var temp = arr2[j-1];

arr2[j-1] = arr2[j];

arr2[j] = temp;

} }

document.getElementById("4").value = arr2.toString();

</script>

</head>

<body>

<form>

Enter no. of element : <input type="number" id="1"/>

Enter element : <input type="text" id="2"/>

<input type="button" id="3" value="Sort" onclick="Sort_N()"/>

Result : <input type="text" id="4" />

</form>

</body>

</html>
Output:
4. Write a javascript to implement stack methods(push and pop).
<html>

<head>

<script language="JavaScript">

arr = [];

function stackpush() {

var n = document.getElementById("1").value;

arr.push(n);

document.getElementById("1").value="";

document.getElementById("4").value=arr.toString();

function stackpop() {

arr.pop();

document.getElementById("4").value=arr.toString();

</script>

</head>

<body>

<form>

<center>Enter Number : <input type="number" id="1"/><br></center>

<center><input type="button" id="2" value="Push" onclick ="stackpush()"/><br></center>

<center><input type="button" id="3" value="Pop" onclick="stackpop()"/><br><br></center>

<center>Stack: <textarea id="4"></textarea></center>

</form>

</body>

</html>
Output:
5.Write a javascript
a.)to change the color of the text using setTimeout().
<html>

<head>

<center> <b> CHANGE THE COLOUR </b> </center</head>

<body>

<script language="javascript">

function f1(){

document.getElementById("1").style.color = "#FF0000";

setTimeout ( "f2()", 2000 );

function f2() {

document.getElementById("1").style.color = "#0000ff";

</script>

<form >

<center><br> ENTER THE TEXT:<br>

<input type="text" id="1" name="text1"/><br>

<input type="button" value="change color" id ="2" onclick="f1()"/>

</form>

</body>

</html>

Output:

b.)to move an image across screen using setInterval().


<html>

<head>
<title>JavaScript Animation</title>

<script type="text/javascript">

var imgObj = null;

var animate ;

function init(){

imgObj = document.getElementById('myImage');

imgObj.style.position= 'relative';

imgObj.style.left = '0px';

function moveRight(){

imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';

animate = setInterval(moveRight,2000); // call moveRight in 20msec

function stop(){

clearTimeout(animate);

imgObj.style.left = '0px';

window.onload =init;

//-->

</script>

</head>

<body>

<form>

<img id="myImage"
src="http://www.seedgeeks.com/store/image/cache/data/product/flowers/ox.eye.sunflower2-
500x500_0.jpg"/>

<p>Click the buttons below to handle animation</p>

<input type="button" value="Start" onclick="moveRight();" />

<input type="button" value="Stop" onclick="stop();" />

</form>
</body>

</html>

Output:
6. Create a webpage which accepts user information and user comments on the website.
Design the webpage using form elements and check if all the text fields have being
entered with the data else display an alert.
<html>

<head></head>

<title>INFONET SERVICES</title>

<script language="JavaScript">

var c;

function f1(){

for(c=0;c<9;c++){

if(document.my.elements[c].value=="") {

alert("empty field" +document.my.elements[c].name);

document.my.elements[c].focus();

break;

else {

alert("Welcome");

</script>

<body>

<i> <h1> INFONET SERVICES </h1></i><p>

<form name="my">

<input type="text" name="fname" id="1" > FIRST NAME<br><p>

<input type="text" name="lname" id="2" > LAST NAME<br><p>

<input type="text " name="mail" id="3"> EMAIL<br><p>

<input type="text" name="add" id ="4" > ADDRESS<br><p>

<input type="text" name="city" id ="5" > CITY<br><p>

<input type="text" name="state" id ="6" > STATE

<input type="text" name="country" id ="7" > COUNTRY

<input type="text" name="postal" id ="8" > POSTAL_CODE<br><p>


<i>PLEASE CHOOSE THE MOST APPROPRIATE STATEMENT</i><br>

<input type="radio" id="a1" > I regularly purchase items online<br>

<input type="radio" id="a2" > I have an occassion purchase items online<br>

<input type="radio" id="a3" > I have not purchased anything online but i would consider it<br>

<input type="radio" id="a4" > I prefer to shop in real stores<br><p>

<i>I'm interested in (choose all that apply)</i><br>

<input type="checkbox" id="c1" > Hiking<br>

<input type="checkbox" id="c2" > Mountain biking<br>

<input type="checkbox" id="c3" > Camping<br>

<input type="checkbox" id="c4" > OIT-Read 4WD<br>

<input type="checkbox" id="c5" > CRoss-country skiing</br><p>

<i>I learned about this site from</i>

<SELECT NAME=" " size="" >

<OPTION SELECTED > Print Ads</OPTION></br>

<OPTION >See Ads</OPTION>

<OPTION ></OPTION></SELECT></br><p>

<i>Write comments here</i><p>

<input type="text area" value="Please type any comments here" id="9"></br>

<input type="button" value="submit info" onclick="f1()">

<input type="reset" value="Start over" ></br>

</form>

</body>

</html>
Output:
JSP PRACTICALS

1. Display the pattern


1
1 2
1 2 3
Take ‘n’ in a textbox from user. Display the pattern using
•Scriptlets
•<c:forEach>loop
index.html
<!DOCTYPE html>
<html>
<head>
<title> Question 1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<form action='index.jsp'>
Enter Number of Lines: <input type='text' name='lineno'>
<input type='submit' name='submit' value="Submit">
</form>
</body>
</html>

index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%!int n;%>

<html>
<body>
<% n=Integer.parseInt(request.getParameter("lineno"));
for(int i=1;i<=n;i++)
{ for(int j=1;j<=i;j++)
{ out.println(j+" "); }
out.println("<br>");
}
%>

</body>
</html>
Output:
2. Make two files as follows:
a. main.html: shows 2 text boxes and 3 radio buttons with values “addition”,”subtraction”
and multiplication.
b. operate.jsp:depending on what the user selects perform the corresponding functions

main.html
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<form action="operate.jsp">
Enter Number 1: <input type="text" name="no1">
Enter Number 2:<input type="text" name="no2"><br>
Addition: <input type="radio" name="op" value="add"><br>
Subtraction: <input type="radio" name="op"
value="sub"><br>
Multiplication: <input type="radio" name="op"
value="mul"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

operate.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%! int n1,n2;%>
<%! String ch;%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>
<body>
<% n1=Integer.parseInt(request.getParameter("no1"));
n2=Integer.parseInt(request.getParameter("no2"));
ch=request.getParameter("op");

if(ch.equals("add"))
out.println("Addition is: "+(n1+n2));
else if(ch.equals("sub"))
out.println("Subtraction is: "+(n1-n2));
else if(ch.equals("mul"))
out.println("Multiplication is: "+(n1*n2));
%>
</body>
</html>

Output:

4.Display Good Morning <uname>,Good Afternoon <uname> or Good Evening <uname>


based on the current time of the day.
index.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<div>
<form action="time.jsp" method="post">
question number<input type="text" name="number" >
<input type="submit">
</div>
</body>
</html>

time.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@taglib prefix="ex" uri="/WEB-INF/tlds/library"%>
<!DOCTYPE html>
<html>
<head id>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>HI</title>
</head>
<body>
<ex:uname/>
</body>
</html>

library.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>library</short-name>
<uri>/WEB-INF/tlds/library</uri>
<tag>
<name>uname</name>
<tagclass>pack.Servlet</tagclass>
<body-content>scriptless</body-content>
</tag>
</taglib>
Servlet.java

package pack;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.Date;
import javax.servlet.jsp.JspException;

public class Servlet extends SimpleTagSupport


{
public void doTag() throws JspException,IOException
{
JspWriter out=getJspContext().getOut();
try{
long d=new Date().getTime();

if(d<12)
out.println("Good Morning");
else if(d>=12 && d<=16)
out.println("good afternoon");
else
out.println("good evening");
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:

5. Let the user enter a word in a textbox and let her/him select one of even or odd radio
buttons.If he/she selects odd,check the odd positions in the word entered, if they all
contain vowels , then display ‘You Win’ , else display ‘You Lose’. If the user selects even ,
check for vowels in all even positions in the word entered . Use jstl’s ‘fn’ library.
EvenOdd.html
<!DOCTYPE html>

<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-


scale=1.0">

</head>

<body>

<center><form action="Checkpos.jsp" method="POST">

Enter a string: <input type="text" name="str">


<br>

Select the operation: <br>

Odd<input type="radio" value="1" name="rad"><br>

Even<input type="radio" value="2" name="rad"><br>

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

</form></center>

</body>

</html>

Checkpos.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
String str1=request.getParameter("str");
String str2=request.getParameter("rad");
int c=Integer.parseInt(str2);
char ch;
int flag=0;
switch(c)
{
case 1: for(int i=0;i<str1.length();i++)
{
if(i%2!=0)
{ ch=str1.charAt(i); if(ch=='a' ||
ch=='A' || ch=='e' || ch=='E' ||ch=='i' || ch=='I' || ch=='o' ||
ch=='O' ||ch=='u' || ch=='U')
{ flag=1; }
else
{ flag=0; break; }
}
}
if(flag==1)
out.println("..You win..");
else
out.println("..You lose..");
break;
case 2 : for(int i=0;i<str1.length();i++)
{
if(i%2==0)
{ ch=str1.charAt(i);
if(ch=='a' || ch=='A' || ch=='e' ||
ch=='E' ||ch=='i' || ch=='I' || ch=='o' || ch=='O' ||ch=='u' ||
ch=='U')
{ flag=1; }
else
{ flag=0; break; }
}
}
if(flag==1)
out.println("..You win..");
else
out.println("..You lose..");
break;
}
%>
</body>
</html>

Output:
6. Create a custom library which contains two tags : <hello>.<choco>.
serv.java

package test;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.Date;
import javax.servlet.jsp.JspException;
public class serv extends SimpleTagSupport
{
String texture;
public void setTexture(String texture)
{
this.texture=texture;
}

public void doTag() throws JspException,IOException


{
JspWriter out=getJspContext().getOut();
if(texture.equals("Crunchy"))
out.println("Munch KitKat");
if(texture.equals("Chewy"))
out.println("FiveStar BarOne");
}
}

servlet.java

package test;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.Date;
import javax.servlet.jsp.JspException;

public class servlet extends SimpleTagSupport


{
String name;
public void setName(String name)
{
this.name=name;
}
@Override
public void doTag() throws JspException,IOException
{
JspWriter out=getJspContext().getOut();
out.println("Hello "+name);
}
}

tag.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>tag</short-name>
<uri>/WEB-INF/tlds/tag</uri>

<tag>
<name>hello</name>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<tagclass>test.servlet</tagclass>
<body-content>scriptless</body-content>
</tag>

<tag>
<name>choco</name>
<attribute>
<name>texture</name>
<required>true</required>
</attribute>
<tagclass>test.serv</tagclass>
<body-content>scriptless</body-content>
</tag>
</taglib>

Jsp.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ct" uri="/WEB-INF/tlds/tag.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>

<ct:hello name="Ajay"/><br>
<ct:choco texture="Chewy"/><br>
<ct:choco texture="Crunchy"/>

</html>

Output:
7.Create a custom tag “substring” with 3 mandatory attributes “input”,”start”,”end”
which will do substring operation on given input.

jsp.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ct" uri="/WEB-INF/tlds/lib.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>

<body>
<ct:substring input="People" begin="2" end="5"/>
</body>
</html>

lib.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>lib</short-name>
<uri>/WEB-INF/tlds/lib</uri>

<tag>
<name>substring</name>
<attribute>
<name>input</name>
<required>true</required>
</attribute>
<attribute>
<name>begin</name>
<required>true</required>
</attribute>
<attribute>
<name>end</name>
<required>true</required>
</attribute>
<tagclass>test.servlet</tagclass>
<body-content>scriptless</body-content>
</tag>
</taglib>

servlet.java
package test;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.Date;
import javax.servlet.jsp.JspException;

public class servlet extends SimpleTagSupport


{
String input;
int begin,end;
public void setInput(String input)
{
this.input=input;
}

public void setBegin(int begin)


{
this.begin=begin;
}

public void setEnd(int end)


{
this.end=end;
}

@Override
public void doTag() throws JspException,IOException
{
JspWriter out=getJspContext().getOut();
String sub="";
for(int i=begin;i<=end;i++)
sub+=input.charAt(i);
out.println("Substring is: "+sub);
}
}

Output:
8. Create a custom tag “reverse” with a mandatory attribute “input” to reverse a string.
jsp.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ct" uri="/WEB-INF/tlds/tag.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>
<body>
<ct:reversed input="hello"/>
</body>
</html>

tag.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>tag</short-name>
<uri>/WEB-INF/tlds/tag</uri>

<tag>
<name>reversed</name>
<attribute>
<name>input</name>
<required>true</required>
</attribute>
<tagclass>test.servlet</tagclass>
<body-content>scriptless</body-content>
</tag>
</taglib>

servlet.java

package test;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.Date;
import javax.servlet.jsp.JspException;

public class servlet extends SimpleTagSupport


{
String input;
public void setInput(String input)
{
this.input=input;
}
@Override
public void doTag() throws JspException,IOException
{
JspWriter out=getJspContext().getOut();
String reverse = new
StringBuffer(input).reverse().toString();
out.println(reverse);
}
}

Output:
9.Create a custom tag “today” that displays today’s date and time .
servletfile.java
package test;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.Date;
import javax.servlet.jsp.JspException;
public class servletfile extends SimpleTagSupport
{
public void doTag() throws JspException,IOException
{
JspWriter out=getJspContext().getOut();
Date date=new Date();
out.println(date.toString());
}
}

libfile.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>libfile</short-name>
<uri>/WEB-INF/tlds/libfile</uri>

<tag>
<name>today</name>
<tagclass>test.servletfile</tagclass>
<body-content>scriptless</body-content>
</tag>
</taglib>

jspfile.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ct" uri="/WEB-INF/tlds/libfile.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>
<body>
<ct:today/>
</body>
</html>

Output:
10. Ask a user’s name and age on html page. Then display hello <uname> on jsp page. Ask
user the product they want to buy.
index.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<form action="mytag.jsp" method="post">
Enter your name : <input type="text" name="name">
Enter your age : <input type="number" name="age">
<input type="submit">
</form>
</body>
</html>

hello.java
package ques10;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class hello extends SimpleTagSupport{
private String name;
public void setName(String name)
{
this.name=name;
}
public void doTag() throws JspException,IOException{
JspWriter out=getJspContext().getOut();
out.println("Hello "+name);

}
}

mytag.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ex" uri="WEB-INF/tlds/uname.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<ex:uname name="${param.name}"/>
<p:product x="${param.sell}" scope="application"/>
<form action="tag.jsp" method="post">

BAG<input type="radio" name="sell" value="Bag">


BOTTLE<input type="radio" name="sell" value="Bottle">
TIFFIN BOX<input type="radio" name="sell" value="Tiffin box">
<input type="submit">
</form>
</body>
</html>

tag.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<c:out value="${x}"></c:out>
<c:out value="${param.sell}"></c:out>
</body>
</html>

uname.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>ex</short-name>
<uri>/WEB-INF/tlds/uname</uri>
<!-- A validator verifies that the tags are used correctly at JSP
translation time. Validator entries look like this:
<validator>
<validator-class>com.mycompany.TagLibValidator</validator-class>
<init-param>
<param-name>parameter</param-name>
<param-value>value</param-value>
</init-param>
</validator>
-->
<!-- A tag library can register Servlet Context event listeners in
case it needs to react to such events. Listener entries look
like this:
<listener>
<listener-class>com.mycompany.TagLibListener</listener-class>
</listener>
-->
<tag>
<name>uname</name>
<tag-class>ques10.hello</tag-class>
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<body-content>scriptless</body-content>
</tag>
</taglib>

Output:

Вам также может понравиться