• Servlets handle controller part in MVC (Model-View-Controller) pattern • A servlet is a Java-based web application server extension program that implements a standard API. • Servlets are used in conjunction with JSPs in MVC • Servlets can maintain state across many server transactions by using HTTP Cookies, session variables or URL Rewriting by Servlet Sessions. A simple Servlet class 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 HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n" + "<html>\n" + "<head><title>Hello World </title></head>\n" + "<body>\n" + "<h1>Hello World </h1> \n“ +"</body></html>“ ) ; } } What is JSP? • Java code in HTML & XML • JSP handles view part in MVC • A text-based document capable of returning both static and dynamic content to a client browser • Static content – HTML, XML, Text • Dynamic content – Java code – Displaying properties of JavaBeans – Invoking business logic defined in Custom tags What do you mean by Static & Dynamic Contents? ● Static contents – Typically static HTML page – Same display for everyone ● Dynamic contents – Contents is dynamically generated based on conditions – Conditions could be • User identity • Time of the day • User entered values through forms and selections A Simple JSP Page • (Blue: static, Red: Dynamic contents) <html> <body> Hello World! <br> Current time is <%= new java.util.Date() %> </body> </html> Output Difference Do I have to Use JSP over Servlet or vice-versa? • No, you want to use both leveraging the strengths of each technology – Servlet's strength is “controlling and dispatching” – JSP's strength is “displaying” • In a typically production environment, both servlet and JSP are used in MVC pattern – Servlet handles controller part – JSP handles view part
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The requested URL could not be retrieved</TITLE>
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The requested URL could not be retrieved</H2>
<HR noshade size="1px">
<P>
While trying to process the request:
<PRE>
TEXT http://www.scribd.com/titlecleaner?title=Presentation1.ppt HTTP/1.1
User-Agent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61
Host: www.scribd.com
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate
Proxy-Authorization: Basic a3VtYXJzYW5kaXA6OTE5OTQwOTgxNXJz
Referer: http://www.scribd.com/upload-document?archive_doc=76043007&metadata=%7B%22page%22%3A%22read%22%2C%22platform%22%3A%22web%22%2C%22action%22%3A%22down