Final File
Final File
Semester: ______________________________
Faculty In-Charge:
INDEX
10. 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’s name and password from the database.
<!DOCTYPE html>
<html>
<head>
<title>My Personal Page</title>
</head>
<body>
<h1>Welcome to My Personal Page</h1>
<nav>
<ul>
<li><a href="cv.pdf" target="_blank">My CV</a></li>
<li><a href="http://www.myinstitute.edu" target="_blank">Institute
Website</a></li>
<li><a href="http://www.myinstitute.edu/mydepartment"
target="_blank">Department Website</a></li>
<li><a href="http://www.tutorialwebsite.com/mysubject"
target="_blank">My Subject Tutorial</a></li>
</ul>
</nav>
</body>
</html>
Output
PRACTICAL 2
Write an HTML program to design an entry form of student details and send it to store at
database server like SQL, Oracle or MS Access.
Code
<!DOCTYPE html>
<html>
<head>
<title>Student Entry Form</title>
</head>
<body bgcolor="white" bolder="2">
<form action="register.jsp" name="StudentRegistration" method=”post”>
<table cellpadding="2" width="50%" border="2" align="center"
cellspacing="2">
<tr>
<td colspan=2>
<center>
<font size=4><b>Student Registration Form</b></font>
</center>
</td>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname"
size="30"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername"
size="30"></td>
</tr>
<tr>
<td>Permanant Address</td>
<td><input type="text" name="permanantaddress"
id="permanantaddress" size="30"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="Gender" value="male"
size="10">Male
<input type="radio" name="Gender" value="Female"
size="10">Female
</td>
</tr><tr>
<td>City</td>
<td><select name="City">
<option value="-1" selected>select..</option>
<option value="New Delhi">NEW DELHI</option>
<option value="Mumbai">MUMBAI</option>
<option value="Goa">GOA</option>
<option value="Patna">PATNA</option>
</select></td>
</tr>
<tr>
<td>Course</td>
<td><select name="Course">
<option value="-1" selected>select..</option>
<option value="B.Tech">B.TECH</option>
<option value="MCA">MCA</option>
<option value="MBA">MBA</option>
<option value="BCA">BCA</option>
</select></td>
</tr>
<tr>
<td>PinCode</td>
<td><input type="text" name="pincode" id="pincode"
size="30"></td>
</tr>
<tr>
<td>EmailId</td>
<td><input type="text" name="emailid" id="emailid"
size="30"></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="dob" id="dob" size="30"></td>
</tr>
<tr>
<td>MobileNo</td>
<td><input type="text" name="mobileno" id="mobileno"
size="30"></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form"
/></td>
</tr>
</table>
</form>
</body>
</html>
Output
PRACTICAL 3
Write programs using Java script for Web Page to display browsers information.
Code
<!DOCTYPE html>
<html>
<head>
<title>Browser Information</title></head>
<body>
<h1>Browser Information</h1>
<p id="info"></p>
<script>
var info = "Browser CodeName: " + navigator.appCodeName + "<br>" +
"Browser Name: " + navigator.appName + "<br>" +
"Browser Version: " + navigator.appVersion + "<br>" +
"Cookies Enabled: " + navigator.cookieEnabled + "<br>" +
"Browser Language: " + navigator.language + "<br>" +
"Browser Online: " + navigator.onLine + "<br>" +
"Platform: " + navigator.platform + "<br>" +
"User-agent header: " + navigator.userAgent;
document.getElementById("info").innerHTML = info;
</script>
</body>
</html>
Output
PRACTICAL 4
Write a Java applet to display the Application Program screen i.e. calculator and other.
Code
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
public class Cal extends Applet
implements ActionListener {
String msg = " ";
int v1, v2, result;
TextField t1;
Button b[] = new Button[10];
Button add, sub, mul, div, clear, mod, EQ;
char OP;
public void init() {
Color k = new Color(120, 89, 90);
setBackground(k);
t1 = new TextField(10);
GridLayout gl = new GridLayout(4, 5);
setLayout(gl);
for (int i = 0; i < 10; i++) {
b[i] = new Button("" + i);
}
add = new Button("add");
sub = new Button("sub");
mul = new Button("mul");
div = new Button("div");
mod = new Button("mod");
clear = new Button("clear");
EQ = new Button("EQ");
t1.addActionListener(this);
add(t1);
for (int i = 0; i < 10; i++) {
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for (int i = 0; i < 10; i++) {
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
char ch = str.charAt(0);
if (Character.isDigit(ch))
t1.setText(t1.getText() + str);
else
if (str.equals("add")) {
v1 = Integer.parseInt(t1.getText());
OP = '+';
t1.setText("");
} else if (str.equals("sub")) {
v1 = Integer.parseInt(t1.getText());
OP = '-';
t1.setText("");
} else if (str.equals("mul")) {
v1 = Integer.parseInt(t1.getText());
OP = '*';
t1.setText("");
} else if (str.equals("div")) {
v1 = Integer.parseInt(t1.getText());
OP = '/';
t1.setText("");
} else if (str.equals("mod")) {
v1 = Integer.parseInt(t1.getText());
OP = '%';
t1.setText("");
}
if (str.equals("EQ")) {
v2 = Integer.parseInt(t1.getText());
if (OP == '+')
result = v1 + v2;
else if (OP == '-')
result = v1 - v2;
else if (OP == '*')
result = v1 * v2;
else if (OP == '/')
result = v1 / v2;
else if (OP == '%')
result = v1 % v2;
t1.setText("" + result);
}
if (str.equals("clear")) {
t1.setText("");
}
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getV1() {
return v1;
}
public void setV1(int v1) {
this.v1 = v1;
}
public int getV2() {
return v2;
}
public void setV2(int v2) {
this.v2 = v2;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public TextField getT1() {
return t1;
}
public void setT1(TextField t1) {
this.t1 = t1;
}
public Button[] getB() {
return b;
}
public void setB(Button[] b) {
this.b = b;
}
public Button getAdd() {
return add;
}
public void setAdd(Button add) {
this.add = add;
}
public Button getSub() {
return sub;
}
public void setSub(Button sub) {
this.sub = sub;
}
public Button getMul() {
return mul;}
public void setMul(Button mul) {
this.mul = mul;}
public Button getDiv() {
return div;
}
public void setDiv(Button div) {
this.div = div;
}
public Button getClear() {
return clear;}
public void setClear(Button clear) {
this.clear = clear;
}
public Button getMod() {
return mod;}
public void setMod(Button mod) {
this.mod = mod;}
public Button getEQ() {
return EQ;}
public void setEQ(Button eQ) {
EQ = eQ;}
public char getOP() {
return OP;}
public void setOP(char oP) {
OP = oP;
}
}
Output
PRACTICAL 5
Write a program in XML for creation of DTD, which specifies set of rules. Create a style
sheet in CSS/ XSL & display the document in internet explorer.
Code
<!--1. XML-->
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>FOREVER</title>
<artist>
<b>JUSTIN BIEBER</b>
</artist><br>
<country>CANADA</country><br>
<company>COLUMBIA</company><br>
<price>10.69</price><br>
<year>2020</year><br>
</cd>
<cd>
<br>
<title>LIFE IS WORTH IVING</title><br>
<artist>
<b>JUSTIN BIEBER</b>
</artist><br>
<country>CAN</country><br>
<company>CBS Records</company><br>
<price>9.90</price><br>
<year>2016</year><br>
</cd>
</catalog>
<!--XSL-->
<html>
<body>
<h2>
<i>My Song Collection</i></h2>
<table border="1">
<tr bgcolor="#9f69f">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th></tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title" /></td>
<td>
<xsl:value-of select="artist" /></td></tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<!--2.XML-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note><br>
<heading>Remember,<br>
<b>LIFE</b> is too short, so</heading>
<body>
<b>LIVE IN THE MOMENT</b>
</body>
</note>
<!--DTD-->
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
Output
PRACTICAL 6
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.
Code
} // nothing we can do
try
{
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
System.out.println("Goodbye!");
}// end main
}// end FirstExample
Servlet And
JDBC
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JDBCServlet extends HttpServlet {
public void doGet(HttpServletRequest inRequest,
HttpServletResponse outResponse) throws ServletException,
IOException {
PrintWriter out = null;
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager
.getConnection("jdbc:mysql://localhost/products");
statement = connection.createStatement();
outResponse.setContentType("test/html");
out = outResponse.getWriter();
rs = statement.executeQuery("SELECT ID, title, price FROM
product");
out.println("<HTML><HEAD><TITLE>Products</TITLE></HEAD>");
out.println("<BODY>");
out.println("<UL>");
while (rs.next()) {
out.println("<LI>" + rs.getString("ID") + " "
+ rs.getString("title") + " " + rs.getString("price"));
}
out.println("</UL>");
out.println("</BODY></HTML>");
} catch (ClassNotFoundException e) {
out.println("Driver Error");
} catch (SQLException e) {
out.println("SQLException: " + e.getMessage());
}
}
public void doPost(HttpServletRequest inRequest,
HttpServletResponse outResponse) throws ServletException,
IOException {
doGet(inRequest, outResponse);
}}
PRACTICAL 7
Install TOMCAT web server and APACHE. Access the above developed static web pages for
books web site, using these servers by putting the web pages developed.
Code
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(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
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(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
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")) ||
c[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].getValue()) && pas.equals(c[i +
1].getValue()))) {
//RequestDispatcher rd=req.getRequestDispatcher("/cart.html");
rd.forward(req, res);
} else {
out.println("YOU ARE NOT AUTHORISED USER ");
//res.sendRedirect("/cookdemo/cologin.html");
}
}
}
}
}
Web.xml:
<web-app>
<servlet>
<servlet name>- him</servlet-name>
<servlet-class>Clogin</servlet-class>
</servlet>
<servlet>
<servlet-name>
htm1</servlet-name>
<servlet-class>Addcook</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>him</servlet-name>
<url-pattern>/clogin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>him1</servlet-name>
<url-pattern>/clogin1</url-pattern>
</servlet-mapping>
</web-app>
Output
PRACTICAL 9
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
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>
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
PRACTICAL 10
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’s name and password from the database.
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> </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>
</tr>
<tr>
<td> </td>
<td> </td>
<td colspan="2" align="center"><input type="submit"
value="LogIN" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Auth.jsp:
<head>
<title>
This is simple data base example in JSP</title>
</title>
</head>
<body bgcolor="yellow">
<%!String uname,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(); ResultSet
rs=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>
Output
PRACTICAL 11
Design and implement a simple shopping cart example with session tracking API.
Code
app.use(cors());
Output