• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Servlet


What is Servlet
Servlets are Java classes that are used to implement web applications. They are compiled to bytecode that can run on a Java Virtual Machine (JVM), and they are specifically designed to handle requests and generate responses in a web environment.
A servlet receives a request from a client, processes it, and generates a response. It can be used to create dynamic web pages, handle form data, and manage sessions. Servlets are usually part of a larger application that is run on a web server, such as Apache Tomcat or Jetty.
To use servlets, you will need a web container (also known as a servlet container) that supports the Servlet API. The Servlet API is a set of Java classes and interfaces that define how a servlet should behave and interact with the web container and with clients.

There are several benefits to using servlets in web development:
They are fast: Servlets are written in Java, which is a compiled language. This means that they are faster than interpreted languages, such as Python or Ruby, because the code is already compiled and ready to run.
They are flexible: Servlets can be used to generate dynamic content, handle form data, and manage sessions. This makes them suitable for a wide range of web applications.
They are portable: Servlets are designed to run on any web server that supports the Servlet API, so they can be easily deployed on different servers.
They are secure: Servlets can be used to implement security measures, such as authentication and authorization, to protect sensitive data.

Key Points

  • Servlets

Image
Simple Servlet Program
Steps: click on your project
go to src
go to main
go to java
right click on java
new
other
servlet
next
Enter className first
next
next
select init,service,destroy methods


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
 * Servlet implementation class first1
 */
@WebServlet("/first1")
public class first1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public first1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		int x=10,y=20,z;
		z=x+y;
		out.print(z);
	}
	

}

    

Output:
30
Servlet Program with HTML
Step -1: Create html file
Second.html
Step -2: Create Servlet file
Second
Second.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="second" method="get">
First Number<input type="text" name="fno" ><br>
Second Number<input type="text" name="sno" ><br>
<input type="submit" value="add me">
</form>

</body>
</html>

 
Second.servlet


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class second1
 */
@WebServlet("/second1")
public class second1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public second1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		int x,y,z;;
		x=Integer.parseInt(request.getParameter("fno"));
		y=Integer.parseInt(request.getParameter("sno"));
		z=x+y;
		out.print(z);
		
	}
	

}


Output:
24
Servlet Program with HTML and Database
Step -1: Create html file
third.html
Step -2: Create Servlet file
third
Step -3: Create table in Database:
create table calc (fno int,sno int,res int);

third.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="third" method="get">
First Number<input type="text" name="fno" ><br>
Second Number<input type="text" name="sno" ><br>
<input type="submit" value="add me">
</form>

</body>
</html>

third.servlet


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class third1
 */
@WebServlet("/third1")
public class third1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public third1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			//String url="jdbc:oracle:thin:@localhost:1521:XE";
			// con = DriverManager.getConnection(url,"System","1234");

				
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		con=DriverManager.getConnection("jdbc:mysql://localhost:4306/Rain","root","");
		
		stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}


	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		try
		{
		int x,y,z;;
		x=Integer.parseInt(request.getParameter("fno"));
		y=Integer.parseInt(request.getParameter("sno"));
		z=x+y;
		out.print(z);
		
		String s= "insert into calc values('"+x+"','"+y+"','"+z+"')";
		stmt.executeUpdate(s);
		}
		catch(Exception e)
		{
			out.print(e);
		}
		
	}

}


Output:
Servlet Program -4

Registration Form:


Step -1 : reg.html
Step -2 : reg.servlet
Step -3 : reg Table in Database
    
    
create table reg(
        username varchar(20),
        password varchar(20),
        gender varchar(20),
        location varchar(20),
        l1 varchar(20),
        l2 varchar(20),
        l3 varchar(20) ,
        address varchar(20)
        );
reg.html
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="reg" method="post">

Username:<input type="text" name="uname" id="uname"><br>
Password:<input type="password" name="pass" id="pass"><br>
gender<input type="radio" name="gender" id="gender" value="male">Malee
<input type="radio" name="gender" id="gender" value="female">Femalee<br>

location:<select name="loc">

<option value="hyd">Hyderabad</option>
<option value="del">Delhi</option>
<option value="che">Chennai</option>

</select>
<br>
Languages:<input type="checkbox" name="telugu" value="telugu" id="telugu">Teluguu
<input type="checkbox" name="English" value="English" id="English">"English"
<input type="checkbox" name="hindi" value="hindi" id="hindi">hindi

Address:<textarea rows="20" cols="20" id="adder" name="addr"></textarea>
<br>
<input type="submit" value="sign up">
</form>

</body>
</html>





reg.servlet



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class reg1
 */
@WebServlet("/reg1")
public class reg1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public reg1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			//String url="jdbc:oracle:thin:@localhost:1521:XE";
			// con = DriverManager.getConnection(url,"System","1234");

				
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		con=DriverManager.getConnection("jdbc:mysql://localhost:4306/Rain","root","");
		
		stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}
	

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		try
		{
			String username="",password="",gender="",loc="",l1="",l2="",l3="",addr="";
			username=(request.getParameter("uname"));
			password=(request.getParameter("pass"));
			gender=(request.getParameter("gender"));
			loc=(request.getParameter("loc"));
			l1=(request.getParameter("telugu"));
			l2=(request.getParameter("english"));
			l3=(request.getParameter("hindi"));
			addr=(request.getParameter("addr"));
			String s= "INSERT INTO reg VALUES ('"+username+"', '"+password+"', '"+gender+"','"+loc+"','"+l1+"','"+l2+"','"+l3+"','"+addr+"')";
			stmt.executeUpdate(s);
			out.print("Successfully Inserted");
		}
		catch(Exception e)
		{
			out.print(e);
		}
		
	}
	

}


    

Output:
Servlet Program -5

Login Form(Without redirect)

Steps:
Step 1: login.html
Step 2: login.servlet
Login html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">

Username<input type="text" name="uname"><br>
Password<input type="password" name="pass"><br>
<input type="submit" value="login">



</form>
</body>
</html>

 
login.servlet
    


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class login1
 */
@WebServlet("/login1")
public class login1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
    PrintWriter out=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  
		 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
	     String url="jdbc:oracle:thin:@localhost:1521:XE";
		 con = DriverManager.getConnection(url,"System","1234");
	   	 stmt = con.createStatement();
		 }
		 catch(Exception e)
		 {
			
	    }

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    out=response.getWriter();
		String x="",y="",dx="",dy="";
		x=request.getParameter("uname");
		y=request.getParameter("pass");
		
		try
		{
		String s="select * from reg where username='"+x+"' and password='"+y+"'";
		ResultSet rs=stmt.executeQuery(s);
		        if(rs.next())   
		        {
		            dx=rs.getString(1);
		            dy=rs.getString(2);  
		        }
		        
		        if(x.equals(dx) && y.equals(dy))
		        {
		        	out.print("log in success");
		        }
		        else
		        {
		        	out.print("login failed");
		        }
		        
		}
		catch(Exception e)
		{
			
		}
		    
		
	}
	

}





Output:
Servlet Program -6

Login Form(With redirect)

Steps:
Step 1: login.html
Step 2: success.html
Step 3: login.servlet
Step 1: login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">

Username<input type="text" name="uname"><br>
Password<input type="password" name="pass"><br>
<input type="submit" value="login">

</form>
</body>
</html>

 
Step 2: success.html
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>login success</h1>

</body>
</html>


    
Step 3: login.servlet
    
    


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class login1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
    PrintWriter out=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  
		 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
	     String url="jdbc:oracle:thin:@localhost:1521:XE";
		 con = DriverManager.getConnection(url,"System","1234");
	   	 stmt = con.createStatement();
		 }
		 catch(Exception e)
		 {
			
	    }

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    out=response.getWriter();
		String x="",y="",dx="",dy="";
		x=request.getParameter("uname");
		y=request.getParameter("pass");
		
		try
		{
		String s="select * from reg where username='"+x+"' and password='"+y+"'";
		ResultSet rs=stmt.executeQuery(s);
		        if(rs.next())   
		        {
		            dx=rs.getString(1);
		            dy=rs.getString(2);  
		        }
		        
		        if(x.equals(dx) && y.equals(dy))
		        {
		        	response.sendRedirect("success.html");
		        }
		        else
		        {
		        	out.print("login failed");
		        }
		        
		}
		catch(Exception e)
		{
			
		}
		    
		
	}
	

}





Output:
Login Page(with using session)
Step 1: login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">

Username<input type="text" name="uname"><br>
Password<input type="password" name="pass"><br>
<input type="submit" value="login">

</form>
</body>
</html>

 
Step 2: success.html
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>login success</h1>

</body>
</html>


    
Step 3: login.servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class login1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
    PrintWriter out=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  
		 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
	     String url="jdbc:oracle:thin:@localhost:1521:XE";
		 con = DriverManager.getConnection(url,"System","1234");
	   	 stmt = con.createStatement();
		 }
		 catch(Exception e)
		 {
			
	    }

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    out=response.getWriter();
		String x="",y="",dx="",dy="";
		x=request.getParameter("uname");
		y=request.getParameter("pass");
		
		 HttpSession session=request.getSession();
		 session.setAttribute("uname",x);
		 session.setAttribute("pass",y);
		
		try
		{
		String s="select * from reg where username='"+x+"' and password='"+y+"'";
		ResultSet rs=stmt.executeQuery(s);
		        if(rs.next())   
		        {
		            dx=rs.getString(1);
		            dy=rs.getString(2);  
		        }
		        
		        if(x.equals(dx) && y.equals(dy))
		        {
		        	response.sendRedirect("success.html");
		        }
		        else
		        {
		        	out.print("login failed");
		        }
		        
		}
		catch(Exception e)
		{
			
		}
		    
		
	}
	

}




Output:
Login Page(with using cookies)
Step 1: login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">

Username<input type="text" name="uname"><br>
Password<input type="password" name="pass"><br>
<input type="submit" value="login">

</form>
</body>
</html>

 
Step 2: success.html
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>login success</h1>

</body>
</html>


    
Step 3: login.servlet
    
    


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class login1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
    PrintWriter out=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  
		 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
	     String url="jdbc:oracle:thin:@localhost:1521:XE";
		 con = DriverManager.getConnection(url,"System","1234");
	   	 stmt = con.createStatement();
		 }
		 catch(Exception e)
		 {
			
	    }

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    out=response.getWriter();
		String x="",y="",dx="",dy="";
		x=request.getParameter("uname");
		y=request.getParameter("pass");
		
		
		Cookie ck1=new Cookie("uname",x);//creating cookie object  
	    response.addCookie(ck1);//adding cookie in the response  

	    Cookie ck2=new Cookie("pass",y);//creating cookie object  
	    response.addCookie(ck2);//adding cookie in the response 
		
		try
		{
		String s="select * from reg where username='"+x+"' and password='"+y+"'";
		ResultSet rs=stmt.executeQuery(s);
		        if(rs.next())   
		        {
		            dx=rs.getString(1);
		            dy=rs.getString(2);  
		        }
		        
		        if(x.equals(dx) && y.equals(dy))
		        {
		        	response.sendRedirect("success.html");
		        }
		        else
		        {
		        	out.print("login failed");
		        }
		        
		}
		catch(Exception e)
		{
			
		}
		    
		
	}
	

}



Output:

Servlet Program


Success Page

Key Points

  • What is Servlet
  • What is Sessions
  • What is Cookies

Image
Servlet Program -7

Success.html

                                       
success.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>Servlet login success</h1> <form action="view" method="post"> <input type="submit" value="view my details"> </form> <form action="viewtable" method="post"> <input type="submit" value="view in table"> </form> <form action="viewlist" method="post"> <input type="submit" value="view in list"> </form> <form action="viewtextbox" method="post"> <input type="submit" value="view in textbox"> </form> <form action="edit" method="post"> <input type="submit" value="edit my details"> </form> </body> </html>
Output:
Servlet Program -8
view.servlet
View Data
View Program(without using sessions and cookies)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class view
 */
