Module 3
Back End Development
What is Java Servlet?
● 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 web server,
process the request, produce the response, and then send a response
back to the web server.
● Properties of Java Servlet
○ Servlets work on the server side.
○ Servlets are capable of handling complex requests obtained from the
web server.
Java Servlets Architecture
● The Clients send the request to the Web
Server.
● The Web Server receives the request.
● The Web Server passes the request to the
corresponding servlet.
● The Servlet processes the request and
generates the response in the form of
output.
● The Servlet sends the response back to the
webserver.
● The Web Server sends the response back to
the client and the client browser displays it
on the screen.
Life Cycle of Java Servlet
GET Method
● The GET method sends the encoded user information appended to the
page request. The page and the encoded information are separated by
the ? (question mark) symbol as follows-
[Link] = value1&key2 = value2
● The GET method is the default method to pass information from browser
to web server and it produces a long string that appears in your browser's
Location:box.
● Never use the GET method if you have password or other sensitive
information to pass to the server. The GET method has size limitation:
only 1024 characters can be used in a request string.−
Example for GET method
<form action="/action_page.php" method="get">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
TRY IT YOURSELF
Output
POST Method
● A generally more reliable method of passing information to a backend
program is the POST method.
● This packages the information in exactly the same way as GET method,
but instead of sending it as a text string after a ? (question mark) in the
URL it sends it as a separate message.
● This message comes to the backend program in the form of the standard
input which you can parse and use for your processing. Servlet handles
this type of requests using doPost() method.
Example for POST Method
<form action="/action_page.php" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
TRY IT YOURSELF
Output
Running Servlet Programs
1. Install Java
2. Install Tomcat
3. Create Dir Structure
4. Source Code
5. Compile
6. Deployment Descriptor
7. Run Tomcat
8. Call in Web Browser
Installation of Java
1. Java link
Installation of Tomcat
Tomcat Link
Create Directory Structure C:\
1. Follow path from diagram Program
2. Create myApp folder inside webapps Files
3. Create src folder and WEB-INF folder
Apache Software
inside myApp
Foundation
4. Create classes folder inside WEB-INF
folder Tomcat 9.0
webapps
myApp
Write source code save in src folder
import [Link].*;
import [Link].*;
import [Link].*;
public class TestServlet extends HttpServlet
public void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
PrintWriter out=[Link]();
[Link]("Welcome to my first servlet program");
}
Compile
1. Open command prompt
2. Open source code(java file) location
3. Set path of jdk C:\Program Files\Apache Software Foundation\Tomcat
9.0\webapps\myApp\src>set path=C:\Program Files\Java\jdk-22\bin
4. set classpath=C:\Program Files\Apache Software Foundation\Tomcat
9.0\lib\[Link]
5. C:\Program Files\Apache Software Foundation\Tomcat
9.0\webapps\myApp\src>javac -d ../WEB-INF/classes [Link]
Deployment Descriptor
Create [Link] file in myApp/WEB-INF
Code:
<web-app>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
Run tomcat
Services
Stop and start again
Call in Web Browser
localhost:8080/myApp/Test