Servlet Fundamentals File - e

Download Report

Transcript Servlet Fundamentals File - e

Net-centric Computing
Java Servlets
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
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
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
History
CGI
CGI
(in C)
(java,
C++)
Template
(ASP, PHP)
complexity
Speed, Security
Servlet
JSP
How it works for Java Servlets
Web server app is
commonly Apache
Web container app is Servlets are run by
Tomcat
Tomcat
SE-2840 Dr. Mark L. Hornick
6
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
Servlets Life Cycle
Web Container
(Tomcat)
Your servlet class
This is where the
servlet spends most of
its life
CS-4220 Dr. Mark L. Hornick
8
Steps to run servlet on xampp


Set environment variables

Enter the paths ,;C:\xampp\tomcat\lib\servletapi.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\<?>)
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>
Servlet Example 1

http://localhost:8080/servlet/MyServlet
HelloWorld
import
import
import
public
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
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("<body>");
out.println("<h1>Hello CS764!</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
<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>
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
import
import
import
import
java.io.*;
java.util.*;
javax.servlet.*;
javax.servlet.http.*;
Ass2Servlet
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>");
}
}
Servlets vs. Java Applications

Servlets do not have a main()



Servlet interaction with end user is indirect
via request/response object APIs


The main() is in the server
Entry point to servlet code is via call to a
method (doGet() in the example)
Actual HTTP request/response processing is
handled by the server
Primary servlet output is typically HTML