Servlet
Servlet
Web Servers: Apache, Microsoft IIS, nginx, Google Web Server, LiteSpeed Web Server
Application Servers: Apache Tomcat, WebLogic (Google), WebSphere (IBM), JBoss (RedHat)
Cookies
Cookies are data, stored in small text files, on your computer.
When a web server has sent a web page to a browser, the connection is shut down, and the server
forgets everything about the user.
Cookies were invented to solve the problem "how to remember information about the user":
When a user visits a web page, his name can be stored in a cookie.
Next time the user visits the page, the cookie remembers his name.
A cookie is a name-value pair e.g. “username=msit”.
Session Management
HTTP is a stateless protocol. Each request is independent of the previous one. However, in some
applications, it is necessary to save state information so that information can be collected from
several interactions between a browser and a server. Sessions provide such a mechanism.
Servlets
Java Servlets often serve the same purpose as programs implemented using the Common
Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI.
● Servlets are platform-independent because they are written in Java.
● Servlets execute within the address space of a Web server. It is not necessary to create a
separate process to handle each client request.
● Performance is significantly better.
● Java security manager on the server enforces a set of restrictions to protect the resources
on a server machine. So servlets are trusted.
● The full functionality of the Java class libraries is available to a servlet. It can
communicate with applets, databases, or other software via the sockets and RMI
mechanisms that you have seen already.
Servlets Architecture
Servlet Life-Cycle
A servlet life cycle can be defined as the entire process from its creation till the destruction. The
following are the paths followed by a servlet.
● The servlet is initialized by calling the init() method.
● The servlet calls service() method to process a client's request.
● The servlet is terminated by calling the destroy() method.
● Finally, servlet is garbage collected by the garbage collector of the JVM.
First Servlet
HelloServlet.java
import java.io.*;
import javax.servlet.*;
ServletRequest object enables the servlet to read data that is provided via the client request.
ServletResponse object enables the servlet to formulate a response for the client.
Anything written to PrintWriter stream is sent to the client as part of the HTTP response.
ColorServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColorServlet extends HttpServlet
{
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws Servlet Exception, IOException
{
String color = request.getParameter(“color”);
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
pw.println(“<b>The selected color is: </b>”);
pw.println(color);
pw.close();
}
}
// get method is default in html form, but we can also set method=“get” explicitly.
// In Servlets, we need to replace doPost() with doGet() only.
Servlet JDBC
register.html
<html> <body>
<form action="servlet/Register" method="post">
Name:<input type="text" name="userName"/><br/><br/>
Password:<input type="password" name="userPass"/><br/><br/>
Email Id:<input type="text" name="userEmail"/><br/><br/>
Country: <select name="userCountry">
<option>India</option> <option>Pakistan</option> <option>other</option>
</select> <br/><br/>
<input type="submit" value="register"/>
</form> </body> </html>
Register.java
import java.io.*; import java.sql.*;
import javax.servlet.http.*; import javax.servlet.ServletException;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Class.forName("oracle.jdbc.driver.OracleDriver");
//Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydb”);
// Connection con=DriverManager.getConnection("jdbc:mysql://localhost/TEST","root","password");
// DB_URL, USERNAME, PASSWORD
int i=ps.executeUpdate();
if(i>0) out.print("You are successfully registered...");
out.close();
}
Cookies in Servlets
// To generate cookie when server interacts with the client for first time.
String data = request.getParameter(“t1”);
Cookie cookie = new Cookie(“MyCookie”,data);
// MyCookie is the name of the cookie, and data is its value.
response.addCookie(cookie);
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();