02 up close with servlets

1,665 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,665
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
75
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

02 up close with servlets

  1. 1. 02-Up Close with Servlets<br />DhrubojyotiKayal<br />
  2. 2. Servlet responses<br />Request Response Sequence<br />Servlet URLs<br />Servlet life cycle<br />Agenda<br />
  3. 3. Exercise<br />
  4. 4. Servlet with simple text response<br />public void doGet(HttpServletRequest request,<br />HttpServletResponse response)<br /> throws ServletException, IOException {<br />PrintWriter out = response.getWriter();<br />out.println("Hello World");<br />}<br />Servlet Responses<br />
  5. 5. Servlet with simple text response<br />public void doGet(HttpServletRequestrequest,HttpServletResponse response)<br />throws ServletException, IOException {<br />response.setContentType("text/html");<br />PrintWriter out = response.getWriter();<br />out.println<br /> ("<!DOCTYPE html> " +<br /> "<html> " +<br /> "<head><title>A Test Servlet</title></head> " +<br /> "<body bgcolor="#fdf5e6"> " +<br /> "<h1>Test</h1> " +<br /> "<p>Simple servlet for testing.</p> " +<br /> "</body></html>");<br />}<br />Other forms – XML, Excel, Word, PDF ….<br />Servlet Responses<br />
  6. 6. Request Response Sequence<br />
  7. 7. Java code<br />com.empweb.web – package<br />public class SimpleServlet extends HttpServlet<br />web.xml<br />register<br /><servlet><br /> <servlet-name>SimpleServlet</servlet-name><br /> <servlet-class>com.empweb.web.SimpleServlet</servlet-class><br /> </servlet><br />Map url<br /><servlet-mapping><br /><servlet-name>SimpleServlet</servlet-name><br /><url-pattern>/simpleservlet</url-pattern><br /></servlet-mapping><br />Final URL – http://domain/context/servlet-name<br />Servlet URLs<br />
  8. 8. init ()<br />Executed once when the servlet is first loaded.<br />Not called for each request.<br />service ()<br />Called in a new thread by server for each request.<br />Dispatches to doGet, doPost, etc.<br />Do not override this method!<br />doGet () / doPost () / doXXX()<br />Handles GET, POST, etc. requests.<br />Override these to provide desired behavior.<br />destroy()<br />Called when server deletes servlet instance.<br />Not called after each request.<br />Servlet Lifecycle<br />
  9. 9. Servlet Lifecycle<br />
  10. 10. <servlet> <br /><servlet-name>MyServletName</servlet-name> <br /><servlet-class>com.mycompany.MyServlet</servlet-class> <br /><init-param> <param-name> param1 </param-name> <param-value> value1 </param-value> </init-param> <init-param> <param-name> param2 </param-name> <param-value> value2 </param-value> </init-param> ... <br /></servlet> <br />Servlet Initialization<br />
  11. 11. @Override<br />public void init() throws ServletException {<br /> String value = getServletConfig().getInitParameter("param1"); <br />System.out.println(value);<br />}<br />Initial Parameter and Values<br />
  12. 12. Create servlet instances before a request comes in<br />Order of creating servlet instances<br />Load on Startup<br />
  13. 13. Q&A<br />

×