Docsity

Servlet and Form Processing in Web Engineering, Slides of Web Design and Development

Partial preview of the text

Download Servlet and Form Processing in Web Engineering and more Slides Web Design and Development in PDF only on Docsity!

ICE 4203: Web Engineering

Lecture 3: Servlet

July – December 2020

Hasan Mahmud Department of Information and Communication Technology (ICT) Bangladesh University of Professionals (BUP)

Contents

• Introduction to servlets

• Servlets Vs. Applets

• Servlet lifecycle

• A simple servlet

  • Applet: downloadable Java code executed on a Browser’s JVM
    • Applet programs can only write to files if the applets are located on the host system of the managing Web page. An applet cannot write a file over a network.
    • An applet program is a self-contained block of running Java code that a Web browser launches and runs from a Web page. Applets are called using the HTML tag.
    • To display an applet on a Web page, use an HTML file containing a reference to your applet. In HTML, a simple applet tag looks like this:

Applets

• The EMBED tag

Applets

Servlet Lifecycle

destroy()

service(HttpServletRequest r,

HttpServletResponse p)

Called only the first time a servlet is

loaded into memory!

doGet()

doPost()

doXXX()

Used only when memory is freed

If the Servlet implements SingleThreadModel

there will be no multithreading

init()

Servlet Application Architecture

service()

  • This code is part of the class HttpServlet protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod (); if (method.equals ("GET")) { long ifModifiedSince; long lastModified; long now; ifModifiedSince = req.getDateHeader ("If-Modified-Since"); lastModified = getLastModified (req); maybeSetLastModified (resp, lastModified); if (ifModifiedSince == - 1 || lastModified == - 1 ) doGet (req, resp); else { now = System.currentTimeMillis (); if (now < ifModifiedSince || ifModifiedSince < lastModified) doGet (req, resp); else resp.sendError (HttpServletResponse.SC_NOT_MODIFIED); }

} else if (method.equals ("HEAD")) { long lastModified; lastModified = getLastModified (req); maybeSetLastModified (resp, lastModified); doHead (req, resp); } else if (method.equals ("POST")) { doPost (req, resp); } else if (method.equals ("PUT")) { doPut(req, resp); } else if (method.equals ("DELETE")) { doDelete(req, resp); } else if (method.equals ("OPTIONS")) { doOptions(req,resp); } else if (method.equals ("TRACE")) { doTrace(req,resp); } else { resp.sendError (HttpServletResponse.SC_NOT_IMPLEMENTED, "Method '" + method + "' is not defined in RFC 2068 "); }} service()

// set content type and other response header fields first response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println(""); out.println(title); out.println(""); out.println("" + title + ""); out.println("

This is output from SimpleServlet."); out.println("

"); out.close(); } } A taste of servlet programming..

  • Give to the user the possibility to send information to the

Web server

  • The FORM tag defines a form and has the following attributes:
    • ACTION identifies the processing engine
    • ENCTYPE specifies the MIME type used to pass data to the server (Es. Text/html)
  • FORM contains the sub-tag: several tags for collecting data
    • An INPUT tag must be of type SUBMIT for sending the data
    • An INPUT can be of tye RESET to cancel all the gathered data
  • See also: http://www.cs.tut.fi/~jkorpela/forms/ Forms (a quick overview)

Make your choice: Auto Bus Truck

Red Silver

Form – input type=“radio”

Make your choice: Auto Bus Truck

Form – input type=“checkbox” - select

  1. package coreservlets;
  2. import java.io.*;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import java.util.*;
  6. public class ShowParameters extends HttpServlet {
  7. public void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException, IOException {
  8. response.setContentType("text/html");
  9. PrintWriter out = response.getWriter();
  10. String title = "Reading All Request Parameters";
  11. out.println ("" +title+ “" +
  12. "\n" +
  13. "" + title + "\n" +
  14. "\n" +
  15. "\n" +
  16. "Parameter NameParameter Value(s)"); Example: ShowParameters
  1. Enumeration paramNames = request.getParameterNames();
  2. while(paramNames.hasMoreElements()) {
  3. String paramName = (String)paramNames.nextElement();
  4. out.print("" + paramName + "\n");
  5. String[] paramValues = request.getParameterValues(paramName);
  6. if (paramValues.length == 1 ) {
  7. String paramValue = paramValues[ 0 ];
  8. if (paramValue.length() == 0 ) out.println("No Value");
  9. else out.println(paramValue);
  10. } else {
  11. out.println("");
  12. for(int i= 0 ; i" +paramValues[i]); }
  13. out.println("");
  14. }
  15. }
  16. out.println("\n");
  17. } Example: ShowParameters…