21. Create a Java class called Product with the following properties...
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
22. Create a servlet filter that logs all access to and from servlets...
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
23. Develop a interest calculation application in which user will...
//HTML
<html>
<head>
<script language="javascript">
function check(form)
{
if(form.total.value=="")
{
form.total.value="Enter Valid Value";
}
if(form.rate.value=="")
{
form.rate.value="Enter Valid Value";
}
if(form.duration.value=="")
{
form.duration.value="Enter Valid Value";
}
}
</script>
</head>
<body>
<form action="/myapp/servlet/interest">
<center>
<big><blink>INTEREST CALCULATOR</blink></big><br/><br/>
<b>Total Amount :</b><input type="text" name="total"><br/>
<b>Interest Rate :<b><input type="text" name="rate" > %<br/>
<b>Duration :</b><input type="text" name="duration" onBlur="check(this.form)">Months<br/>
<br/><input type="submit" value="Calculate" >
</center>
</body>
</html>
//interest.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class interest extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
float total=Float.parseFloat(request.getParameter("total"));
float rate=Float.parseFloat(request.getParameter("rate"));
float duration=Float.parseFloat(request.getParameter("duration"));
float intr=(total*rate*duration)/100;
total=total+intr;
out.println("<html><body>");
out.println("<B><h3>your Interest Amount is "+intr);
out.println("And Total Amout will be "+total+"</h3><b>");
out.println("</body></html>");
}
}
24. Develop an application to demonstrate how the client (browser)...
//gtu24.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/** Servlet that uses session tracking to keep per-client
* access counts. Also shows other info about the session.
*/
public class gtu24 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
Date d1 = new Date(session.getCreationTime());
Date d2 = new Date(session.getLastAccessedTime());
double d;
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";
out.println("<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=\"#FFAE00\">\n" +
" <TH>Info Type<TH>Value\n" +
"<TR>\n" +
" <TD>Creation Time\n" +
" <TD>" +
d1 + "\n" +
"<TR>\n" +
" <TD>Time of Last Access\n" +
" <TD>" +
d2 + "</TD> \n" +
"<TR>" );
Dates dt = new Dates();
d = dt.DifferenceInSeconds(d2,d1);
out.println("<TD>Duration Since Last Access </TD>"+ "\n" +
"<TD>" + d + "(Seconds)</TD>"+ "\n" +
"<TR>\n"+
"<TD>Number of Previous Accesses\n" +
"<TD>" + accessCount + "\n" +
"</TABLE> \n" +
"</CENTER></BODY></HTML>");
}
}
//Dates.java
import java.io.*;
import java.util.*;
public class Dates
{
public static double DifferenceInMonths(Date date1, Date date2)
{
return DifferenceInYears(date1, date2) * 12;
}
public static double DifferenceInYears(Date date1, Date date2)
{
double days = DifferenceInDays(date1, date2);
return days / 365.2425;
}
public static double DifferenceInDays(Date date1, Date date2)
{
return DifferenceInHours(date1, date2) / 24.0;
}
public static double DifferenceInHours(Date date1, Date date2)
{
return DifferenceInMinutes(date1, date2) / 60.0;
}
public static double DifferenceInMinutes(Date date1, Date date2)
{
return DifferenceInSeconds(date1, date2) / 60.0;
}
public static double DifferenceInSeconds(Date date1, Date date2)
{
return DifferenceInMilliseconds(date1, date2) / 1000.0;
}
public static double DifferenceInMilliseconds(Date date1, Date date2)
{
return Math.abs(GetTimeInMilliseconds(date1) - GetTimeInMilliseconds(date2));
}
private static long GetTimeInMilliseconds(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getTimeInMillis() + cal.getTimeZone().getOffset(cal.getTimeInMillis());
}
}
//web_xml_entry
<servlet>
<servlet-name>gtu_24</servlet-name>
<servlet-class>gtu24</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gtu_24</servlet-name>
<url-pattern>/24</url-pattern>
</servlet-mapping>
25. Develop an application to keep track of one user across several..
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
26. Develop an application to write a "page-composite" JSP...
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
27. You want to reduce the amount of Java coding in your JSP using...
Shared By : Mihirgiri Gosai (College - Anand Institute of Information Science (AIIS) )
Show/Hide Program
MyBean.java
-----------
package my;
public class MyBean
{
private String name=new String();
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
UseBean.jsp
-----------
<html>
<title>Java bean example in jsp</title>
<head>
<h1>Java bean example in jsp</h1>
</head>
<body>
<jsp:useBean id="mybean" class="my.MyBean" scope="session" >
<jsp:setProperty name="mybean" property="name" value=" Hello world" />
</jsp:useBean>
<h1> <jsp:getProperty name="mybean" property="name" /></h1>
</body>
</html>
28. Develop a program to perform the database driven operation...
NOTE : 1. use this DATABASE (download)
2. create system dsn not user dsn...
---------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class DbOperations extends HttpServlet
{
private String Name, desig, j_date;
private int id, emp_sal;
private String msg, action;
public void init()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Loads Driver...
}
catch(ClassNotFoundException nf)
{
//System.out.println(nf);
}
}
public void sendEmployeeForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<BR> <H2> Employee Form </H2></BR>");
out.println("<BR> Please Enter Details </BR>");
out.println("<BR> <FORM Method = POST>");
out.println("<TABLE>");
out.println("<TR><TD> Employee ID :</TD><TD><INPUT TYPE = TEXT Name = eid></TD></TR>");
out.println("<TR><TD> Employee Name :</TD><TD><INPUT TYPE = TEXT Name = ename></TD></TR>");
out.println("<TR><TD> Employee Designation :</TD><TD><INPUT TYPE = TEXT Name = edesig></TD></TR>");
out.println("<TR><TD> Employee Joining Date :</TD><TD><INPUT TYPE = TEXT Name = ejdate></TD></TR>");
out.println("<TR><TD> Employee Salary :</TD><TD><INPUT TYPE = TEXT Name = esal></TD></TR>");
out.println("</TABLE>");
out.println("<TABLE>");
out.println("<TR><TD> <INPUT TYPE = SUBMIT Value = Insert name=val></TD>");
out.println("<TD><INPUT TYPE = SUBMIT Value = Delete name=val></TD>");
out.println("<TD><INPUT TYPE = SUBMIT Value = Update name=val></TD>");
out.println("<TD><INPUT TYPE = SUBMIT Value = Search name=val></TD>");
out.println("<TD><INPUT TYPE = RESET Value = Clear></TD></TR>");
out.println("</TABLE>");
out.println("</FORM>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String mysql = null;
sendPageHeader(response);
id = Integer.parseInt(request.getParameter("eid"));
Name = request.getParameter("ename");
desig = request.getParameter("edesig");
j_date = request.getParameter("ejdate");
emp_sal = Integer.parseInt(request.getParameter("esal"));
try
{
action = request.getParameter("val");
PrintWriter out = response.getWriter();
Connection con = DriverManager.getConnection("jdbc:odbc:Employee");
Statement st = con.createStatement();
mysql = "Select * From empInfo ";
mysql = mysql + " Where Empname = '" + id + "'";
ResultSet rs = st.executeQuery(mysql);
out.println("Parameter Value = " +action);
if(action.toString()=="Insert")
{
if(rs.next())
{
rs.close();
msg = " Duplicate Employee ... Enter other name ...";
}
else
{
rs.close();
mysql = "Insert into empInfo";
mysql = mysql + "(EmpId,Empname,Emp_desig,Emp_J_Date,Emp_Salary)";
mysql = mysql + " Values ("+ id + ",'";
mysql = mysql + Name + "','";
mysql = mysql + desig + "','";
mysql = mysql + j_date + "',";
mysql = mysql + emp_sal +")";
out.println(mysql+"<BR>");
int i = st.executeUpdate(mysql);
if(i==1)
{
msg = " Successfully Added One Employee..." + Name;
}
}
}
else if(action.toString()=="Update")
{
}
st.close();
con.close();
}catch(SQLException se)
{
PrintWriter pw = response.getWriter();
pw.println("<BR> <B> SQL Statement : ===> " + mysql + " </B>");
pw.println("<BR> <B> Some Error ==> " + se.toString() + " </B>");
pw.println("<HR><BR>");
}
sendPageFooter(response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
sendPageHeader(response);
sendEmployeeForm(request,response);
sendPageFooter(response);
}
public void sendPageHeader(HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE> Employee Database Driven Operation </TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<CENTER>");
}
public void sendPageFooter(HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("</TABLE>");
out.println("</CENTER>");
out.println("</BODY>");
out.println("</HTML>");
}
}
29. Develop a Java application to perform the database...
NOTE : 1. use this DATABASE (download)
2. create system dsn not user dsn...
---------------------------------------------------
//GTU 29
import java.sql.*;
import java.util.Scanner;
public class gtu29 {
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnf)
{
System.out.println("Error : "+cnf);
}
String mysql = null;
int ch=0;
while(ch!=5)
{
System.out.println("\n====== MAIN MENU ===============\n");
System.out.println("1. Insert");
System.out.println("2. Update");
System.out.println("3. Delete");
System.out.println("4. Search");
System.out.println("5. Exit");
System.out.print("Enter Your Choice :");
Scanner s = new Scanner(System.in);
ch = s.nextInt();
try
{
Connection con = DriverManager.getConnection("jdbc:odbc:Employee");
PreparedStatement st;
int i;
switch(ch)
{
case 1:
mysql = "insert into empInfo values(?,?,?,?,?)";
st = con.prepareStatement(mysql);
st.setInt(1, 1004);
st.setString(2,"Pandya Chaitanya");
st.setString(3,"Manager");
st.setString(4,"20-01-2010");
st.setInt(5,15000);
st.executeUpdate();
i = st.executeUpdate();
if(i == 1)
{
System.out.println("Inserted.....");
}
break;
case 2:
mysql = "UPDATE empInfo SET Emp_Salary = ? WHERE empid = ?";
st = con.prepareStatement(mysql);
st.setInt(1,5000);
st.setInt(2,1004);
st.executeUpdate();
i = st.executeUpdate();
if(i == 1)
{
System.out.println("Updated.....");
}
break;
case 3:
mysql = "delete from empInfo where EmpId = ?";
st = con.prepareStatement(mysql);
st.setInt(1,1004);
st.executeUpdate();
i = st.executeUpdate();
if(i >= 1)
{
System.out.println("Deleted.....");
}
break;
case 4:
mysql = "select * from empInfo where empid = ?";
st = con.prepareStatement(mysql);
st.setInt(1,1004);
ResultSet rs = st.executeQuery();
System.out.println("Empid \t EmpName \t EmpDesig \t EmpJ_Date \t Salary");
while(rs.next())
{
System.out.print(rs.getInt(1));
System.out.print("\t"+rs.getString(2));
System.out.print("\t"+rs.getString(3));
System.out.print("\t"+rs.getString(4));
System.out.print("\t"+rs.getInt(5)+"\n"));
}
break;
case 5:
System.exit(0);
}
}
catch(SQLException se)
{
System.out.println("Error in SQL : "+se);
}
}
}
}
30. Write a Java application to invoke a stored procedure using a...
//incrementsalary.sql
set serveroutput on
create or replace procedure IncSal(percent IN NUMBER)
is
BEGIN
UPDATE employee
SET empsal = empsal + (empsal*(percent/100));
commit;
END IncSal;
/
//JAVA File
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class p30 extends HttpServlet
{
public void init() throws ServletException
{
try
{
// Loads Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(Exception e)
{
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String query;
try
{
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@://localhost:1521/orcl","scott","tiger");
Statement st= con.createStatement();
String operation=request.getParameter("button");
//out.println(operation);
if(operation.equals("Update"))
{
float inc=Float.parseFloat(request.getParameter("inc"));
query="{ call IncSal(?) }";
CallableStatement cst=con.prepareCall(query);
cst.setFloat(1,inc);
cst.execute();
out.println("Incremented successfully");
}
if(operation.equals("View"))
{
query="select * from employee";
ResultSet rs=st.executeQuery(query);
out.println("<html>");
out.println("<body>");
out.println("<center>");
out.println("<h1> Employee Information </h1>");
out.println("<br><br>");
out.println("<table border=1 color=red bgcolor=yellow>");
out.println("<th> EmpId </th>");
out.println("<TH> EmpName </th>");
out.println("<TH> EmpDesignation </th>");
out.println("<TH> EmpJoining Date </th>");
out.println("<TH> EmpSalary </th>");
if(rs==null)
{
out.println("<h2> No Records </h2> ");
}
else
{
while(rs.next())
{
out.println("<tr>");
out.println("<td>");
out.println(rs.getString("empid"));
out.println("</td>");
out.println("<td>");
out.println(rs.getString("empname"));
out.println("</td>");
out.println("<td>");
out.println(rs.getString("empdes"));
out.println("</td>");
out.println("<td>");
out.println(rs.getString("empjd"));
out.println("</td>");
out.println("<td>");
out.println(rs.getString("empsal"));
out.println("</td>");
out.println("</tr>");
}
rs.close();
st.close();
con.close();
}
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
out.println(e);
}
}
}
//JSP file
<HTML>
<HEAD>
</HEAD>
<BODY>
<h1>Increment Salary of Employee Table percent wise</h1>
<FORM action="/myapp/servlet/p30">
<TABLE>
<TR><TD>Increment percentage:<td><input type="text" name="inc"> %
<TR><TD><INPUT TYPE="radio" name="button" value="Update">:Increment Salary
<TD><INPUT TYPE="radio" name="button" checked value="View">View
<tr><td align="center"><input type="submit" value="GO">
<td><input type="reset" value="reset">
</TABLE>
</FORM>
</BODY>
</HTML>
NOTE : 1. use this DATABASE (download)
2. create system dsn not user dsn...
---------------------------------------------------
//GTU 30
import java.sql.*;
import java.util.Scanner;
public class gtu30 {
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Employee");
Scanner s = new Scanner(System.in);
System.out.print("\nEnter Percentage : ");
float per = s.nextFloat();
String sql = "CREATE PROCEDURE incrementSalary(per IN NUMBER)" +
" AS BEGIN "+
" UPDATE EmpInfo SET Emp_Salary = " +
" Emp_Salary + (Emp_Salary * " + (per/100) + ") End;";
System.out.println(sql);
Statement st = con.createStatement();
st.executeUpdate(sql);
st.close();
CallableStatement cst = null;
con.prepareCall("{ = call incrementSalary(?) }");
cst.setFloat(1,per);
cst.execute();
int i = cst.executeUpdate(sql);
if(i == 1)
{
System.out.println("Updated Successfully....");
}
cst.close();
con.close();
}catch(ClassNotFoundException cnf)
{
System.out.println("Error : "+cnf);
}catch(SQLException se)
{
System.out.println("Error : "+se);
}
}
}