AJP Lab Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

1.

To write a Java Program to design an interface for Stack ADT and implement Stack ADT
using both Array and Linked List.

2. Write a Java program that loads names and phone numbers from a text file where the data
is organized as one line per record and each field in a record are separated by a tab (\t). It
takes a name or phone number as input and prints the corresponding other value from the
hash table (hint: use hash tables).

3. Write a Java program that connects to a database using JDBC and does add, delete,
modify and retrieve operations

4. Write a java program that prints the meta-data of a given table

5. Develop and demonstrate a HTML5 document that illustrates the use of ordered list,
unordered list, table, borders, padding, color, and the <div> & <span> tag.

6. Write HTML5 code to provide intra and inter document linking

7. Create a web page with all types of Cascading style sheets.

8. Create a web page with the following using CSS:

a. Text shadows, rounded corners and box shadows.

b. Linear and Radial gradients.

c. Animation

d. Transitions and Transformations

9. Create tables in the database which contain the details of items (books in our case Like
Book name, Price, Quantity, Amount) of each category. Modify your catalogue page in
such a way that you should connect to the database and extract data from the tables and
display them in web page using JDBC.

10. Using java servlets and JDBC store and retrieve the following information from a
database:

a. Name

b. Password
c. Email id

d. Phone number

SQL Query to create Table

` CREATE TABLE STUDENT


(
NAME VARCHAR2(40),
PASSWORD VARCHAR2(40),
EMAILID VARCHAR2(40),
PHONE VARCHAR(10)
) ;

REGISTER.HTML

<html>

<body>

<form action="servlet/Register" method="post">

<table>

<tr>

<td>Name:</td><td><input type="text" name="userName"/></td>

</tr>

<tr>

<td>Password:</td><td><input type="password" name="userPass"/></td>

</tr>

<tr>

<td>Email Id:</td><td><input type="text" name="userEmail"/></td>


</tr>

<tr>

<td>Phone Number:</td><td><input type="text" name="userPhone”></td>

</tr>

<tr>

<td></td><td><input type="submit" value="register"/></td>

</tr>

</table>

</form>

</body>

</html>

REGISTER.JAVA

import java.io.*;

import java.sql.*;

import javax.servlet.ServletException;

import javax.servlet.http.*;

public class Register extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n=request.getParameter("userName");

String p=request.getParameter("userPass");

String e=request.getParameter("userEmail");

long c=request.getParameter("phno");

try{

Class.forName("com.mysql.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vvit”,”root”,”root”);

PreparedStatement ps=con.prepareStatement("insert into student values(?,?,?,?)");

ps.setString(1,n);

ps.setString(2,p);

ps.setString(3,e);

ps.setString(4,c);

int i=ps.executeUpdate();

if(i>0)

out.print("You are successfully registered...");

}catch (Exception e2) {System.out.println(e2);}

out.close();
}

Web.xml:

<web-app>
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/servlet/Register</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Display</servlet-name>
<servlet-class>Display</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Display</servlet-name>
<url-pattern>/servlet/Display</url-pattern>
</servlet-mapping>

</web-app>

Display.html

<form action="servlet/Display">

Enter Name:<input type="text" name="uname"/><br/>

<input type="submit" value="Display"/>

</form>
Display.java

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

public class Display extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String name=request.getParameter("uname");

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/vvit”,”root”,”root”);

PreparedStatement ps=con.prepareStatement("select * from result where name=?");


ps.setString(1,name);

out.print("<table width=50% border=1>");


out.print("<caption>Result:</caption>");

ResultSet rs=ps.executeQuery();

/* Printing column names */


ResultSetMetaData rsmd=rs.getMetaData();
int total=rsmd.getColumnCount();
out.print("<tr>");
for(int i=1;i<=total;i++)
{
out.print("<th>"+rsmd.getColumnName(i)+"</th>");
}

out.print("</tr>");

/* Printing result */

while(rs.next())
{
out.print("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"
</td><td>"+rs.getString(3)+"</td><td>"+rs.getInt(4)+"</td></tr>");

out.print("</table>");

}catch (Exception e2) {e2.printStackTrace();}

finally{out.close();}

OUTPUT:
11. Demonstrate Cookie and Session Management in Servlets.

Index.html

<form action="servlet1" method="post">


Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>

FirstServlet.java

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

public class FirstServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response){


try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n=request.getParameter("userName");
out.print("Welcome "+n);

Cookie ck=new Cookie("uname",n);//creating cookie object


response.addCookie(ck);//adding cookie in the response

//creating submit button


out.print("<form action='servlet2'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");

out.close();

}catch(Exception e){System.out.println(e);}
}
}

SecondServlet.java

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

public class SecondServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response){


try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());

out.close();

}catch(Exception e){System.out.println(e);}
}

web.xml
<web-app>

<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>

</web-app>

OUTPUT:
12. Write a JSP program to conduct online examination and to display student mark list
available in a database.
ExamServer.jsp:

<%@page contentType="text/html" language="java" import="java.sql.*"%>


<html>
<head>
<title>Online Exam Server</title>
<style type="text/css">
body{background-color:black;font-family:courier;color:blue}
</style>
</head>
<body>
<h2 style="text-align:center">ONLINE EXAMINATION</h2>
<p>
<a href="ExamClient.html">Back To Main Page</a>
</p>
<hr/>
<%
String str1=request.getParameter("ans1");
String str2=request.getParameter("ans2");
String str3=request.getParameter("ans3");
int mark=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:examDS");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM examTab");
int i=1;
while(rs.next())
{
if(i==1)
{
String dbans1=rs.getString(1);
if(str1.equals(dbans1))
{
mark=mark+5;
}
}
if(i==2)
{
String dbans2=rs.getString(1);
if(str2.equals(dbans2))
{
mark=mark+5;
}
}
if(i==3)
{
String dbans3=rs.getString(1);
if(str3.equals(dbans3))
{
mark=mark+5;
}
}
i++;
}
if(mark>=10)
{
out.println("<h4>Your Mark Is : "+mark+"</h4>");
out.println("<h3>Congratulations....! You Are Eligible For The Next Round...</h3>");
}
else
{
out.println("<h4>Your Mark is : "+mark+"</h4>");
out.println("<h3>Sorry....!! You Are Not Eligible For The Next Round...</h3>");
}
%>
</form>
</body>
</html>

ExamClient.HTML:

<html>
<head>
<title>Online Exam Client</title>
<style type="text/css">
body{background-color:black;font-family:courier;color:blue}
</style>
</head>
<body>
<h2 style="text-align:center">ONLINE EXAMINATION</h2>
<h3>Answer the following questions (5 marks for each correct answer)</h3>
<hr/>
<form name="examForm" method="post" action="ExamServer.jsp">
1. All computers must have <br/>
<input type="radio" name="ans1" value="Operating System">Operating System
<input type="radio" name="ans1" value="Application Software">Application Software
<input type="radio" name="ans1" value="CD Drive">CD Drive
<input type="radio" name="ans1" value="Microsoft word">Microsoft word
<br/><br/>
2. The term PC means <br/>
<input type="radio" name="ans2" value="Private Computer">Private Computer
<input type="radio" name="ans2" value="Professional Computer">Professional Computer
<input type="radio" name="ans2" value="Personal Computer">Personal Computer
<input type="radio" name="ans2" value="Personal Calculator">Personal Calculator
<br/><br/>
3.C was developed by?<br/>
<input type="radio" name="ans3" value="Dennis Ritchie">Dennis Ritchie
<input type="radio" name="ans3" value="Stroustrup">Stroustrup
<input type="radio" name="ans3" value="David Ritchie">David Ritchie
<input type="radio" name="ans3" value="Charles Babbage">Charles Babbage
<br/><br/>
<input type="submit" value="Check Your Result"/>
</form>
</body>
</html>

13. Write a program to demonstrate cookie & Sessions using JSP.

Writecookies

One.jsp
<%
// Creating cookies for name and age.
Cookie name = new Cookie("User_name", request.getParameter("User_name"));
Cookie age = new Cookie("Age", request.getParameter("Age"));
Cookie gender = new Cookie("Gender", request.getParameter("Gender"));
// Setting expiry date
name.setMaxAge(60*60*24);
age.setMaxAge(60*60*24);
gender.setMaxAge(60*60*24);
// Add both the cookies in the response header.
response.addCookie( name );
response.addCookie( age );
response.addCookie( gender );
%>
<html>
<head>
<title>Set Cookies</title>
</head>
<body>
<h2>--DataFlair--<h2>
<h2>Here we are setting Cookies</h2>
<ul>
<li><p><b>User Name:</b>
<%= request.getParameter("User_name")%>
</p></li>
<li><p><b>Age:</b>
<%= request.getParameter("Age")%>
</p></li>
<li><p><b>Gender:</b>
<%=request.getParameter("Gender")%>
</p></li>
</ul>
</body>
</html>

Hello.jsp

<html>
<body>
<form action = "write.jsp" method = "GET">
User Name: <br/><input type = "text" name = "User_name">
<br />
Age: <br/><input type = "text" name = "Age" />
<br/>
Gender:</br><input type = "text" name = "Gender">
<br/>
<input type = "submit" value = "Go" />
</form>
</body>
</html>
Read Cookies:

Two.jsp
<html>
<head>
<title>Read Cookies</title>
</head>
<body>
<h1>Reading Cookies that we set</h1>
<%
Cookie my_cookie = null;
Cookie[] my_cookies = null;
// Get an array of Cookies associated with the this domain
my_cookies = request.getCookies();
if( my_cookies != null ) {
out.println("<h2> Found Cookies Name and Value</h2>");
for (int i = 0; i < my_cookies.length; i++) {
my_cookie = my_cookies[i];
out.print("Name : " + my_cookie.getName( ) + ", ");
out.print("Value: " + my_cookie.getValue( )+" <br/>");
}
} else {
out.println("<h2>No cookies founds</h2>");
}
%>
</body>
</html>
OUTPUT:

14. Write a java program to implement client-server application

15. Write a program to prompt the user for a hostname then looks up the IP address
for the hostname and read the webpage name then display the contents of the
webpage.

You might also like