Servlet Programs
Servlet Programs
Servlet Programs
Servlets are the Java platform technology of choice for extending and
enhancing Web servers. Servlet provides a component based,
platform-independent method for building Web-based applications,
without the performance limitations like CGI programs. And unlike
proprietary server extension mechanisms (Netscape server API or
Apache Modules), servlets are server and platform-independent.
Servlets have access to the entire family of Java APIs, including the
JDBC API to access enterprise databases. Servlets can also access a
library of HTTP-specific calls and receive all the benefits of the mature
Java language, including portability, performance, reusability, and
crash protection.
PROGRAMS
Program 1: write a program to demonstrate ServletContext object
And ServletConfig object
First.java
package MAINPACK;
/*ServletConfig interface
public String getInitParameter(String name): Returns the value of
given parameter as String, or null if the given parameter doesn’t
exist in web.xml.
public Enumeration getInitParameterNames(): Returns an enumeration of
all the parameter names.
public String getServletName(): Returns the name of the servlet
instance.
public ServletContext getServletContext(): Returns an object of
ServletContext.
*/
/*
ServletContext interface
public String getInitParameter(String name):Returns the parameter
value for the specified parameter name.
public Enumeration getInitParameterNames():Returns the names of the
context's initialization parameters.
public void setAttribute(String name,Object object):sets the given
object in the application scope.
public Object getAttribute(String name):Returns the attribute for the
specified name.
public void removeAttribute(String name):Removes the attribute with
the given name from the servlet context.
*/
import java.io.IOException;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class First
*/
/**
* @see HttpServlet#HttpServlet()
*/
public First() {
//ServletContext object created when application SERVLETAPP is
deployed
/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());
System.out.println(getServletConfig());
System.out.println(getServletContext());
System.out.println(getServletInfo());
System.out.println(getServletName());
/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
Second.java
package MAINPACK;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Second
*/
/**
* @see HttpServlet#HttpServlet()
*/
public Second() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());
System.out.println(getServletConfig());
System.out.println(getServletContext());
System.out.println(getServletInfo());
System.out.println(getServletName());
RequestDispatcher rd=request.getRequestDispatcher("/fir");
rd.forward(request,response);
}
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;
}
}
Output:
HTML:
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Greeting Message</TITLE>
</HEAD>
<BODY BGCOLOR="red" TEXT="blue">
<FORM METHOD="get" ACTION="Welcome">
<B>Enter Your Name:</B>
<INPUT TYPE="text" NAME="uname"><BR><BR>
<INPUT TYPE="submit" VALUE="Click To Submit">
</FORM>
</BODY>
</HTML>
Servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
Output:
Program 9: Demonstrate how the parameters are accepted from
HTML and insert into database through Servlets
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Register form</title>
</head>
<body>
<form method="post" action="register">
Emp id:<input type="text" name="id" /><br/>
Emp Name:<input type="text" name="name" /><br/>
Emp Salary:<input type="text" name="salary" /><br/>
<input type="submit" value="register" />
</form>
</body>
</html>
Servlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
String id = request.getParameter("id");
int empid = Integer.parseInt(id);
String name = request.getParameter("name");
String sal = request.getParameter("salary");
double salary=Double.parseDouble(sal);
try{
PreparedStatement ps=con.prepareStatement
("insert into employee values(?,?,?)");
ps.setInt(1, empid);
ps.setString(2, name);
ps.setDouble(3,salary);
int i=ps.executeUpdate();
if(i>0)
{
out.println("You are sucessfully registered");
}
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from employee");
out.println("<html><head><title>employee</title></head>");
out.println("<body bgcolor=green><center>");
out.println("<table border=2>");
out.println("<tr><td>empid</td><td>name</td><td>salary</td></tr>");
while(rs.next())
{
out.println("<tr><td>"+rs.getInt(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getDouble(3)+"</td></tr>");
}
out.println("</center></table></body></html>");
}
catch(Exception se)
{
se.printStackTrace();
}
//doGet(request, response);
}
MySQL:
Use test;
Create table employee(empid int,name varchar(50),salary double);
Query OK, 0 rows affected (0.37 sec)
Output:
Program 10: Write a Program to set cookies and display
cookies using Servlets
Program:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
}Output:
Program 11: Write a program to display Session Information
using Servlets.
Program
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
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 javax.servlet.http.HttpSession;
pw.println( "</BODY></HTML>" );
res.getWriter().append("Served at: ").append(req.getContextPath());
}
}
Output: