Servlets in Java
Servlets in Java
Servlets are the Java programs that run on the Java-enabled web server or application
server. They are used to handle the request obtained from the webserver, process the request,
produce the response, then send a response back to the webserver.
Properties of Servlets are as follows:
Servlets work on the server-side.
Servlets are capable of handling complex requests obtained from the webserver.
Servlet Architecture can be depicted from the image itself as provided below as follows:
There are three states of a servlet: new, ready and end. The servlet is in new state if servlet
instance is created. After invoking the init() method, Servlet comes in the ready state. In the
ready state, servlet performs all the tasks. When the web container invokes the destroy()
method, it shifts to the end state.
The classloader is responsible to load the servlet class. The servlet class is loaded when
the first request for the servlet is received by the web container.
2) Servlet instance is created
The web container creates the instance of a servlet after loading the servlet class. The
servlet instance is created only once in the servlet life cycle.
The web container calls the service method each time when request for the servlet is received.
If servlet is not initialized, it follows the first three steps as described above then calls the
service method. If servlet is initialized, it calls the service method. Notice that servlet is
initialized only once. The syntax of the service method of the Servlet interface is given below:
The web container calls the destroy method before removing the servlet instance from the
service. It gives the servlet an opportunity to clean up any resource for example memory,
thread etc. The syntax of the destroy method of the Servlet interface is given below:
6 steps to create a servlet example. These steps are required for all the servers.
To create a servlet the class must extend the HttpServlet class and override at least one of its
methods (doGet, doPost, doDelete, doPut).
Methods of HttpServlet Class
1. doGet() Method
Syntax:
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws
ServletException,IOException
Parameters:
request – an HttpServletRequest object that contains the request the client has made
of the servlet.
response – an HttpServletResponse object that contains the response the servlet sends
to the client.
Exceptions:
IOException – if an input or output error is detected when the servlet handles the
GET request.
ServletException – Use to handle the GET request.
2. doPost() Method
3. doHead() Method
4. doPut() Method
5. doDelete() Method
Syntax:
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws
ServletException,IOException
Parameters:
request – an HttpServletRequest object that contains the request the client has
made of the servlet.
response – an HttpServletResponse object that contains the response the
servlet sends to the client.
Exceptions:
IOException – if an input or output error is detected when the servlet handles
the DELETErequest.
ServletException – Use to handle the DELETE request.
6. service() Method
This method receives standard HTTP requests from the public service method and
dispatches them to the doXXX methods defined in this class.
Syntax:
protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException,IOException
Parameters:
request – an HttpServletRequest object that contains the request the client has
made of the servlet.
response – an HttpServletResponse object that contains the response the servlet
sends to the client.
Exceptions:
IOException – if an input or output error is detected when the servlet handles the
HTTP request.
ServletException – Use to handle the HTTP request.
https://www.javatpoint.com/creating-servlet-in-myeclipse-ide
https://www.studytonight.com/servlet/creating-servlet-in-eclipse.php
You need to follow the following steps to create the servlet in the Eclipse IDE. The steps a
follows:
The default port of myeclipse tomcat is 8080, if you have installed oracle on your
system, the port no. will conflict so let's first change the port number of myeclipse
tomcat server. For changing the port number click on the start server icon at the left
hand side of browser icon -> myeclipse tomcat -> Configure server connector ->
change the port number as 8888 in place of 8080 -> apply -> ok.
Now change the port number as 8888 in place of 8080 -> apply -> ok.
Now port number have been changed. For starting the server Right click on your project
-> Run As -> MyEclipse server application.
As you can see that default page of your project is open, write your name -> go.
Hello.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Hello extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("user");
out.print("Hello "+name);
out.close();
}
}
MyHtml.html
<form action="servlet/Hello">
Name:<input type="text" name="user"/>
<input type="submit" value="go"/>
</form>
LoginServlet.Java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class LoginServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String name=req.getParameter("name");
System.out.println("Name is "+name);
String password=req.getParameter("password");
System.out.println("Password is "+password);
pw.println("Welcome :-"+name +",with password:-"+ password);
}
}
web.xml
<web-app>
<servlet>
<servlet-name>anand</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anand</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
3. Form submission :
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>User Details</title>
</head>
<body >
<h3>Fill in the Form</h3>
<tr>
<td>Full Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="gender" value="male"
/>Male
<input type="radio" name="gender"
value="female" />Female</td>
</tr>
<tr>
<td>Select Programming Languages to learn:</td>
<td><input type="checkbox" name="language"
value="java" />Java
<input type="checkbox" name="language"
value="python" />Python
<input type="checkbox" name="language"
value="sql" />SQL
<input type="checkbox" name="language"
value="php" />PHP</td>
</tr>
<tr>
<td>Select Course duration:</td>
<td><select name="duration">
<option value="3months">3 Months</option>
<option value="6months">6 Months</option>
<option value="9months">9
Months</option></select></td>
</tr>
<tr>
<td>Anything else you want to share:</td>
<td><textarea rows="5" cols="40"
name="comment"></textarea></td>
</tr>
</table>
</form>
</body>
</html>
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;
out.print("</body></html>");