Web Technology (Final File)
Web Technology (Final File)
Objective: Write a Java applet to display the Application Program screen i.e. Colour mixer palletCode:-
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
// Create sliders
redSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
greenSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
blueSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
// Add listeners
redSlider.addAdjustmentListener(this);
greenSlider.addAdjustmentListener(this);
blueSlider.addAdjustmentListener(this);
// Create labels
redLabel = new Label("Red: 0");
greenLabel = new Label("Green: 0");
blueLabel = new Label("Blue: 0");
Client.java
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) {
try {
// Create client socket
Socket clientSocket = new Socket("localhost", 5000);
System.out.println("Connected to server...");
// Create input and output streams
InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream();
// Create input and output readers
BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter outputWriter = new PrintWriter(outputStream, true);
// Read and write data
BufferedReader userInputReader = new BufferedReader(new InputStreamReader(System.in));
String userInput, serverResponse;
while ((userInput = userInputReader.readLine()) != null) {
outputWriter.println(userInput);
if ((serverResponse = inputReader.readLine()) != null) {
System.out.println("Server: " + serverResponse);
}
if (userInput.equals("Bye")) {
break;
}
}
// Close everything
userInputReader.close();
inputReader.close();
outputWriter.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
Lab No:
Objective: Write a program to illustrate JDBC connectivity and perform CRUD operation on a
table student/employee (at least 5 attributes).
Code:
package webtech;
import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.DBConversion;
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","DIVYA","divya");
do
{ System.out.println("\n");
System.out.println("Menu:");
System.out.println("1.Create table");
System.out.println("2.Insert Record into the Table");
System.out.println("3.Update The Existing Record.");
System.out.println("4.Display all the Records from the Table");
System.out.println("5.Delete");
System.out.println("6.Exit");
System.out.println("Enter your choice: ");
String sql;
switch(ch)
{ case 1:
Statement stmt=con.createStatement();
String ct="create table employee(emp_id number(5),emp_name varchar2(30),emp_sal
number(8,2))";
boolean b=stmt.execute(ct);
if(b==false) System.out.println("table not created");
else {db_created=true;}
stmt.close();
break;
case 2:
if(db_created==true){
state=con.createStatement();
int i,n;
System.out.println("enter the no of values you want to insert");
n=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
{
System.out.println("Enter Employee Number: ");
no=Integer.parseInt(br.readLine());
System.out.println("Enter Employee Name: ");
name=br.readLine();
System.out.println("Enter Employee Salary: ");
sal=Integer.parseInt(br.readLine());
sql="insert into employee values(?,?,?)";
PreparedStatement p=con.prepareStatement(sql);
p.setInt(1,no);
p.setString(2,name);
p.setInt(3,sal);
p.executeUpdate();
System.out.println("Record Added");
p.close();
}}
else
System.out.println("create table first");
break;
case 3:
if(db_created==true){
state=con.createStatement();
System.out.println("Enter Employee Number for the record you wish to Update: ");
no = Integer.parseInt(br.readLine());
System.out.println("Enter new Name: ");
name = br.readLine();
System.out.println("Enter new Salary: ");
sal = Integer.parseInt(br.readLine());
sql="update employee set Name=?, Salary=? where Code=?";
PreparedStatement p=con.prepareStatement(sql);
p.setString(1,name);
p.setInt(2,sal);
p.setInt(3,no);
p.executeUpdate();
System.out.println("Record Updated");
p.close(); }
else
System.out.println("create table first");
break;
case 4:
if(db_created==true){
state=con.createStatement();
sql="select * from employee";
rs=state.executeQuery(sql);
while(rs.next())
{
System.out.println("\n");
System.out.print("\t" +rs.getInt(1));
System.out.print("\t" +rs.getString(2));
System.out.print("\t" +rs.getInt(3));
} }
else
System.out.println("create table first");
break;
case 5:
if(db_created==true){
state=con.createStatement();
sql="delete from employee where emp_id='1'";
rs=state.executeQuery(sql);
state.close();
}
else
System.out.println("create table first");
break;
case 6:
System.exit(1);
break;
default:
System.out.println("Invalid Choice");
break;
}
}while(ch!=6);
con.close();
}catch(Exception e)
{
System.out.println(e);
}
} }
Output:
Lab No:
Objective: Write a program to illustrate Batch Transaction with prepared statements.
Code:
BatchUpdateDemo_Connection.java
package webtech;
import java.sql.*;
public class BatchUpdateDemo_Connection {
public static Connection getOracleConnection()throws Exception{
String driver="oracle.jdbc.driver.OracleDriver";
String URL="jdbc:oracle:thin:@localhost:1521:xe";
String Username="DIVYA";
String password="divya";
Class.forName(driver);
return DriverManager.getConnection(URL,Username,password); } }
BatchUpdate.java
package webtech;
import java.sql.*;
public class BatchUpdate {
static Savepoint sp1;
public static void main(String args[]) throws Exception
{ Connection con=BatchUpdateDemo_Connection.getOracleConnection();
Statement stmt=null;
try{ con.setAutoCommit(false);
stmt=con.createStatement();
stmt.addBatch("delete from emplo");
stmt.addBatch("insert into emplo values(104,'ABC',5694)");
stmt.addBatch("insert into emplo values(105,'DEF',6694)");
int[] uc=stmt.executeBatch();
sp1 = con.setSavepoint("spv1");
stmt.addBatch("insert into emplo values(106,'GHI',7694)");
stmt.addBatch("insert into emplo2 values(107,'JKl',8694)");
int[] uc1=stmt.executeBatch();
System.out.println(". .."+uc.length);
for(int i=0;i<uc.length;i++)
{ System.out.println(uc[i]); }
con.commit(); }
catch(BatchUpdateException e)
{ System.out.println(e);
int[] abc=e.getUpdateCounts();
System.out.println(". .."+abc.length);
for(int i=0;i<abc.length;i++)
{ System.out.println(abc[i]); }
con.rollback(sp1);
con.commit(); } } }
Output:
Lab No:-
Objective: Install a database (Mysql or Oracle). Create a table which should contain at least
the following fields: name, password, email-id, phone number Write a java program/servlet/JSP
to connect to that database and extract data from the tables and display them. Insert the
details of the users who register with the web site, whenever a new user clicks the submit
button in the registration page.
Code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1><a href="RegisterForm.html">Register new user</a><br>
<a href="Display.jsp">Display existing users</a></h1>
</body>
</html>
RegisterForm.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head> <body>
<form action="Register.jsp" method="post">
<table>
<tr> <td>Name:</td>
<td><input type="text" name="name"></td> </tr>
<tr> <td>Password:</td>
<td><input type="text" name="password"></td> </tr>
<tr> <td>Email:</td>
<td><input type="email" name="email"></td> </tr>
<tr> <td>Phone Number:</td>
<td><input type="number" name="phone"></td> </tr>
<tr> <td></td>
<td><input type="submit" value="Register"></td> </tr>
</table>
</form>
</body> </html>
Register.jsp
Display.jsp
<tr>
<td><%= name %></td>
<td><%= password %></td>
<td><%= email %></td>
<td><%= phone %></td>
</tr>
<%}}%>
</tbody>
</table>
<% }
catch(Exception e) {
System.out.println(e);
}
finally {
out.println("<br><a href='index.html'>Go to home page</a>");
}
%>
</body></html>
Connect.java
package connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Output:
Lab No:
Objective: To Develop a student Mark sheet by using Servlet and HTML with database Oracle.
Code:
index.html
<html>
<body>
<form action="servlet/Search">
Enter your Rollno:<input type="text" name="roll"/><br/>
Search.java
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String rollno=request.getParameter("roll");
int roll=Integer.valueOf(rollno);
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
ResultSet rs=ps.executeQuery();
out.print("</tr>");
/* Printing result */
while(rs.next())
{
out.print("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+"
</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td></tr>");
}
out.print("</table>");
}catch (Exception e2) {e2.printStackTrace();}
finally{out.close();}
}
}
web.xml
<web-app>
<servlet>
<servlet-name>Search</servlet-name>
<servlet-class>Search</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Search</servlet-name>
<url-pattern>/servlet/Search</url-pattern>
</servlet-mapping>
</web-app>
Output:
Lab No:
Objective: Design and implement a simple servlet for an entry form of student details and
send it to store at database server like SQL, Oracle or MS Access.
Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./register" method="post">
<table>
<tr> <td>Roll Number:</td>
<td><input type="number" name="roll"></td>
</tr>
<tr> <td>student name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr> <td>Course:</td>
<td><input type="text" name="course"></td>
</tr>
<tr> <td>branch:</td>
<td><input type="text" name="branch"></td>
</tr>
<tr> <td></td>
<td><input type="submit" value="add"></td>
</tr>
</table>
</form>
</body>
</html>
Connect.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
AddInDatabase.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
web.xml
Output:
Lab No:
Objective: Write a JSP which insert the details of the 3 or 4 users who register with the web
site by using registration form.
Authenticate the user when he submits the login form using the user name and password from
the database
Code:
Connect.java
package connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Login Page</h1>
<form action="Login.jsp" method="get"><br>
<table>
<tr> <td>Username</td>
<td><input type="text" name="uname"></td> </tr>
<tr> <td>Password</td>
<td><input type="text" name="upass"></td> </tr>
<tr> <td></td>
<td><input type="submit" value="Login"></td> </tr>
<tr> <td></td>
<td><center><a href="RegisterForm.html">
New user? Register here.</a></center></td> </tr>
</table>
</form>
</body>
</html>
RegisterForm.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Registration Page</h1>
<form action="Register.jsp" method="post"><br>
<table>
<tr> <td>Name</td>
<td><input type="text" name="name"></td> </tr>
<tr> <td>Username</td>
<td><input type="text" name="uname"></td> </tr>
<tr> <td>Password</td>
<td><input type="text" name="upass"></td> </tr>
<tr> <td>Email</td>
<td><input type="text" name="email"></td> </tr>
<tr> <td>Phone Number</td>
<td><input type="number" name="phone"></td></tr>
<tr> <td></td>
<td><input type="submit" value="Register here"></td> </tr>
</table>
</form>
<a href="index.html">Go back to home page</a>
</body>
</html>
Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%@ page import="connection.Connect" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try{boolean exist=false;
Statement stmt = Connect.conn().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select * from users");
if(!rs.next()){
%>
<h1>No users registered! Register first.</h1>
<%
}
else{
rs.previous();
String uname = request.getParameter("uname");
String password = request.getParameter("upass");
while (rs.next()) {
String name = rs.getString(2);
String pass = rs.getString(3);
if(uname.equals(name)&&password.equals(pass)) {
exist=true;
break; }}
if(exist){ %>
<h1>You have successfully logged in <%= rs.getString(1) %></h1>
<br>
<strong>Your username is <%= rs.getString(2)%><br>
Your email is <%= rs.getString(4)%><br>
Your phone number is <%= rs.getInt(5)%></strong>
<% }
else{
%>
<h1>Wrong username or password!</h1>
<%
}}}
catch(Exception e) {
System.out.println(e);
}
finally {
out.println("<br><a href='index.html'>Go to home page</a>");}%>
</body>
</html>
Register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.Statement" %>
<%@ page import="connection.Connect" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
String name = request.getParameter("name");
String uname = request.getParameter("uname");
String password = request.getParameter("upass");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
try {
Statement stmt = Connect.conn().createStatement();
String query = "insert into users
values('"+name+"','"+uname+"','"+password+"','"+email+"',"+phone+")";
stmt.executeQuery(query);
Connect.conn().close();
stmt.close();
out.println("Registration Successfull");
}catch(java.sql.SQLIntegrityConstraintViolationException e) {
out.println("Error resgistering this user. A student with same username exists!");
}
catch(Exception e) {
out.println("Registration Unsuccessfull!");
System.out.println(e);
}
finally {
out.println("<br><a href='index.html'>Go to home page</a>");
} %>
</body>
</html>
Code: