Java Lab File Modify1
Java Lab File Modify1
EXPERIMENT -1
PROCEDURE:
OUTPUT:
RESULT:
The RMI Application to add two numbers runs successfully.
EXPERIMENT -2
PROCEDURE:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="Calculator1" width=300 height=300></applet>*/
public class Calculator1 extends Applet implements Calc
{
Text t;
Button b[]=new Button[15];
Button b1[]=new Button[6];
String op2[]={“+”,”-”,”*”,”%”,”=”,”C”};
String str1="";
int p=0,q=0;
String oper;
public void init()
{
setLayout(new GridLayout(5,4));
t=new Text(20);
setBackground(Color.pink);
setFont(new Font("Arial",Font.BOLD,20));
int k=0;
t.setEditable(false);
t.setBackground(Color.white);
t.setText("0");
for(int i=0;i<10;i++)
{
b[i]=new Button(""+k);
add(b[i]);
k++;
b[i].setBackground(Color.pink);
b[i].addCalc(this);
}
for(int i=0;i<6;i++)
{
b1[i]=new Button(""+op2[i]);
add(b1[i]);
b1[i].setBackground(Color.pink);
b1[i].addCalc(this);
}
add(t);
}
public void operation(oprtn op)
{
String str=op.getoperationcommand();
if(str.equals("+"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("-"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("*"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("%"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("="))
{ str1="";
if(oper.equals("+")) {
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p+q)));}
else if(oper.equals("-")) {
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p-q))); }
else if(oper.equals("*")){
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p*q))); }
else if(oper.equals("%")){
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p%q))); }
}
else if(str.equals("C")){ p=0;q=0;
t.setText("");
str1="";
t.setText("0");
}
else{ t.setText(str1.concat(str));
str1=t.getText();
}
}
}
Output:
RESULT: Calculator runs successfully.
EXPERIMENT -3
PROCEDURE:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class GFG extends Applet implements MouseListener
{
private int x,y;
private String str = " ";
public void init()
{
this.addMouseListener (this);
}
public void paint(Graphics g)
{
g.drawString(str,x,y);
}
public void update(Graphics g)
{
paint(g);
}
public void mouseClicked(MouseEvent m)
{
x = m.getX();
y = m.getY();
str = "x =" +x+",y = "+y;
repaint();
}
public void mouseEntered(MouseEvent m)
{
}
public void mouseExited(MouseEvent m)
{
}
public void mousePressed(MouseEvent m)
{
}
public void mouseReleased(MouseEvent m)
{
}
Output:
EXPERIMENT -4
OBJECTIVE: Write a program to create a table and Insert values and display them using
JDBC.
PROCEDURE:
package jdbc_program;
import java.sql.*;
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM saurabh";
try (ResultSet rs = stmt.executeQuery(sql)) {
while(rs.next()){
int no = rs.getInt("A");
String A = rs.getString("A");
}
}
stmt.close();
conn.close();
}
catch(SQLException | ClassNotFoundException se){
}
finally{
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
}
}
System.out.println("Goodbye!");
}
RESULT:
Records in database are successfully inserted using JDBC program.
EXPERIMENT -5
OBJECTIVE: Write a program update table values and display them.
PROCEDURE:
RESULT:
Records in database are successfully manipulated using servlet and JSP programs.
EXPERIMENT -6
OBJECTIVE: Write a program in JSP to provide Login Password Functionality using
Database Driver.
PROCEDURE:
//index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
<FORM action="check.jsp" method="get">
<table align="center"> <br><br><br><br><br><br><br><br><br><br>
<tr>
<td>
<b>Admin Login:-<br><br>
Username:<input type="text" size=20 maxlength=20 name="name"><br>
Password: <input type="password" size=20 maxlength=20 name="rollno"></b>
<input type="Submit" value="LOGIN"<center/></td>
</table>
</FORM>
</body>
</html>
//check.jsp
<%@page contentType="text/html"
import="java.sql.*,java.util.*,java.text.*,java.util.Date,java.net.*" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%! String rollno,name; %>
<%
Connection con1=null;
Statement stmt1=null;
ResultSet rs1=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException cnfe){ }
try{ con1 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/s7093","root","s7093");
stmt1 = con1.createStatement();rs1 = stmt1.executeQuery("select *FROM saurabh WHERE
rollno='"+request.getParameter("rollno")+"'AND name='"+request.getParameter("name")
+"'");
while(rs1.next()){
rollno = rs1.getString("rollno");
name= rs1.getString("name");
}
if(name.equals(request.getParameter("name")) &&
rollno.equals(request.getParameter("rollno")))
{
response.sendRedirect("correct.jsp?name="+request.getParameter("name")+"");
}
else
{
response.sendRedirect("incorrect.jsp");
}
}
catch(SQLException sqle){ }
%>
</body> </html>
//correct.jsp
OUTPUT:
RESULT: Login page created successfully.
EXPERIMENT -7
PROCEDURE:
package ds;
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class DateServ extends GenericServlet {
public DateServ() {
System.out.println("SERVLET LOADED...");
}
public void init()
{}
public void service(ServletRequest req, ServletResponse res) throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Date d = new Date();
String s = d.toString();
pw.println("<h1> WELCOME TO SERVLETS <h1>");
pw.println("<h2> CURRENT DATE & TIME IS : " + s + "<h2>");
}
public void destroy()
}
};
EXPERIMENT -8
OBJECTIVE: Write a program using servlet to write persistent and non-persistent cookies on
client side.
SOFTWARE USED: NetBeans, JDK 1.7, Google Chrome
PROCEDURE:
//Cook.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.*;
public class Cook extends HttpServlet {
private static final long serialVersionUID = 1L;
public Cook()
{
super();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("Advanced Java Lab");
boolean newbie = true;
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
for(int i=0; i<cookies.length; i++)
{
Cookie c = cookies[i];
if ((c.getName().equals("repeatVisitor")) && (c.getValue().equals("yes"))) {
newbie = false;
break;
}
String title;
if (newbie) {
Cookie returnVisitorCookie =new Cookie("repeatVisitor", "yes");
returnVisitorCookie.setMaxAge(60*60*24*365); // 1 year
response.addCookie(returnVisitorCookie);
title = "Welcome Aboard";
}
else {
title = "Welcome Back";
}
out.println("<H1>Setting and Reading Cookies</H1>");
out.println(title);
out.println("</body>");
out.println("</html>");
}
finally {
out.close(); }
}
}
OUTPUT:
RESULT: Program for persistent and non persistent cookies runs successfully.
EXPERIMENT -9
OBJECTIVE: Write a program to print server side information using JSP as Client IP Address ,
URL , Context Info, hit count.
//hit.jsp
OUTPUT: