Download presentation
Presentation is loading. Please wait.
1
Net-centric Computing
Java Servlets
2
Lecture Outline Why Servlet What is Servlet History How Servlet Works
What the container (e.g. Tomcat) does Setting up to run servlet Simple Examples
3
Why Servlet The need for servlet Support creation of Dynamic web pages
Reduce the overhead on the server and network To take care of processing data on the Web server
4
What is Servlet Servlet:
Java programs that can be deployed on a Java enabled Web server A Java class that can respond to HTTP requests Runs in a special web server, the servlet container
5
History CGI CGI Servlet JSP Template (ASP, PHP) (java, C++) (in C)
complexity Speed, Security Servlet JSP
6
How it works for Java Servlets
Web server app is commonly Apache Web container app is Tomcat Servlets are run by Tomcat SE-2840 Dr. Mark L. Hornick
7
What the container does
Communication Creates server-side sockets Listens for client connections Determines client HTTP request type and “decodes” HTTP headers Servlet Lifecycle management Figures out which Servlet should be used to process a specific request Handles Servlet class loading Handles Servlet instantiation/construction Handles Servlet initialization Servlet execution support Launches/manages threads that service each incoming request Handles Servlet service() method invocation Creates and passes Request and Response objects to the Servlet Supports Security Supports JSP
8
Servlets Life Cycle CS-4220 Dr. Mark L. Hornick Web Container (Tomcat)
This is where the servlet spends most of its life Web Container (Tomcat) Your servlet class CS-4220 Dr. Mark L. Hornick
9
Steps to run servlet on xampp
Set environment variables Enter the paths ,;C:\xampp\tomcat\lib\servlet-api.jar and lib folder of Java installation (e.g. C:\Java\jdk1.8.0_31\lib) in the CLASSPATH system variable Enter the path for bin folder of Java installation (e.g. C:\Java\jdk1.8.0_31\bin) in the PATH system variable Classes must be in WEB-INF/classes folder (inside c:\tomcat\webapps\<?>)
10
Steps to run servlet on xampp
Configure the WEB-INF/web.xml file as follows (YourFileName.class): <servlet> <servlet-name>YourFileName</servlet-name> <servlet-class>YourFileName</servlet-class> </servlet> <servlet-mapping> <servlet-name>YourFileName</servlet-name> <url-pattern>/YourFileName</url-pattern> </servlet-mapping>
11
Servlet Example 1
12
HelloWorld import java.io.*; import javax.servlet.*;
import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello CS764!</title>"); out.println("</head>"); out.println("<h1>Hello CS764!</h1>"); out.println("</body>"); out.println("</html>"); out.close(); }
13
<html><head></head> <body> <a href="
<html><head></head> <body> <a href="../servlet/HelloWorld"> <h1>Execute HelloWorld Servlet</h1> </a> </body> </html> <html> <head> <title>Hello CS764!</title></head> <body> <h1>Hello CS764!</h1> </body> </html>
14
Example 3: Get Stock Price
Ass2.html <html><head></head> <body> <form action="../servlet/Ass2Servlet" method=POST> <h2>Stock Symbol name: <input type=text name="stockSymbol"></h2><br> <input type="submit" value = "get price"> </form> </body></html> Client Side
15
Ass2Servlet public class Ass2Servlet extends HttpServlet {
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Ass2Servlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); String stockSymb = request.getParameter("stockSymbol"); StockGrabber sg = new StockGrabber(); sg.setStockSymbol(stockSymb); // Set the stock symbol as “input” String stockPrice = sg.getPrice();// Get the price of stock System.out.println("After StockGrabber.getPrice --"+stockPrice);// Debug out.println("<html><head></head><body><br><br>"); out.println(stockSymb + " -- " + stockPrice); out.println("<hr>"); out.println("<form action=\"../servlet/Ass2Servlet\" method=POST>"); out.println("<h3>Stock Symbol name: <input type=text name=\"stockSymbol\"></h3>"); out.println("<input type=submit value=\"get price\">"); out.println("</form>"); out.println("</body></html>"); }
16
Servlets vs. Java Applications
Servlets do not have a main() The main() is in the server Entry point to servlet code is via call to a method (doGet() in the example) Servlet interaction with end user is indirect via request/response object APIs Actual HTTP request/response processing is handled by the server Primary servlet output is typically HTML
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.