@WebServlet("/view")
public class view extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public view() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			String url="jdbc:oracle:thin:@localhost:1521:XE";
			con = DriverManager.getConnection(url,"System","1234");
		    stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		try
		{
		String s="select * from reg ";
		ResultSet rs=stmt.executeQuery(s);
		       while(rs.next())   
		        {
		            out.print(rs.getString(1));
		            out.print(rs.getString(2));
		            out.print(rs.getString(3));
		            out.print(rs.getString(4));
		            out.print(rs.getString(5));
		            out.print(rs.getString(6));
		            out.print(rs.getString(7));
		            out.print(rs.getString(8));
		              
		        }
		}
		catch(Exception e)
		{
			
		}
		    
		
	}

}



Output:
View Program(with using sessions)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class view
 */
@WebServlet("/view")
public class view extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public view() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			String url="jdbc:oracle:thin:@localhost:1521:XE";
			con = DriverManager.getConnection(url,"System","1234");
		    stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		String x,y;
		try
		{
		
		HttpSession session=request.getSession(false);
		x=(String)session.getAttribute("uname");
		y=(String)session.getAttribute("pass");
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		       while(rs.next())   
		        {
		            out.print(rs.getString(1));
		            out.print(rs.getString(2));
		            out.print(rs.getString(3));
		            out.print(rs.getString(4));
		            out.print(rs.getString(5));
		            out.print(rs.getString(6));
		            out.print(rs.getString(7));
		            out.print(rs.getString(8));
		              
		        }
		}
		catch(Exception e)
		{
			
		}
		    
		
	}

}



Output:
View Program(with using cookies)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class view
 */
@WebServlet("/view")
public class view extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public view() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			String url="jdbc:oracle:thin:@localhost:1521:XE";
			con = DriverManager.getConnection(url,"System","1234");
		    stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		String x,y;
		try
		{
		
		Cookie ck[]=request.getCookies();
		x=ck[0].getValue();
		y=ck[1].getValue();
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		       while(rs.next())   
		        {
		            out.print(rs.getString(1));
		            out.print(rs.getString(2));
		            out.print(rs.getString(3));
		            out.print(rs.getString(4));
		            out.print(rs.getString(5));
		            out.print(rs.getString(6));
		            out.print(rs.getString(7));
		            out.print(rs.getString(8));
		              
		        }
		}
		catch(Exception e)
		{
			
		}
		    
		
	}

}



Output:
view Program -9
viewtable
View Data in html Table
Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Centro comercial Moctezuma Francisco Chang Mexico
View Data in html Table Program(without using sessions and cookies)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewtable1
 */
@WebServlet("/viewtable1")
public class viewtable1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewtable1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		  String url="jdbc:oracle:thin:@localhost:1521:XE";
		  con = DriverManager.getConnection(url,"System","1234");
		 stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		
		
		
		try
		{
		String s="select * from reg ";
		ResultSet rs=stmt.executeQuery(s);
		out.print("<table border=1>");
		       while(rs.next())   
		        {
		    	   out.print("<tr>");
		            out.print("<td>"+rs.getString(1)+"</td>");
		            out.print("<td>"+rs.getString(2)+"</td>");
		            out.print("<td>"+rs.getString(3)+"</td>");
		            out.print("<td>"+rs.getString(4)+"</td>");
		            out.print("<td>"+rs.getString(5)+"</td>");
		            out.print("<td>"+rs.getString(6)+"</td>");
		            out.print("<td>"+rs.getString(7)+"</td>");
		            out.print("<td>"+rs.getString(8)+"</td>");
		            out.print("</tr>");
		              
		        }
		        
		       out.print("</table>");
		        
		}


		catch(Exception e)
		{
			
		}
		    
		
	}
	

}

Output:
View Data in html Table Program(with using sessions)
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewtable1
 */
@WebServlet("/viewtable1")
public class viewtable1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewtable1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		  String url="jdbc:oracle:thin:@localhost:1521:XE";
		  con = DriverManager.getConnection(url,"System","1234");
		 stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		
		String x,y;
		try
		{
		HttpSession session=request.getSession(false);
		x=(String)session.getAttribute("uname");
		y=(String)session.getAttribute("pass");
		
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		out.print("<table border=1>");
		       while(rs.next())   
		        {
		    	   out.print("<tr>");
		            out.print("<td>"+rs.getString(1)+"</td>");
		            out.print("<td>"+rs.getString(2)+"</td>");
		            out.print("<td>"+rs.getString(3)+"</td>");
		            out.print("<td>"+rs.getString(4)+"</td>");
		            out.print("<td>"+rs.getString(5)+"</td>");
		            out.print("<td>"+rs.getString(6)+"</td>");
		            out.print("<td>"+rs.getString(7)+"</td>");
		            out.print("<td>"+rs.getString(8)+"</td>");
		            out.print("</tr>");
		              
		        }
		        
		       out.print("</table>");
		        
		}
		catch(Exception e)
		{
			
		}
		    
		
	}
	

}

Output:
View Data in html Table Program(with using cookies)
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewtable1
 */
@WebServlet("/viewtable1")
public class viewtable1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewtable1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		  String url="jdbc:oracle:thin:@localhost:1521:XE";
		  con = DriverManager.getConnection(url,"System","1234");
		 stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		
		String x,y;
		try
		{
		Cookie ck[]=request.getCookies();
		x=ck[0].getValue();
		y=ck[1].getValue();
		
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		out.print("<table border=1>");
		       while(rs.next())   
		        {
		    	   out.print("<tr>");
		            out.print("<td>"+rs.getString(1)+"</td>");
		            out.print("<td>"+rs.getString(2)+"</td>");
		            out.print("<td>"+rs.getString(3)+"</td>");
		            out.print("<td>"+rs.getString(4)+"</td>");
		            out.print("<td>"+rs.getString(5)+"</td>");
		            out.print("<td>"+rs.getString(6)+"</td>");
		            out.print("<td>"+rs.getString(7)+"</td>");
		            out.print("<td>"+rs.getString(8)+"</td>");
		            out.print("</tr>");
		              
		        }
		        
		       out.print("</table>");
		        
		}
		catch(Exception e)
		{
			
		}
		    
		
	}
	

}
Output:
Servlet Program -10
viewlist.servlet
View Data in html List
  • Coffee
  • Tea
  • Milk
View Data in html List Program(without using sessions and cookies)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewlist1
 */
@WebServlet("/viewlist1")
public class viewlist1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewlist1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		  String url="jdbc:oracle:thin:@localhost:1521:XE";
		  con = DriverManager.getConnection(url,"System","1234");
		  stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		
		
		
		try
		{
		String s="select * from reg ";
		ResultSet rs=stmt.executeQuery(s);
		
		       while(rs.next())   
		        {
		    	   out.print("<ol type=1>");
		            out.print("<li>"+rs.getString(1)+"</li>");
		            out.print("<li>"+rs.getString(2)+"</li>");
		            out.print("<li>"+rs.getString(3)+"</li>");
		            out.print("<li>"+rs.getString(4)+"</li>");
		            out.print("<li>"+rs.getString(5)+"</li>");
		            out.print("<li>"+rs.getString(6)+"</li>");
		            out.print("<li>"+rs.getString(7)+"</li>");
		            out.print("<li>"+rs.getString(8)+"</li>");
		            out.print("</ol>");
		              
		        }



		}
		catch(Exception e)
		{
			
		}
		
	}
	

}


Output:
View Data in html List Program(with using sessions)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewlist1
 */
@WebServlet("/viewlist1")
public class viewlist1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewlist1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		  String url="jdbc:oracle:thin:@localhost:1521:XE";
		  con = DriverManager.getConnection(url,"System","1234");
		  stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		
		String x,y;
		try
		{
		HttpSession session=request.getSession(false);
		x=(String)session.getAttribute("uname");
		y=(String)session.getAttribute("pass");
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		
		       while(rs.next())   
		        {
		    	   out.print("<ol type=1>");
		            out.print("<li>"+rs.getString(1)+"</li>");
		            out.print("<li>"+rs.getString(2)+"</li>");
		            out.print("<li>"+rs.getString(3)+"</li>");
		            out.print("<li>"+rs.getString(4)+"</li>");
		            out.print("<li>"+rs.getString(5)+"</li>");
		            out.print("<li>"+rs.getString(6)+"</li>");
		            out.print("<li>"+rs.getString(7)+"</li>");
		            out.print("<li>"+rs.getString(8)+"</li>");
		            out.print("</ol>");
		              
		        }

		}
		catch(Exception e)
		{
			
		}
		
	}
	

}

    
Output:
View Data in html List Program(with using cookies)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewlist1
 */
@WebServlet("/viewlist1")
public class viewlist1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewlist1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		  String url="jdbc:oracle:thin:@localhost:1521:XE";
		  con = DriverManager.getConnection(url,"System","1234");
		  stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		
		String x,y;
		try
		{
		Cookie ck[]=request.getCookies();
		x=ck[0].getValue();
		y=ck[1].getValue();
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		
		      while(rs.next())   
		        {
		    	   out.print("<ol type=1>");
		            out.print("<li>"+rs.getString(1)+"</li>");
		            out.print("<li>"+rs.getString(2)+"</li>");
		            out.print("<li>"+rs.getString(3)+"</li>");
		            out.print("<li>"+rs.getString(4)+"</li>");
		            out.print("<li>"+rs.getString(5)+"</li>");
		            out.print("<li>"+rs.getString(6)+"</li>");
		            out.print("<li>"+rs.getString(7)+"</li>");
		            out.print("<li>"+rs.getString(8)+"</li>");
		            out.print("</ol>");
		              
		        }



		}
		catch(Exception e)
		{
			
		}
		
	}
	

}


Output:
Servlet Program -11
viewtextbox.servlet
View Data in html TextBox
  • Coffee
  • Tea
  • Milk
View Data in html TextBox Program(without using sessions and cookies)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewtext1
 */
@WebServlet("/viewtext1")
public class viewtext1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewtext1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			String url="jdbc:oracle:thin:@localhost:1521:XE";
			con = DriverManager.getConnection(url,"System","1234");
		
		stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		try
		{
		String s="select * from reg ";
		ResultSet rs=stmt.executeQuery(s);
		out.print("<form action='' method=''>");
		 while(rs.next())   
		  {
		      out.print("Username <input type='text' name='uname' value='"+rs.getString(1)+"'> <br>");
		      out.print("Passwod <input type='text' name='pass' value='"+rs.getString(2)+"'> <br>");
		      out.print("Gender <input type='text' name='gender' value='"+rs.getString(3)+"'> <br>");
		      out.print("location <input type='text' name='loc' value='"+rs.getString(4)+"'> <br>");
		      out.print("language-1 <input type='text' name='l1' value='"+rs.getString(5)+"' ><br>");
		      out.print("language-2 <input type='text' name='l2' value='"+rs.getString(6)+"'> <br>");
		      out.print("language-3 <input type='text' name='l3' value='"+rs.getString(7)+"'> <br>");
		      out.print("Address <input type='text' name='addr' value='"+rs.getString(8)+"'> <br>");
		  }
		  out.print("</form>");
		}
		catch(Exception e)
		{
			
		}
		
	}
	

}


Output:
View Data in html TextBox Program(with using sessions)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewtext1
 */
@WebServlet("/viewtext1")
public class viewtext1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewtext1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			String url="jdbc:oracle:thin:@localhost:1521:XE";
			con = DriverManager.getConnection(url,"System","1234");
		
		stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		String x,y;
		
		try
		{
		HttpSession session=request.getSession(false);
		x=(String)session.getAttribute("uname");
		y=(String)session.getAttribute("pass");
		
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		out.print("<form action='' method=''>");
		 while(rs.next())   
		  {
		      out.print("Username <input type='text' name='uname' value='"+rs.getString(1)+"'> <br>");
		      out.print("Passwod <input type='text' name='pass' value='"+rs.getString(2)+"'> <br>");
		      out.print("Gender <input type='text' name='gender' value='"+rs.getString(3)+"'> <br>");
		      out.print("location <input type='text' name='loc' value='"+rs.getString(4)+"'> <br>");
		      out.print("language-1 <input type='text' name='l1' value='"+rs.getString(5)+"' ><br>");
		      out.print("language-2 <input type='text' name='l2' value='"+rs.getString(6)+"'> <br>");
		      out.print("language-3 <input type='text' name='l3' value='"+rs.getString(7)+"'> <br>");
		      out.print("Address <input type='text' name='addr' value='"+rs.getString(8)+"'> <br>");
		  }
		  out.print("</form>");
		}
		catch(Exception e)
		{
			
		}
		
	}
	

}

    
Output:
View Data in html TextBox Program(with using cookies)


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class viewtext1
 */
@WebServlet("/viewtext1")
public class viewtext1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewtext1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			String url="jdbc:oracle:thin:@localhost:1521:XE";
			con = DriverManager.getConnection(url,"System","1234");
		
		stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		String x,y;
		
		try
		{
		Cookie ck[]=request.getCookies();
		x=ck[0].getValue();
		y=ck[1].getValue();
		
		String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		ResultSet rs=stmt.executeQuery(s);
		out.print("<form action='' method=''>");
		 while(rs.next())   
		  {
		      out.print("Username <input type='text' name='uname' value='"+rs.getString(1)+"'> <br>");
		      out.print("Passwod <input type='text' name='pass' value='"+rs.getString(2)+"'> <br>");
		      out.print("Gender <input type='text' name='gender' value='"+rs.getString(3)+"'> <br>");
		      out.print("location <input type='text' name='loc' value='"+rs.getString(4)+"'> <br>");
		      out.print("language-1 <input type='text' name='l1' value='"+rs.getString(5)+"' ><br>");
		      out.print("language-2 <input type='text' name='l2' value='"+rs.getString(6)+"'> <br>");
		      out.print("language-3 <input type='text' name='l3' value='"+rs.getString(7)+"'> <br>");
		      out.print("Address <input type='text' name='addr' value='"+rs.getString(8)+"'> <br>");
		  }
		  out.print("</form>");
		}
		catch(Exception e)
		{
			
		}
		
	}
	

}


Output:
Servlet Program -12
edit.servlet
Edit Data in textBox


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class edit
 */
@WebServlet("/edit")
public class edit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public edit() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			//String url="jdbc:oracle:thin:@localhost:1521:XE";
			// con = DriverManager.getConnection(url,"System","1234");

				
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		con=DriverManager.getConnection("jdbc:mysql://localhost:4306/Rain","root","");
		
		stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}
	

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		String x,y;
	
		try
		{
		 HttpSession session=request.getSession(false);
		 x=(String)session.getAttribute("uname");
		 y=(String)session.getAttribute("pass");
		 String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		 ResultSet rs=stmt.executeQuery(s);
		 out.print("<form action='update' method='post'>");
		      
		 while(rs.next())   
		  {
		      out.print("Username <input type='text' name='uname' value='"+rs.getString(1)+"'> <br>");
		      out.print("Passwod <input type='text' name='pass' value='"+rs.getString(2)+"'> <br>");
		      out.print("Gender <input type='text' name='gender' value='"+rs.getString(3)+"'> <br>");
		      out.print("location <input type='text' name='loc' value='"+rs.getString(4)+"'> <br>");
		      out.print("language-1 <input type='text' name='l1' value='"+rs.getString(5)+"' ><br>");
		      out.print("language-2 <input type='text' name='l2' value='"+rs.getString(6)+"'> <br>");
		      out.print("language-3 <input type='text' name='l3' value='"+rs.getString(7)+"'> <br>");
		      out.print("Address <input type='text' name='addr' value='"+rs.getString(8)+"'> <br>");
		  }
		  out.print("</form>");
		}
		catch(Exception e)
		{
			
		}
		
	}
	

}

 
Output:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class edit
 */
@WebServlet("/edit")
public class edit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public edit() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
			//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			//String url="jdbc:oracle:thin:@localhost:1521:XE";
			// con = DriverManager.getConnection(url,"System","1234");

				
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		con=DriverManager.getConnection("jdbc:mysql://localhost:4306/Rain","root","");
		
		stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}

	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}
	

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		String x,y;
	
		try
		{
		 Cookie ck[]=request.getCookies();
	     x=ck[1].getValue();
		 y=ck[2].getValue();

		 String s="select * from reg where username='"+x+"' and password='"+y+"' ";
		 ResultSet rs=stmt.executeQuery(s);
		 out.print("<form action='update' method='post'>");
		      
		 while(rs.next())   
		  {
		      out.print("Username <input type='text' name='uname' value='"+rs.getString(1)+"'> <br>");
		      out.print("Passwod <input type='text' name='pass' value='"+rs.getString(2)+"'> <br>");
		      out.print("Gender <input type='text' name='gender' value='"+rs.getString(3)+"'> <br>");
		      out.print("location <input type='text' name='loc' value='"+rs.getString(4)+"'> <br>");
		      out.print("language-1 <input type='text' name='l1' value='"+rs.getString(5)+"' ><br>");
		      out.print("language-2 <input type='text' name='l2' value='"+rs.getString(6)+"'> <br>");
		      out.print("language-3 <input type='text' name='l3' value='"+rs.getString(7)+"'> <br>");
		      out.print("Address <input type='text' name='addr' value='"+rs.getString(8)+"'> <br>");
		  }
		  out.print("</form>");
		}
		catch(Exception e)
		{
			
		}
		
	}
	

}

 
Output:
Servlet Program -13
update.servlet
Update
                                    


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class update
 */
@WebServlet("/update")
public class update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		   String url="jdbc:oracle:thin:@localhost:1521:XE";
	       con = DriverManager.getConnection(url,"System","1234");
		   stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}
	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}
	

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");

		String gender="",loc="",l1="",l2="",l3="",address="";
		gender=request.getParameter("gender");
		loc=request.getParameter("loc");
		l1=request.getParameter("l1");
		l2=request.getParameter("l2");
		l3=request.getParameter("l3");
		address=request.getParameter("addr");
		
		String x,y;
		try
		{
		  HttpSession session=request.getSession(false);
		  x=(String)session.getAttribute("uname");
		  y=(String)session.getAttribute("pass");
		  String s= "update reg set gender='"+gender+"',location ='"+loc+"',l1='"+l1+"',l2='"+l2+"',l3='"+l3+"',addr='"+address+"' where username='"+x+"' and password='"+y+"' ";
		  int i=stmt.executeUpdate(s);
		  if(i==1)
		  {
		  out.print("successfullly updated");
		  }
		  else
		  {
		   out.print("Updation failed");
		   }
		  
		}
		catch(Exception e)
		{
			
		}
		    
		
	}

}
 
Output:
                                    
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.Cookie; // Cookie header file
import javax.servlet.http.HttpSession; // Session header file

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class update
 */
@WebServlet("/update")
public class update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	 
    Connection con=null;
    Statement stmt=null;
	public void init(ServletConfig config) throws ServletException {
		try
		{
		   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		   String url="jdbc:oracle:thin:@localhost:1521:XE";
	       con = DriverManager.getConnection(url,"System","1234");
		   stmt = con.createStatement();
		}
		catch(Exception e)
		{
			
		}
	}
	

	/**
	 * @see Servlet#destroy()
	 */
	 
	public void destroy() {
		try
		{
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			
		}
	}
	

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	 
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");

		String gender="",loc="",l1="",l2="",l3="",address="";
		gender=request.getParameter("gender");
		loc=request.getParameter("loc");
		l1=request.getParameter("l1");
		l2=request.getParameter("l2");
		l3=request.getParameter("l3");
		address=request.getParameter("addr");
		
		String x,y;
		try
		{
		  Cookie ck[]=request.getCookies();
		  x=ck[1].getValue();
		  y=ck[2].getValue();
		  String s= "update reg set gender='"+gender+"',location ='"+loc+"',l1='"+l1+"',l2='"+l2+"',l3='"+l3+"',addr='"+address+"' where username='"+x+"' and password='"+y+"' ";
		  int i=stmt.executeUpdate(s);
		  if(i==1)
		  {
		  out.print("successfullly updated");
		  }
		  else
		  {
		   out.print("Updation failed");
		   }
		  
		}
		catch(Exception e)
		{
			
		}
		    
		
	}

}

 
Output: