WT Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

PROGRAM – 6

OBJECTIVE: Program to illustrate JDBC connectivity. Program for maintaining database by sending
queries. Design and implement a simple servlet book query with the help of JDBC & SQL. Create MS
Access Database, Create on ODBC link, Compile & execute JAVA JDVC Socket.

PROGRAM CODE:
SOURCE CODE:
import java.sql.*;

public class OracleJdbcConnection

public static void main(String []s)

try

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

String url="jdbc:oracle:thin:@localhost:1521:XE";

Connection con=DriverManager.getConnection(url,"dbuser","user@12345");

Statement stml=con.createStatement();

int num=stml.executeUpdate("insert into student values(1,'P. K.


Daruwala',45,'Delhi')");

System.out.println(+num+" Row Inserted");

con.close();

catch(Exception e)

System.out.println(e);

}
OUTPUT:
PROGRAM – 7

OBJECTIVE: (i) Install TOMCAT web server and APACHE. (ii) Write an HTML program to design an entry
form of student details and send it to store at database server likeSQL, Oracle or MS Access.

PROGRAM CODE:
SOURCE CODE:
Reg.html
<html>
<body><br /><br />
<form name="myform" method="post" action="reg">
<table align="center" >
<tr>
<td>NAME</td>
<td> :<input type="text" name="name" /></td>
</tr>
<tr>
<td>ADDRESS</td>
<td> :<input type="text" name="addr" /></td>
</tr>
<tr>
<td>CONTACT NUMBER</td>
<td> :<input type="text" name="phno" /></td>
</tr>
<tr>
<td>LOGINID</td>
<td> :<input type="text" name="id" /></td>
</tr>
<tr>
<td>PASSWORD</td>
<td> :<input type="password" name="pwd" /></td>
</tr>
</table>
<br /><br />
<div align="center"><input type="submit" value="ok" onclick="validate()" />
<input type="reset" value="clear" />
</div>
</form>
</body>
</html>

login.java
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class login extends HttpServlet {


public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.println("<html><body>");
String id = req.getParameter("id");
String pwd = req.getParameter("pwd");
String s1 = "", s2 = "";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:XE",
"system", "tiger");
Statement stmt = con.createStatement();
String sqlstmt = "select * from login";
ResultSet rs = stmt.executeQuery(sqlstmt);
int flag = 0;
while (rs.next()) {
s1 = rs.getString(4);
s2 = rs.getString(5);
}
if (id.equals(s1) && pwd.equals(s2))
flag = 1;
if (flag == 0) {
pw.println("<br><br>SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\"login.html\">press LOGIN to RETRY</a>");
} else {
pw.println("<br><br>WELCOME TO " + id.toUpperCase() + "<br><br>");
pw.println("<h3><ul>");
pw.println("<li><a href=\"profile.html\"><font color=\"black\">USER
PROFILE</font></a></li><br><br>");
pw.println("<li><a href=\"catalog.html\"><font color=\"black\">BOOKS
CATALOG</font></a></li><br><br>");
pw.println("<li><a href=\"order.html\"><font color=\"black\">ORDER
CONFIRMATION</font></a></li></ul><br><br>");
}
pw.println("</body></html>");
} catch (Exception e) {
resp.sendError(500, e.toString());
}
}
}

OUTPUT: To understand the logic of database connectivity with web page and how the query works.
PROGRAM – 8

OBJECTIVE: Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2,
pwd3 and pwd4 respectively. Write a servlet for doing the following. (i) Create a Cookie and add these
four user id’s and passwords to this Cookie. (ii) Read the user id and passwords entered in the Login
form and authenticate with the values available in the cookies.

PROGRAM CODE:
SOURCE CODE:
cologin.html:
<html>
<head>
<title> login Page </title>
<p style= "background:yellow; top: 100px; left:250px; position:absolute; ">
</head>
<body>
<form ACTION="clogin">
<label> Login </label>
<input type="text" name="usr" size="20"><br><br>
<label> Password </label>
<input type="password" name="pwd" size="20"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
cologin1.html
<html>
<head>
<title> login Page </title>
<p style= "background:yellow; top:100px; left:250px; position:absolute; ">
</head>
<body>
<form ACTION="clogin1">
<label> Login </label>
<input type="text" name="usr" size="20"><br><br>
<label> Password </label>
<input type="password" name="pwd" size="20"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>

Addcook.java:
import javax.servlet .* ;
import javax.servlet.http .*;
import java.io .*;
public class Addcook extends HttpServlet
{
String user,pas;
Public void service(HttpServletRequestreq,HttpServletResponse res) throws
ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out=res.get Writer();
Cookie c1=new Cookie("usr1","suni");
Cookie p1=new Cookie("pwd1","ani");
Cookie c2=new Cookie("usr2","abc");
Cookie p2=new Cookie("pwd2","123");
Cookie c3=new Cookie("usr3","def");
Cookie p3=new Cookie("pwd3","456");
Cookie c4=new Cookie("usr4","mno");
Cookie p4=new Cookie("pwd4","789");
res.addCookie(c1);
res.addCookie(p1);
res.addCookie(c2);
res.addCookie(p2);
res.addCookie(c3);
res.addCookie(p3);
res.addCookie(c4);
res.addCookie(p4);
out.println("COOKIE ADDED");
}
}

Clogin.java:
import javax.servlet .* ;
import javax.servlet.http .*;
import java.io .*;
public class Clogin extends HttpServlet
{
String user,pas;
Public void service(HttpServletRequestreq,HttpServletResponse res) throws
{
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.get Writer();
user=req.getParameter("usr");
pas=req.getParameter("pwd");
Cookie[] c=req.getCookies();
for(int i=0;i<c.length;i++)
{
if((c[i].getName().equals("usr1")&&c[i+1].getName().equals("pwd1"))||
:[i].getName().equals("usr2")
&&c[i+1].getName().equals("pwd2"))||(c[i].getName().equals("usr3")&&
c[i+1].getName().equals("pwd3"))||(c[i].getName().equals("usr4")&&
c[i+1].getName().equals("pwd4") ))
{
if((user.equals(c[i].get Value()) &&pas.equals(c[i+1].getValue())) )
{
//RequestDispatcherrd=req.getRequestDispatcher("/cart.html");
rd.forward(req,res);
}
else
{
out.println("YOU ARE NOT AUTHORISED USER ");
//res.sendRedirect("/cookdemo/cologin.html");
}
}
}
}
}
}
OUTPUT:

SOURCE CODE:
(ii) Read the user id and passwords entered in the Login form and authenticate with the values available in the
cookies.

home.html:
<html>
<head>
<title>Authentication</title>
</head>
<body>
<form action="ex1">
<label>Username </label>
<input type="text"size="20" name="user"><br><br>
password<input type="text" size="20" name="pwd"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>

Examplel.java
import javax.servlet .*;
import java.io .*;
public class Examplel extends GenericServlet
{
private String user1,pwd1,user2,pwd2,user3,pwd3,user4,pwd4,user5,pwd5;
public void init(ServletConfigsc)
{
user1=sc.getInitParameter("username1");
pwd1=sc.getInitParameter("password1");
user2=sc.getInitParameter("username2");
pwd2=sc.getInitParameter("password2");

user3=sc.getInitParameter("username3");
pwd3=sc.getInitParameter("password3");

user4=sc.getInitParameter("username4");
pwd4=sc.getInitParameter("password4");
}
Public void service(ServletRequestreq,ServletResponse res)throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
user5=req.getParameter("user");
pwd5=req.getParameter("pwd");
if((user5.equals(user1)&&pwd5.equals(pwd1))||(user5.equals(user2)&&pwd5.equals(pwd
2))||(user5.equals(user3)&&pwd5.equals(pwd3))||(user5.equals(user4)&&pwd5.equals(pwd4)
)0
out.println("<p> welcome to"+user5.toUpperCase());
else
out.println("You are not authorized user");
}
}

web.xml:
<web-app>
<servlet>
<servlet-name>Example</servlet-name>
<servlet-class>Example1</servlet-class>
<init-param>
<param-name>username1</param-name>
<param-value>pvpsit</param-value>
</init-param>
<init-param>
<param-name>password1</param-name>
<param-value>cse</param-value>
</init-param>
<init-param>
<param-name>username2</param-name>
<param-value>1234</param-value>
</init-param>
<init-param>
<param-name>password2</param-name>
<param-value>4567</param-value>
</init-param>
<init-param>
<param-name>username3</param-name>
<param-value>cse</param-value>
</init-param>
<init-param>
<param-name>password3</param-name>
<param-value>pvpsit</param-value>
</init-param>
<init-param>
<param-name>username4</param-name>
<param-value>wt</param-value>
</init-param>
<init-param>
<param-name>password4</param-name>
<param-value>lab</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/ex1</url-pattern>
</servlet-mapping>
</web-app>
PROGRAM – 9

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.

