Docsity

Servlets: Development, Structure, and Deployment, Lecture notes of Network Design

Partial preview of the text

Download Servlets: Development, Structure, and Deployment and more Lecture notes Network Design in PDF only on Docsity!

Server Side Development

Writing Servlets

of 99 2

  • Create a servletclass
    • extend HttpServlet
  • Implement the doGet() or doPost() method
    • Both methods accept two parameters
      • HttpServletRequest
      • HttpServletResponse
    • Obtain parameters from HttpServletRequest Interface

using

  • getParameter(String name)
  • Obtain the writer from the response object
  • Process input data and generate output (in html form) and

write to the writer

  • Close the writer Servlets - Writing a Servlet

218000479 /

HTTPGetServlet.html

HTTPPostServlet.html

WEB-INF/

web.xml

classes/examples

HTTPGetServlet.class

HTTPPostServlet.class

lib/

src/

HTTPGetServlet .java

HTTPPostServlet.java

Servlets: Structure of our Web Application

Handling HTTP GET Requests

  • HTTP GET requests
    • Usually gets content of specified URL
      • Usually HTML document (web page)
  • Example servlet
    • Handles HTTP GET requests
    • User clicks Get Page button in HTML document
      • GET request sent to servlet HTTPGetServlet
    • Servlet dynamically creates HTML document displaying "Welcome to

Servlets!"

Handling HTTP GET Requests

  • setContentType
    • Specify content
    • text/html for HTML documents
  • getWriter
    • Returns PrintWriter object, can send text to client
    • getOutputStream to send binary data (returns ServletOutputStream object)

14 response.setContentType( "text/html" ); // content type

12 PrintWriter output;

15 output = response.getWriter(); // get writer

Handling HTTP GET Requests

  • Lines 19-23 create HTML document
    • println sends response to client
    • close terminates output stream
      • Flushes buffer, sends info to client

19 buf.append( "\n" );

20 buf.append( "A Simple Servlet Example\n" );

21 buf.append( "\n" );

22 buf.append( "Welcome to Servlets!\n" );

23 buf.append( "" );

24 output.println( buf.toString() );

25 output.close(); // close PrintWriter stream

Handling HTTP GET Requests

  • Port number
    • Where server waits for client (handshake point)
    • Client must specify proper port number
      • Integers 1 - 65535, 1024 and below usually reserved
    • Well-known port numbers
      • Web servers - port 80 default
      • JSDK/Apache Tomcat Webserver- port 8080
        • Change in conf/server.xml

Handling HTTP GET Requests

  • HTML documents
    • Comments: ****
    • Tags: ** ... **
      • ** ... ** tags enclose document
      • ** ... ** - enclose header
        • Includes ** Title ** tags
        • Sets title of document

1

2

3

4

5 Servlet HTTP GET Example

6

7

Handling HTTP GET Requests

  • ACTION
    • localhost - your computer
    • :8080 - port
    • / 218000479 - directory
  • GUI component
    • INPUT element
    • TYPE - "submit" (button)
    • VALUE - label
    • When pressed, performs ACTION
    • If parameters passed, separated by? in URL

10 ACTION="http://localhost:8080/ 218000479 /HTTPGetServlet"

14

  1. import 1.1 extends HttpServlet
  2. doGet 2.1 setContentType 2.2 getWriter 2.3 println 1 // Fig. 19.5: HTTPGetServlet.java 2 // Creating and sending a page to the client 3 import javax.servlet.; 4 import javax.servlet.http.; 5 import java.io.*; 6 7 public class HTTPGetServlet extends HttpServlet { 8 public void doGet( HttpServletRequest request, 9 HttpServletResponse response ) 10 throws ServletException, IOException 11 { 12 PrintWriter output; 13 14 response.setContentType( "text/html" ); // content type 15 output = response.getWriter(); // get writer 16 17 // create and send HTML page to client 18 StringBuffer buf = new StringBuffer(); 19 buf.append( "\n" ); 20 buf.append( "A Simple Servlet Example\n" ); 21 buf.append( "\n" ); 22 buf.append( "Welcome to Servlets!\n" ); 23 buf.append( "" ); 24 output.println( buf.toString() ); 25 output.close(); // close PrintWriter stream 26 } 27 } Import necessary classes and inherit methods from HttpServlet. Create PrintWriter object. Create HTML file and send to client.

Program Output

Handling HTTP POST Requests

  • HTTP POST
    • Used to post data to server-side form handler
    • Both GET and POST can supply parameters
  • Example servlet
    • Survey
      • Store results in file on server
    • User selects radio button, presses Submit
      • Browser sends POST request to servlet
    • Servlet updates responses
      • Displays cumulative results

Handling HTTP POST Requests

  • Open survey.txt , load animals array
  • Method getParameter( name )
    • Returns value of parameter as a string
  • Content type

18 File f = new File( "survey.txt" );

23 ObjectInputStream input = new ObjectInputStream(

24 new FileInputStream( f ) );

26 animals = (int []) input.readObject();

40 String value =

41 request.getParameter( "animal" );

64 response.setContentType( "text/html" ); // content type

Handling HTTP POST Requests

  • Return HTML document as before
  • **** tag
    • Preformatted text, fixed-width
  • tag - line break

67 StringBuffer buf = new StringBuffer();

68 buf.append( "\n" );

69 buf.append( "Thank you!\n" );

70 buf.append( "Thank you for participating.\n" );

71 buf.append( "Results:\n" );

73 DecimalFormat twoDigits = new DecimalFormat( "#0.00" );

74 for ( int i = 0; i < percentages.length; ++i ) {

75 buf.append( "" );

76 buf.append( animalNames[ i ] );

88 responseOutput.println( buf.toString() );