Professional Documents
Culture Documents
Servlet Program
import java.io.IOException;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Myservletprog extends HttpServlet
{
protected void doGet(HttpServletRequest rq,
HttpServletResponse response)
throws ServletException, IOException
{
int n1=Integer.parseInt(rq.getParameter("num1"));
int n2=Integer.parseInt(rq.getParameter("num2"));
int sum=n1+n2;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Calculator File</title>");
out.println("</head>");
out.println("<body>");
out.println(<h1>+ sum + </h1>);
out.println("</body>");
out.println("</html>");
}
}
(3)Program Three
tier can then access the database to manipulate the data. The three tiers
can reside on separate computers that are connected to a network.
(4)Program Four
//Write a servlet program that display all the records from
the Database table after click on show button from the html
page
HTML Program
<html>
<head>
<title>Program For Displaying Record</title>
</head>
<body>
<FORM method="Get"
ACTION="http://localhost:8080/servletproject/ShowRecord">
<BR>
<font size=5 color="red">
Display Record
<BR>
<INPUT TYPE=submit name=submit Value="ShowData">
</font>
</FORM>
</body>
</html>
Servlet Program
package myserpackage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.io.*;
public class ShowRecord extends HttpServlet
{
public void doGet(HttpServletRequest rq,
HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter p=res.getWriter();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:emp","","");
Statement st=con.createStatement();
ResultSet rs;
rs=st.executeQuery("Select * From table1");
p.println("<html>");
p.println("<body bgcolor=#C3C3FF>");
p.println("<font face=verdana size=4 color=#b88ff9>");
p.print("<table border=1><tr><th>Code");
p.println("</th><th>Name</th><th>Designation</th><th>Salary<
/th></tr>");
while (rs.next())
{
p.println("<tr><td>" + rs.getInt(1)+
"</td><td>" + rs.getString(2)+
"</td><td>"+rs.getString(3)+
"</td><td>"+rs.getInt(4)+
"</td></tr>");
}
p.println("</table>") ;
p.println("</body></html>") ;
}//End of try
catch(ClassNotFoundException e)
{}
catch(SQLException T)
{}
}
}
Program 5
ps.executeUpdate();
p.println("<html>");
p.println("<body>");
p.println("Data Is Added");
p.println("</html>");
p.println("</body>");
}//End of try
catch(ClassNotFoundException e)
{}
catch(SQLException T)
{}
}
}
Program 6
//Write a program to Update record using servlet
HTML Program
<HTML>
<BODY bgcolor="white">
<FORM Method="Get" ACTION="http://localhost:8080/Mywebproject/Servletmod">
<BR>
<font size=5 color="red">
Enter Code For Modification
<INPUT TYPE=text name="cd" Value="">
<br>
<INPUT TYPE=submit name=submit Value="Modify">
</font>
</FORM>
</BODY>
</HTML>
Servlet Program6(1)Part
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servletmod extends HttpServlet
{
public void doGet(HttpServletRequest rq, HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter p=res.getWriter();
int mcode;
int rt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:san","","");
Statement stmt=con.createStatement();
String buff;
ResultSet rs;
mcode=Integer.parseInt(rq.getParameter("cd"));
rs=stmt.executeQuery("select * from table1 where code="+mcode);
int i=0;
while(rs.next())
{
i++;
p.println("<html>");
p.println("<body bgcolor=red>");
p.println("<font face=verdana size=4 color=blue>");
p.println("<h1>MODIFICATION FORM</h1>");
p.println("<form method=Get
action=http://localhost:8080/Mywebproject/Sermodify>");
p.println("Code");
p.println("<input type=text name=cd value='"+rs.getInt(1)+"'
size=20><br>");
p.println("Name");
p.println("<input type=text name=nm value='"+rs.getString(2)+"'
size=20><br>");
p.println("Designation");
p.println("<input type=text name=des value='"+rs.getString(3)+"'
size=20><br>");
p.println("Salary");
p.println("<input type=text name=sal value='"+rs.getInt(4)+"'
size=20><br>");
p.println("<input type=submit value=Update>");
p.println("</form>");
p.println("</body></html>");
}
if(i==0)
{
p.println("<html>");
p.println("<body bgcolor=red>");
p.println("<font face=verdana size=4 color=blue>");
p.println("<h1>Record Not Found</h1>");
p.println("</body></html>");
}
}//End of try
catch(ClassNotFoundException e)
{}
catch(SQLException T)
{}
}
Servlet Program(6) 2 Part
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Sermodify extends HttpServlet
{
public void doGet(HttpServletRequest rq, HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter p=res.getWriter();
int mcode,msal;
String mname,mdesig;
int rt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:san","","");
Statement stmt=con.createStatement();
String buff;
ResultSet rs;
mcode=Integer.parseInt(rq.getParameter("cd"));
mname=rq.getParameter("nm");
mdesig=rq.getParameter("des");
msal=Integer.parseInt(rq.getParameter("sal"));
buff="Update table1 Set
code='"+mcode+"',name='"+mname+"',desig='"+mdesig+"',salary='"+msal+"' where
code="+mcode;
rt=stmt.executeUpdate(buff);
if(rt>0)
{
p.println("<html>");
p.println("<body bgcolor=red>");
p.println("<font face=verdana size=4 color=blue>");
p.println("<h1>Record Updated</h1>");
p.println("</body></html>");
}
else
{
p.println("<html>");
p.println("<body bgcolor=red>");
p.println("<font face=verdana size=4 color=blue>");
p.println("<h1>Record Not Updated</h1>");
p.println("</body></html>");
}
}//End of try
catch(ClassNotFoundException e)
{}
catch(SQLException T)
{}
}
}
Program 7
//Write a program to Delete record using servlet
HTML Program
<HTML>
<BODY bgcolor="white">
<FORM Method="Get" ACTION="http://localhost:8080/Mywebproject/Servletdel">
<BR>
<font size=5 color="red">
Enter Code For Deletion
<INPUT TYPE=text name="cd" Value="">
<br>
<INPUT TYPE=submit name=submit Value="Delete">
</font>
</FORM>
</BODY>
</HTML>
Servlet Program
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servletdel extends HttpServlet
{
public void doGet(HttpServletRequest rq, HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter p=res.getWriter();
int mcode;
int rt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:san","","");
Statement stmt=con.createStatement();
String buff;
ResultSet rs;
mcode=Integer.parseInt(rq.getParameter("cd"));
rs=stmt.executeQuery("select * from table1 where code="+mcode);
int i=0;
while(rs.next())
{
i++;
}
if(i>0)
{
buff="delete from table1 where code="+mcode;
rt=stmt.executeUpdate(buff);
}
if(i>0)
{
p.println("<html>");
p.println("<body bgcolor=red>");
p.println("<font face=verdana size=4 color=blue>");
p.println("<h1>Record Deleted");
}
else
{
p.println("<html>");
p.println("<body bgcolor=red>");
p.println("<font face=verdana size=4 color=blue>");
p.println("<h1>Record Not Found");
}
p.println("</body></html>");
}
catch(ClassNotFoundException e)
{}
catch(SQLException T)
{}
}
}
javax.servlet.http.HttpServlet
public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
doPost()
URL bar.
7 It allows bookmarks.
.What are the differences between the ServletConfig interface and the
ServletContext interface?
ServletConfig
The ServletConfig interface is
implemented by the servlet container
in order to pass configuration
information to a servlet. The server
passes an object that implements the
ServletConfig interface to the
servlet's init() method.
ServletContext
A ServletContext defines a set of
methods that a servlet uses to
communicate with its servlet
container.
HttpServlet
Cookies are text files stored on the client computer and they are kept
for various information tracking purpose. Java Servlets transparently
supports HTTP cookies.
There are three steps involved in identifying returning users:
10
This method sets the boolean value indicating whether the cookie
should only be sent over encrypted (i.e. SSL) connections.
11
12
1. Overview of Cookies
Cookies are small bits of textual information that a Web server sends to a browser and
that the browser returns unchanged when visiting the same Web site or domain later. By
having the server read information it sent the client previously, the site can provide
visitors with a number of conveniences:
Benefits of Cookies
There are four typical ways in which cookies can add value to your site.
Identifying a user during an e-commerce session. This type of
short-term tracking is so important that another API is layered on top
of cookies for this purpose.
<html>
<body>
<form action="HelloForm" method="GET">
First Name: <input type="text" name="first_name">
<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Keep above HTML content in a file Hello.htm and put it in <Tomcat-installationdirectory>/webapps/ROOT directory. When you would access
http://localhost:8080/Hello.htm, here is the actual output of the above form.
First Name:
Last Name:
Try to enter First Name and Last Name and then click submit button. This would display
first name and last name on your screen and same time it would set two cookies
firstName and lastName which would be passed back to the server when next time you
would press Submit button.
Program how you would access these cookies back in your web application.
Reading Cookies with Servlet:
To read cookies, you need to create an array of javax.servlet.http.Cookie objects by
calling the getCookies( ) method of HttpServletRequest. Then cycle through the array,
and use getName() and getValue() methods to access each cookie and associated value.
Example:
Let us read cookies which we have set in previous example:
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class ReadCookies extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Cookie cookie = null;
Cookie[] cookies = null;
// Get an array of Cookies associated with this domain
cookies = request.getCookies();
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Cookies Example";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title +
"</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" );
if( cookies != null ){
out.println("<h2> Found Cookies Name and
Value</h2>");
for (int i = 0; i < cookies.length; i++){
cookie = cookies[i];
out.print("Name : " + cookie.getName( ) + ",
");
out.print("Value: " + cookie.getValue( )+"
<br/>");
}
}else{
out.println(