PROGRAM CODE:
SOURCE CODE: Registration.html:
<html>

<head>

<title>Registration page</title>

</head>

<body bgcolor="#00FFFf>

<form METHOD="POST" ACTION="register">

<CENTER>

<table> <center>

<tr> <td> Username </td>

<td><input type="text" name="usr"> </td> </tr>

<tr><td> Password </td>

<td><input type="password" name="pwd"> </td> </tr>

<tr><td>Age</td>

<td><input type="text" name="age"> </td> </tr>

<tr> <td>Address</td>

<td> <input type="text" name="add"> </td> </tr>

<tr> <td>email</td>

<td> <input type="text" name="mail"> </td> </tr>

<tr> <td>Phone</td>

<td> <input type="text" name="phone"> </td> </tr>

<tr> <td colspan=2 align=center> <input type="submit" value="submit"> </td></tr>

</center></table>

</form>

</body></html>

Login.html
<html>

<head>

<title>Registration page</title>

</head>

<body bgcolor=pink> <center> <table>

<form METHOD="POST" ACTION="authent">

<tr> <td> Username </td>

<td><input type="text" name="usr"></td> </tr>

<tr> <td> Password </td>

<td> <input type="password" name="pwd"></td></tr>

<tr> <td align=center colspan="2"><input type="submit" value="submit"></td> </tr>

</table> </center>

</form>

</body>

</html>

Ini.java:
import javax.servlet .*;
import java.sql .*;
import java.io .*;
public class Ini extends GenericServlet
{
private String user1,pwd1,email1;
public void service(ServletRequest req,ServletResponse res)throws
ServletException, IOException
{
user1=req.getParameter("user");
pwd1=req.getParameter("pwd");
email1=req.getParameter("email");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
Try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@195.100.101.158:1521:cclab","scott","tiger");
PreparedStatement st=con.prepareStatement("insert into personal values(?,?,?,?,?,?)");
st.setString(1,user1);
st.setString(2,pwd1);
st.setString(3,"25");
st.setString(4,"hyd");
st.setString(5,email1);
st.setString(6,"21234");
st.executeUpdate();
con.close();
}
catch(SQLException s)
{ out.println("not found "+s);
}
catch(ClassNotFoundException c)
{
out.println("not found "+c);
}}}

web.xml:
<web-app>
<servlet>
<servlet-name>init1</servlet-name>
<servlet-class>Ini</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>init1</servlet-name>
<url-pattern>/regis</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
PROGRAM – 10

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.

PROGRAM CODE:
SOURCE CODE:
Login.html:

<html>

<body>

<center><h1>XYZ Company Ltd .< /h1></center>

<table border="1" width="100%" height="100%">

<tr>

<td valign="top" align="center"><br/> <form action="auth.jsp"><table>

<tr>

<td colspan="2" align="center"><b>Login Page</b></td>

</tr>

<tr>

<td colspan="2" align="center"><b>&nbsp ;< /td>

</tr>

<tr>

<td>User Name</td>

<td><input type="text" name="user"/></td>

</tr>

<tr>

<td>Password</td>

<td><input type="password" name="pwd"/></td>

</tr>

<tr>

<td>&nbsp ;< /td>

<td>&nbsp ;< /td>

</tr>

<tr>
<td colspan="2" align="center"><input type="submit" value="LogIN"/></td>

</tr>

</table>

</form>

</td>

</tr>

</table>

</body>

</html>

Authentication.jsp:

<%@page import="java.sql .*; "%>

<html><head><title>This is simple data base example in JSP</title></head>

<body bgcolor="yellow">

<%!Stringuname,pwd;%>

<%uname=request.getParameter("user");

pwd=request.getParameter("pwd");

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con =DriverManager.getConnection

("jdbc:oracle:thin:@195.100.101.158:1521:CCLAB","scott","tiger");

Statement st=con.createStatement();

ResultSetrs=st.executeQuery("select name ,password from personal where name='"'+uname+"' and


password=''+pwd+"');

if(rs.next()){

out.println("Authorized person");}

else{

out.println("UnAuthorized person");}

con.close();}

catch(Exception e){out.println(""+e);}

%>

</body>

</html>

You might also like