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

PRACTICAL : 1

Q.1 write a servlet program to find wheather the number is


even or odd.
Source code:
HTML file:
<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 method="get" action="EO">
<h4>enter a number : </h4>
NO:<input type="text" name="no">
<br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>

Output:

JAVA file:
import
import
import
import
import
import

java.io.IOException;
java.io.PrintWriter;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class EO extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
int a=Integer.parseInt(request.getParameter("no"));
if(a%2==0)
out.println(a+" is even");
else
out.println(a+" is odd");
}
catch(Exception e)
{
out.println("ERROR:"+e.getMessage());
}
}
}

Q.2 write a servlet program for design a calculator to performs


the operation.(addition,subtraction,multiplication,division)
Source code:
HTML file:
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="calc">

<h4>enter the two number for calculation</h4>


NO-1: <input type="text" name="no1">
NO-2: <input type="text" name="no2">
<br>
<br>
<input type="submit" value="+" name="btn">
<input type="submit" value="-" name="btn">
<input type="submit" value="*" name="btn">
<input type="submit" value="/" name="btn">
</form>
</body>
</html>

Output:

JAVA file:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class calc extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
int a=Integer.parseInt(request.getParameter("no1"));
int b=Integer.parseInt(request.getParameter("no2"));
int c=0;
String op=request.getParameter("btn");
if(op.equals("+")) c=a+b;
if(op.equals("-")) c=a-b;
if(op.equals("*")) c=a*b;
if(op.equals("/")) c=a/b;
out.println(a+op+b+"="+c);
}

}
}

Output:
1)Addition:

3)multiplication:

2)Subtraction:

4)division:

Q.3 write a servlet program to find factorial of a number.


Source code:
HTML file:
<html>
<head>
<title>factorial</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="fact">
<h4>enter a number :</h4>
NO:<input type="text" name="no">
<br><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>

</html>

JAVA file:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class fact extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
int a=Integer.parseInt(request.getParameter("no"));
int fact=1;
for(int i=1;i<=a;i++)
{
fact=fact*i;
}
out.println("factorial of "+a+" is "+fact);
}
catch(Exception e)
{
out.println("ERROR:"+e.getMessage());
}
}
}

Output:

Q4.write a servlet program to find the number is prime or not.


Source code:
HTML file:
<html>
<head>
<title>prime</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="prime">
<h4>enter a number to check number is prime or not.</h4>

NO:<input type="text" name="no">


<br><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>

Output:

JAVA file:

import
import
import
import
import
import
import

java.io.IOException;
java.io.PrintWriter;
javax.servlet.ServletException;
javax.servlet.annotation.WebServlet;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class prime extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
int a=Integer.parseInt(request.getParameter("no"));
int i;
for( i=2;i<a;i++)
{
if((a%i)==0)
break;
}
if(i==a)
out.println(a+" is prime");
else
out.println(a+" is not prime");
}
catch(Exception e)

{
out.println("ERROR:"+e.getMessage());
}
}
}

Output:

Q.5 write a servlet program to find reverse of a number.


Source code:
<html>
<head>
<title>reverse</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="reverse">
<h4>enter a number :</h4>
NO:<input type="text" name="no">
<br><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>

Output:

JAVA file:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

import
import
import
import

javax.servlet.annotation.WebServlet;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class reverse extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
int a=Integer.parseInt(request.getParameter("no"));
int rem,rev=0;
int aa=a;
while(a>0)
{
rem=a%10;
rev=rem+rev*10;
a=a/10;
}
out.println("reverse of "+aa+" is "+rev);
}
catch(Exception e)
{
out.println("ERROR:"+e.getMessage());
}
}
}

Output:

PRACTICAL : 2
Q.1 write a servlet program to design a Question
Answer Formate.
Source Code:
HTML File:
<html>

<head>
<title>Question Answer formate</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="get" action="Qa">
<center><h1>MULTIPLE CHOICE QUESTIONS.</h1></center>
1.what is fullform of JSF<br>
<input type="radio" name="f1" value="1">
Java server pages<br>
<input type="radio" name="f1" value="2">
Java servlet fact<br>
<input type="radio" name="f1" value="3">
JJ SS FF<br>
<input type="radio" name="f1" value="4">
none of these.<br><br>
2.what is the fullform of CGI?<br>
<input type="radio" name="f2" value="1">
Comman Gateway Integrity<br>
<input type="radio" name="f2" value="2">
Comman Group Interface<br>
<input type="radio" name="f2" value="3">
Comman GateWay Interface<br>
<input type="radio" name="f2" value="4">
None of these<br><br>

3.Servlet is a ......<br>

<input type="radio" name="f3" value="1">


Server-Side program.<br>
<input type="radio" name="f3" value="2">
client-side program.<br>
<input type="radio" name="f3" value="3">
both a& b<br>
<input type="radio" name="f3" value="4">
None of these<br><br>

4. what is the fullform of JSP?<br>


<input type="radio" name="f4" value="1">
Java Server Pages<br>
<input type="radio" name="f4" value="2">
Java Script<br>
<input type="radio" name="f4" value="3">
Java server Package
<br>
<input type="radio" name="f4" value="4">
None of these<br><br>

5.init() method is used to .....<br>


<input type="radio" name="f5" value="1">
to accept the request <br>
<input type="radio" name="f5" value="2">
to destroy the servlet<br>
<input type="radio" name="f5" value="3">
to response to the client.
<br>

<input type="radio" name="f5" value="4">


to initialize the servlet<br><br>
<center><input type="submit" value="DONE"></center>
</form>
</body>
</html>

Output:

JAVA file:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Qa extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
int r=0;
int a=Integer.parseInt(request.getParameter("f1"));
int b=Integer.parseInt(request.getParameter("f2"));
int c=Integer.parseInt(request.getParameter("f3"));
int d=Integer.parseInt(request.getParameter("f4"));
int e=Integer.parseInt(request.getParameter("f5"));
if(a==2)r++;
if(b==3)r++;
if(c==1)r++;
if(d==1)r++;
if(e==4)r++;
out.println("total score is "+r);
} finally {
out.close();
}
}
}

PRACTICAL : 3
Q.2write a java program to retrieve data from database using
SQL statement.
package jdbc;
import java.sql.*;
public class JDBC {
public static void main(String[] args)throws SQLException {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","tiger");
Statement st=con.createStatement();
String sql="select * from dept";
ResultSet rs=st.executeQuery(sql);
ResultSetMetaData md=rs.getMetaData();
System.out.print("\n");
int n=md.getColumnCount();
for(int i=1;i<=n;i++)
System.out.print(md.getColumnName(i)+"\t");
System.out.print("\n");
if(!(rs.isBeforeFirst()))
{
System.out.print("no matching records\t\t");
System.exit(0);
}

else
{
while(rs.next())
{
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t\t");
System.out.print(rs.getString(3)+"\n");
}
}
con.close();
}
catch(Exception e)
{
System.out.print("Error: "+e.getMessage());
}
}
}

OUTPUT:

Q.2 Perform a SQL Query by using SQL PreparedStatement.


i)INSERT Command:
package jdbc;
import java.sql.*;
import java.io.*;
public class insert_statement {
public static void main(String args[])throws IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","");
PreparedStatement ps=con.prepareStatement("insert into dept values(?,?,?)");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("enter DEPT_NO:");
int deptno=Integer.parseInt(br.readLine());
System.out.println("enter DEPT_NAME:");
String deptname=br.readLine();
System.out.println("enter DEPT_LOC:");
String deptloc=br.readLine();
ps.setInt(1, deptno);
ps.setString(2, deptname);
ps.setString(3, deptloc);
ps.executeUpdate();
System.out.println("DATA ADDED.....");
}
catch(Exception e)
{
System.out.print("ERROR :"+e.getMessage());
}
}
}

Output:

ii). UPDATE Command :


import java.sql.*;
import java.io.*;
public class update_statement {
public static void main(String args[])throws IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","");
String sql="update dept set dept_name=?,dept_loc=? where dept_no=?";
PreparedStatement ps=con.prepareStatement(sql);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter DEPT_NO:");

int dept_no=Integer.parseInt(br.readLine());
System.out.println("enter DEPT_NAME:");
String dept_name=br.readLine();
System.out.println("enter DEPT_LOC:");
String dept_loc=br.readLine();
ps.setString(1, dept_name);
ps.setString(2, dept_loc);
ps.setInt(3, dept_no);
ps.executeUpdate();
System.out.println("DATA UPDATED.....");
}
catch(Exception e)
{
System.out.print("ERROR :"+e.getMessage());
}
}

OUTPUT:

iii). DELETE Command:


import java.sql.*;
import java.io.*;
class delete_statement {
public static void main(String args[])throws IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","");
String sql="delete from dept where dept_no=?";
PreparedStatement ps=con.prepareStatement(sql);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter DEPT_NO:");

int dept_no=Integer.parseInt(br.readLine());
ps.setInt(1, dept_no);
ps.executeUpdate();
System.out.println("ROW DELETED.....");
}
catch(Exception e)
{
System.out.print("ERROR :"+e.getMessage());
}
}
}

OUTPUT:

Q.3 Develop a JSP application to register user as per the


registration details.
HTML file:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="reg.jsp" method="post">
<table width="30%" align="center" border="2"><br>
<th> REGISTRATION NO : <input type="text" name="no"><br> <br>

NAME : <input type="text" name="name">


<br><br>
EMAIL ADDRESS : <input type="text" name="email"><br><br>
<input type="submit" value="register"> <br><br></th>
</table>>
</form>
</body>
</html>

OUTPUT:

JSP File:
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@page import= "java.sql.*" %>
<html>
<body>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/registration","root","");
PreparedStatement ps=con. prepareStatement("insert into user
values(?,?,?)");
int regno=Integer.parseInt(request.getParameter("no"));
String reg_name=request.getParameter("name");
String email_add=request.getParameter("email");
out.println("<br>REGISTRATION NO :"+regno);
out.println("<br>NAME :"+reg_name);
out.println("<br>EMAIL ADDRESS :"+email_add);

ps.setInt(1,regno);
ps.setString(2,reg_name);
ps.setString(3,email_add);
ps.executeUpdate();
out.println("<br> Data added....");
}
catch(Exception e)
{
out.println("not possible to add the record"+e.getMessage());
}
%>
00</body>
</html>

OUTPUT:

PRACTICAL 4
Q.1 Develop a JSP application to authenticate user login as per
the registration details.
Source Code:

File name: LOGIN.HTML


<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
</head>
<body>
<form method="post" action="login.jsp">
<table width="40%" align="center" border="5"><br>
<th> <center> <h2>SIGN UP</h2></center> <br>
USERNAME :<input type="text" name="uname"><br><br>
PASSWORD :<input type="password" name="passwd"><br><br>

<center><input type="submit" value="LOGIN"


name="submit"></center> <br></th>
</table>
</form>
</body>
</html>
OUTPUT:

File name: LOGIN.JSP


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ page import="java.sql.*" %>
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/mydb1","root","tiger");

PreparedStatement ps=con.prepareStatement("select * from user where


username=? and password=?");
String username=request.getParameter("uname");
String password=request.getParameter("passwd");
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
if(!rs.isBeforeFirst())
{
%>
<center><h2> INVALID USERNAME OR PASSWORD!!!!
</h2></center><<br>
<%@include file="/index.html" %>
<%}
else
{
%>
<jsp:forward page="/login1.jsp" />
<%
}
%>
</body>
</html>

File name: LOGIN1.JSP


<%@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>
<h2>WELCOME USER</h2>
<%=request.getParameter("uname")%>
</body>
</html>

OUTPUT:
Valid password:

Invalid password:

Q.2 Develop a JSP application to performs a operations +,-,*,/


by using calculator.
File name: CAL.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="calculator.jsp" method="post">
no1:<input type="text" name="t1"><br><br>
no2:<input type="text" name="t2"><br><br>
<input type="submit" value="+" name="btn">
<input type="submit" value="-" name="btn">
<input type="submit" value="*" name="btn">
<input type="submit" value="/" name="btn">
</form>
</body>
</html>
OUTPUT:

File name: calculator.JSP


<%@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>
<%
try
{
int a=Integer.parseInt(request.getParameter("t1"));
int b=Integer.parseInt(request.getParameter("t2"));
int c=0;
String op=request.getParameter("btn");
if(op.equals("+"))c=a+b;
if(op.equals("-"))c=a-b;
if(op.equals("*"))c=a*b;
if(op.equals("/"))c=a/b;
out.println(a+op+b+"="+c);
}
catch(Exception e)
{
out.println("ERROR"+e.getMessage());
}
%>

</body>
</html>

OUTPUT:

Practical : 5
Q.1 create an applet that can handle action
event(button),when an event occurs display the
respective result on the applet window.
Source code:
File name:button.java
package applet184;
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
/*<applet code="Applet184" width="300" height="400">
* </applet>
*/
public class Applet184 extends Applet implements ActionListener
{
String msg="";
Button b1,b2;
public void init()
{
b1=new Button("hello");
b2=new Button("hi");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);

}
@Override
public void actionPerformed(ActionEvent e)
{
String st=e.getActionCommand();
if(st.equals("hello"))msg="hello buton pressed";
else if(st.equals("hi"))msg="hi button pressed";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,100);
}
}

Output:

Q.2 write a java AWT program to design a calculator


GUI to performs the operation.
(addition,subtraction,multiplication,division)

Source code:
File name:button.java
packageAppletex;
importjava.applet.Applet;
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.GridLayout;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.TextEvent;
importjava.awt.event.TextListener;
public class appletx2 extends Applet implements ActionListener, TextListener{
String s,s1,s2,s3,s4;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button add,sub,eq,c1,mul,div;
TextField t1;
inta,b,c;
public void init() {
t1=new TextField(10);
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
add=new Button("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
eq=new Button("=");
c1=new Button("clear");
GridLayoutgb=new GridLayout(4,5);
setLayout(gb);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);

add(add);
add(sub);
add(mul);
add(div);
add(eq);
add(c1);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
add.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
eq.addActionListener(this);
c1.addActionListener(this);
paint();
}
public void paint()
{
setBackground(Color.green);
}
public void actionPerformed(ActionEvent e)
{
s=e.getActionCommand();
if(s.equals("0")||s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")
||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")||
s.equals("0"))
{
s1=t1.getText()+s;
t1.setText(s1);
}
if(s.equals("+"))
{
s2=t1.getText();
t1.setText("");
s3="+";
}
if(s.equals("-"))
{
s2=t1.getText();
t1.setText("");
s3="-";
}
if(s.equals("*"))
{
s2=t1.getText();
t1.setText("");

s3="*";
}
if(s.equals("/"))
{
s2=t1.getText();
t1.setText("");
s3="/";
}
if(s.equals("="))
{
s4=t1.getText();
a=Integer.parseInt(s2);
b=Integer.parseInt(s4);
if(s3.equals("+"))
c=a+b;
if(s3.equals("-"))
c=a-b;
t1.setText(String.valueOf(c));
}
if(s.equals("clear"))
{
t1.setText("");
}
}
public void textValueChanged(TextEvent e)
{
}
}

Output :

Q.3 Write a java program to present the set of choices


for a user to select the stationary product and display
the price of product after selected from file.
Source Code:
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

class stationary extends JFrame implements ListSelectionListener


{
JList list;
String prod[]={"pen","notebook","pencil","graph","colorbox","files"};
int price[]={5,40,20,10,140,50};
JScrollPane jsp;
JLabel lbl1;
Container c;
stationary()
{

super("product details");
c=getContentPane();
list=new JList(prod);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
lbl1= new JLabel();
jsp=new JScrollPane(list);
jsp.setBounds(200,200,100,135);

c.add(jsp);
lbl1.setBounds(200,200,100,135);
c.add(lbl1);
setSize(400,400);
setDefaultCloseOperation(3);

setVisible(true);
}
public static void main(String[] args) {
new stationary();
}

public void valueChanged(ListSelectionEvent e) {


int val=list.getSelectedIndex();
lbl1.setText("Price="+price[val]);

}
}

OUTPUT:

Q.1 Write a java program to demonstrate typical


editable table, describing employee details for a
software company.

SOURCE CODE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*; public class employee extends JFrame implements
ActionListener
{
JTable jt;
DefaultTableModel dtm;
JScrollPane jsp;
JLabel l1,l2,l3;
JTextField t1,t2,t3;
employee()
{ super("employee table");
Object[][]data=null;
String[] head={"employee no","name","age"};;
dtm=new DefaultTableModel(0,3);
dtm.setDataVector(data,head);
jt=new JTable(dtm);
jt.setGridColor(Color.RED);
jt.setRowSelectionAllowed(true);
jsp=new JScrollPane(jt);
JPanel p1=new JPanel();
p1.add(jsp);
l1=new JLabel("employee no.:");
l2=new JLabel("Name:");
l3=new JLabel("Age:");

t1=new JTextField(5);
t2=new JTextField(13);
t3=new JTextField(3);
JButton b1=new JButton("ADD");
b1.addActionListener(this);
JPanel p2=new JPanel();
p2.add(l1);
p2.add(t1);
p2.add(l2);
p2.add(t2);
p2.add(l3);
p2.add(t3);
p2.add(b1);
Container c=getContentPane();
c.add(p1,BorderLayout.NORTH);
c.add(p2,BorderLayout.CENTER);
setSize(600,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
int empno=Integer.parseInt(t1.getText());
String name=t2.getText();
int age=Integer.parseInt(t3.getText());
Object[] data={empno,name,age};
dtm.addRow(data);

t1.setText("");
t2.setText("");
t3.setText("");
}
public static void main(String[] args) {
new employee();
}
}

OUTPUT:

PRACTICAL 7
Write a java program using SplitPane to demonstrate a screen
divide into two parts,one part contains names of planet and

another displays image of the planet.when user selects the


planet name from left screen, appropriate image of planet
display in right screen.
Source code:
package planet;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Planet extends JFrame implements ListSelectionListener{
JList list1;
JScrollPane jsp1;
JLabel lb1;
JSplitPane pane;
ImageIcon img1,img2,img3,img4,img5,img6,img7,img8,img9;
Container c;
Planet()
{
super("planet display");
c=getContentPane();
String
[]str={"mercury","venus","earth","mars","jupiter","saturn","urnas","neptune","pluto"};
list1=new JList(str);
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list1.addListSelectionListener(this);
jsp1=new JScrollPane(list1);
img1=new ImageIcon("mercury.jpg");
img2=new ImageIcon("venus.jpg");
img3=new ImageIcon("earth.jpg");
img4=new ImageIcon("mars.jpg");
img5=new ImageIcon("jupiter.jpg");
img6=new ImageIcon("saturn.jpg");
img7=new ImageIcon("urnas.jpg");
img8=new ImageIcon("neptune.jpg");
img9=new ImageIcon("pluto.jpg");
lb1=new JLabel("");
pane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jsp1,lb1);
pane.setBounds(40, 100, 350, 250);
c.add(pane);
setSize(400,400);
setDefaultCloseOperation(3);
setVisible(true);
}
public void valueChanged(ListSelectionEvent e) {
int ch=list1.getSelectedIndex()+1;
switch(ch)
{
case 1:lb1.setIcon(img1);break;
case 2:lb1.setIcon(img2);break;
case 3:lb1.setIcon(img3);break;

case 4:lb1.setIcon(img4);break;
case 5:lb1.setIcon(img5);break;
case 6:lb1.setIcon(img6);break;
case 7:lb1.setIcon(img7);break;
case 8:lb1.setIcon(img8);break;
case 9:lb1.setIcon(img9);break;
default:lb1.setText("image not found.");
}
}
public static void main(String[] args)
{
new Planet();
}
}

OUTPUT:

Q.2) write a java program to create a tree with root node as


SEM V and sub node as NS,ST,NET,LA,AJ.
SOURCE CODE:
package planet;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class Tree extends JFrame
{
private JTree jt;
DefaultMutableTreeNode semv,NS,ST,NET,LA,AJ;
public Tree()
{
semv=new DefaultMutableTreeNode("sem v",true);
NS=new DefaultMutableTreeNode("NS");
ST=new DefaultMutableTreeNode("ST");
NET=new DefaultMutableTreeNode(".NET");
LA=new DefaultMutableTreeNode("LA");
AJ=new DefaultMutableTreeNode("AJ");
semv.add(NS);
semv.add(ST);
semv.add(NET);
semv.add(LA);
semv.add(AJ);
jt=new JTree(semv);
add(jt);
}
public static void main(String[] args)throws Exception
{
JFrame f=new Tree();
f.setTitle("My Frame");
f.setSize(400,400);
f.setVisible(true);
}
}

OUTPUT:

Q.3) write a java program to create a menubar with two menu


file and edit ,where file has two submenu open & new.and edit
has submenu as edit.
SOURCE CODE:
package planet;
import java.awt.*;
import javax.swing.*;
public class Menu extends JFrame
{
private JMenuBar mb;
private JMenu file,edit;
private JMenuItem open,cut,New;
public Menu()
{
setLayout(new FlowLayout());
mb=new JMenuBar();
setJMenuBar(mb);
file=new JMenu("File");
edit=new JMenu("Edit");
mb.add(file);
mb.add(edit);
open=new JMenuItem("open");
New=new JMenuItem("New");
cut=new JMenuItem("Cut");
file.add(open);
file.addSeparator();
file.add(New);
edit.add(cut);
}
public static void main(String[] args)throws Exception
{
JFrame f=new Menu();
f.setTitle("My Frame");
f.setSize(400,400);
f.setVisible(true);
}
}

OUTPUT:

PRACTICAL 8
1)create a JSF application for inventory of product.
MYSQL COMMAND:

INDEX.XHTML:

<?xml version='1.0' encoding='UTF-8' ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Product Form</title>
</h:head>
<h:body>
<h:form>
Product Detail Form
<h:panelGrid columns="2" cellspacing="1" cellpadding="1">
Product code<h:inputText id="txtcode" value="#{b1.pcode}"/>
Product name<h:inputText id="txtname" value="#{b1.pname}"/>
<h:commandButton id="cmdAdd" value="ADD" action="#{b1.add}"/>
</h:panelGrid>
</h:form>
</h:body>
</html>s

SUCCESS.XHTML:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Product Form</title>
</h:head>
<h:body>
Product Details Added Successfully
</h:body>
</html>
ERROR.XHTML:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Product Form</title>
</h:head>
<h:body>
Error in data entry
</h:body>
</html>
BEAN.JAVA:
package bean;
import java.sql.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class bean
{
private int pcode;
private String pname;
public int getPcode()
{
return pcode;
}
public void setPcode(int pcode)
{
this.pcode = pcode;
}
public String getPname()
{

return pname;
}
public void setPname(String pname)
{
this.pname = pname;
}
public String add()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/prod133","root","tiger");
PreparedStatement ps=con.prepareStatement("insert into product133 values(?,?)");
ps.setInt(1,pcode);
ps.setString(2,pname);
ps.executeUpdate();
return "yes";
}
catch(Exception e)
{
return "no";
}
}
}
FACES-CONFIG.XML:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>yes</from-outcome>
<to-view-id>/success.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>no</from-outcome>
<to-view-id>/error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>b1</managed-bean-name>
<managed-bean-class>bean.bean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>

OUTPUT:

2)create a JSF Application for checking the validity of a user.

INDEX1.XHTML:

<?xml version='1.0' encoding='UTF-8' ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Login Form</title>
</h:head>
<h:body>
<h:form>
The Login Form
<h:panelGrid columns="2" cellspacing="1" cellpadding="1">
Name<h:inputText id="txtname" value="#{b1.name}"/>
Password<h:inputSecret id="txtpass" value="#{b1.pass}"/>
<h:commandButton id="cmdLogin" value="login" action="#{b1.perform}"/>
</h:panelGrid>
</h:form>
</h:body>
</html>

WELCOME.XHTML:

<?xml version='1.0' encoding='UTF-8' ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Login Form</title>
</h:head>
<h:body>

<h:outputText id="txtname" value="welcome#{b1.name}"/>


</h:body>
</html>
ERROR1.XHTML:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Login Form</title>
</h:head>
<h:body>
Invalid User!!!
<h:commandButton id="cmdBack" value="back" action="#{b1.back}"/>
</h:body>
</html>
BEAN1.JAVA:
package bean1;
import java.sql.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class bean1
{
private String name,pass;
public bean1()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String perform()
{
try
{

Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/log133","root","tiger");
PreparedStatement ps=con.prepareStatement("select * from login133 where
name=? and pass=?");
ps.setString(1,name);
ps.setString(2,pass);
ResultSet rs=ps.executeQuery();
if(rs.isBeforeFirst())
return "welcome";
else
return "error";
}
catch(Exception e)
{
return "error";
}
}
public String back()
{
return "index1";
}
}
FACES-CONFIG.XML:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>/index1.xhtml</from-view-id>
<navigation-case>
<from-outcome>welcome</from-outcome>
<to-view-id>/welcome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>/error1.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/error1.xhtml</from-view-id>
<navigation-case>
<from-outcome>index</from-outcome>
<to-view-id>/index1.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>b1</managed-bean-name>
<managed-bean-class>bean1.bean1</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>

</managed-bean>
</faces-config>
OUTPUT:

PRACTICAL 9
Q 1) develop a stateless session bean for Room Reservation
System.Creates an application to accept number of rooms for
CHECKIN and CHECKOUT from the client ,according to that
changes are made in Database by using EJB.

Mysql command:

File name: roombeanLocal.java


package EJB;
import javax.ejb.Local;
@Local
public interface roombeanLocal {
public int checkin(int no);
public int checkout(int no);
}

File name: roombean.java


package EJB;
import java.sql.*;
import java.sql.DriverManager;
import javax.ejb.Stateless;
@Stateless
public class roombean implements roombeanLocal {
@Override
public int checkin(int no) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/registration","root","tiger");

String sql1="select * from room";


Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql1);
rs.next();
int total=rs.getInt(1);
int occ=rs.getInt(2);
int free=total-occ;
System.out.println(total);
System.out.println(free);
if(free>=no)
{
String sql2="update room set occ=?";
PreparedStatement ps=con.prepareStatement(sql2);
ps.setInt(1,occ+no);
int res=ps.executeUpdate();
return res;
}
else return 0;
}
catch(Exception e)
{
return 0;
}
}
@Override
public int checkout(int no) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/registration","root","tiger");
String sql1="select * from room";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql1);
rs.next();
int total=rs.getInt(1);
int occ=rs.getInt(2);
if(occ>=no)
{
String sql2="update room set occ=?";
PreparedStatement ps=con.prepareStatement(sql2);
ps.setInt(1,occ-no);
int res=ps.executeUpdate();
return res;
}
else return 0;
}
catch(Exception e)

{
return 0;
}
}
}

File name: room.java


<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="post" action="roomcl">
<br>NO of Rooms :<input type="text" name="t1">
<br><input type="submit" value="CHECKIN" name="btn">
<br><input type="submit" value="CHECKOUT" name="btn">
</form>
</body>
</html>

File name: roomcl.java


import
import
import
import
import
import
import
import
import

EJB.roombeanLocal;
java.io.IOException;
java.io.PrintWriter;
javax.ejb.EJB;
javax.servlet.ServletException;
javax.servlet.annotation.WebServlet;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

@WebServlet(name = "roomcl", urlPatterns = {"/roomcl"})


public class roomcl extends HttpServlet {
@EJB roombeanLocal obj;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
int no=Integer.parseInt(request.getParameter("t1"));
String b=request.getParameter("btn");
int res=0;
if(b.equals("CHECKIN"))
{
res=obj.checkin(no);
if(res==1)
out.println(no+"room check in");
}
if(b.equals("CHECKOUT"))
{

res=obj.checkout(no);
if(res==1)
out.println(no+"room check out");
}
if(res==0)
out.println("not possible to do CHECK IN/OUT");
out.println("<br><br><a href=room.html>BACK </a>");
} finally {
out.close();
}
}
}

Output:
1.CHECKIN

2.CHECKOUT

Practical No. 10
Hibernate Feedback of Website Visitor
Program :MySql Command :1) create database db;
2) use db;
3) create table guestbook(no int primary key auto_increment, name varchar(20),msg
varchar(100), dt varchar(40));
File name - Guestbook.java
package hibernate;
public class Guestbook implements java.io.Serializable {

private Integer no;


private String name;
private String msg;
private String dt;

public Guestbook() {
}

public Guestbook(String name, String msg, String dt) {


this.name = name;
this.msg = msg;
this.dt = dt;
}
public Integer getNo() {
return this.no;
}
public void setNo(Integer no) {
this.no = no;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;

}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getDt() {
return this.dt;
}
public void setDt(String dt) {
this.dt = dt;
}
}

File name - hibernate.cfg.xml


<hibernate-configuration>
<session-factory>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">tiger</property>
<mapping resource="hibernate/Guestbook.hbm.xml"/>

</session-factory>
</hibernate-configuration>

File name - Guestbook.hbm.xml


<hibernate-mapping>
<class name="hibernate.Guestbook" table="guestbook" catalog="db">
<id name="no" type="java.lang.Integer">
<column name="no" />
<generator class="identity" />
</id>
<property name="name" type="strin g">
<column name="name" length="20" />
</property>
<property name="msg" type="string">
<column name="msg" length="100" />
</property>
<property name="dt" type="string">
<column name="dt" length="40" />
</property>
</class>
</hibernate-mapping>
File name - index.jsp
<html>

<head>
<title>Guest Book</title>
</head>
<body>
Guest Book <hr> <br> <br>
<form action="guestbookview.jsp" method="post">
Name <input type="text" name="name" maxlength="20"> <br>
Message <textarea rows="5" cols="40" maxlength="100"
name="msg"></textarea>
<br> <input type="submit" value="submit">
</form>
</body>
</html>

File name - guestbookview.jsp


<%@page import="org.hibernate.SessionFactory"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="hibernate.Guestbook"%>

<%!
SessionFactory sf;
org.hibernate.Session ss;
List<hibernate.Guestbook> gbook;
%>
<%
sf = new Configuration().configure().buildSessionFactory();
ss= sf.openSession();
Transaction tx=null;
Guestbook gb=new Guestbook();
try
{
tx=ss.beginTransaction();
String name=request.getParameter("name");
String msg=request.getParameter("msg");
String dt=new java.util.Date().toString();
gb.setName(name);
gb.setMsg(msg);
gb.setDt(dt);
ss.save(gb);
tx.commit();
}
catch(Exception e){ out.println("Error"+e.getMessage()); }
try
{
ss.beginTransaction();
gbook=ss.createQuery("from Guestbook").list();

}
catch(Exception e){ }
%>
<html>
<head>
<title>Guest View</title>
</head>
<body>
Guest View
Click here to go <a href="index.jsp"> BACK </a>
<br><br>
<%
Iterator it=gbook.iterator();
while(it.hasNext())
{
Guestbook each=(Guestbook)it.next();
out.print(each.getDt()+" ");
out.print(each.getName()+"<br>");
out.print(each.getMsg()+"<br><br>");
}
%>
</body>

</html>

Output :-

Steps:

Practical No. 11
Struts E-mail Validator
AIM
Develop a simple Struts Application to Demonstrate E-mail Validator.

Description

I.

II.

III.

IV.

Create a Struts project


File -> New Project (Select Java Web and Web Application ) Next (Give the Project
Name; change the project location as required; select the checkboxes Dedicated folder for
storing libraries ) Next
Select Glassfish Server Next Select the framework Struts 1.3.10(select the check
box Add struts TDL) Finish
Creating Views
To add views Right click on the project -> New -> JSP (Add the required
number of JSP pages) eg (login, success, failure)
Writing business login (Model)
A. Design Action bean
To add StructsActionForm bean, Right click on the project -> other ->
select(Struts and StrutsActionFormBean) -> next
Give the bean class name (bean1) -> select the package name from the
list -> Finish
Include the variables (username and email) into the action bean and
insert setter and getter method
B. Design Action
To add Action, Right click on the project -> other -> select(Struts
and StrutsAction) -> next
Give the class name (LoginAction) -> select the package from the
list -> type the action path as (/login) -> next
Select the action form bean(bean1) -> keep input resource blank ->
select the scope as request -> uncheck validate ActionForm Bean
checkbox -> Finish
Modify the execute() method.
Configuring controllers and mapping in struts-config.xml
A. Configuration File
Select the struts-config.xml file from the configuration files
Right click within the <action> tag
Select Struts->Add forward
Select the forward name (success) and select the resource
file(success.jsp)
Repeat and Add all the forwards

B. Mapping file
Select the web.xml file from the configuration files
Click the pages tag
Select the welcome files (login.jsp)
Pracs11:struts:
Login.jsp:
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form name="login" action="login.do">
<br>Username
<input type="text" name="username" value="">
<br>Email ID
<input type="text" name="email" value="">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Success.jsp:
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Success Page</title>
</head>
<body>
Email Validation is done...

<br>Your entered details are


<br>Username
<bean:write name="bean1" property="username" />
<br>email
<bean:write name="bean1" property="email" />
</body>
</html>
Failure.jsp:
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Failure Page</title>
</head>
<body>
Wrong Email ID..
<br>Your entered details are
<br>Username
<bean:write name="bean1" property="username" />
<br>email
<bean:write name="bean1" property="email" />
</body>
</html>
Struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC


"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans>
<form-bean name="bean1" type="com.myapp.struts.bean1"/>

</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>

<action-mappings>
<action name="bean1" path="/login" scope="request"
type="com.myapp.struts.loginaction1" validate="false">
<forward name="login" path="/login.jsp"/>
<forward name="success" path="/success.jsp"/>
<forward name="failure" path="/failure.jsp"/>
</action>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>

<plug-in className="org.apache.struts.tiles.TilesPlugin" >


<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>

<!-- ========================= Validator plugin


================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>

Bean1.java(form bean)
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class bean1 extends org.apache.struts.action.ActionForm {

private String username;


private String email;

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public String getUsername() {


return username;
}

public void setUsername(String username) {


this.username = username;
}

}
Loginaction1.java

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class loginaction1 extends org.apache.struts.action.Action {

private static final String SUCCESS = "success";


private static final String FAILURE = "FAILURE";

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
bean1 b1=(bean1)form;
String m=b1.getEmail();
if (m == null || m.equals("") || m.indexOf("@") == -1) {
return mapping.findForward(FAILURE);
}
else{
return mapping.findForward(SUCCESS);
}

}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>

</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
Output:

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