Java Servlets Basics What is Java Servlets Javas
Java Servlets
Basics • What is Java Servlets? – Java’s solution to CGI programs. • Wrong way to use Java as CGI – Write stand-alone Java program – Invoke it via new CGI shell process. • Right way to use Java as CGI – Use servlets and a servlet-compatible server.
How it works? · Browser makes a servlet request to webserver. · Webserver recognizes the URL as a servlet URL. (e. g. , http: //www. obvious. com/servlets/blah) · Webserver loads servlet if not already loaded. · Servlet executes and delivers output to webserver (HTML output). · Webserver delivers output to Browser.
What you need to write servlets? · Either JDK 1. 1 and JSDK (Java Servlet Development Kit). · JDK 1. 2 · A servlet-compatible webserver.
GET & POST · When Form data is sent by the browser, it can be sent in one of two ways: (1) using the GET method and (2) using the POST method. · • In the GET method, the form data (parameters) is appended to the URL, as in: http: //www. yahoo. com/search? music Here, the text field contains music. · In the POST method, the browser simply sends the form data directly. · When you create an HTML form, you decide whether you want to use GET or POST. · When you use GET, the do. Get() method of your servlet is called, otherwise the do. Post() method is called. · The standard practice is to use POST, unless you need to use GET. · You need to use GET when you want a link to invoke a CGI program (servlet).
Your first servlet -- Hello World import javax. servlet. *; import javax. servlet. http. *; import java. io. *; public class helloworld extends Http. Servlet { public void do. Get (Http. Servlet. Request req, Http. Servlet. Response resp) throws Servlet. Exception, IOException { // Set the content type of the response. resp. set. Content. Type ("text/html"); // Create a Print. Writer to write the response. java. io. Print. Writer out = new Print. Writer (resp. get. Output. Stream()); // The first part of the response. out. println (""); // The greeting. out. println ("Yo, Hello World!"); // Last part. out. println (""); out. close(); // Screen I/O System. out. println ("Inside servlet. . . servlet complete"); } public void do. Post (Http. Servlet. Request req, Http. Servlet. Response resp) throws Servlet. Exception, IOException { do. Get (req, resp); } }
Hello World Notes • • • The servlet class libraries need to be imported, along with java. io. Print. Writer. • • There are two object parameters to each method, the "request" and the "respponse". • To make writing easier, we wrap a Print. Writer around the Output. Stream instance: // Create a Print. Writer to write the response. java. io. Print. Writer out = new Print. Writer (resp. get. Output. Stream()); We set the content-type (as required by the HTTP protocol): // Set the content type of the response. resp. set. Content. Type ("text/html"); We write HTML to the output, e. g. , out. println (""); Don't forget to close the output stream: out. close(); Optionally, for debugging, we can also write to the local screen where the webserver is running: System. out. println ("Inside servlet. . . servlet complete"); • • There are two methods, one for each type of request: GET or POST. You can decide not to "really" implement one method and instead have it call the other. The Http. Servlet. Response instance has an Output. Stream that is used to write directly to the requesting browser.
Extracting Parameters in a Servlet • Idea -- create an HTML Form, and have a servlet pick up the Form data entered by the user. • HTML Form <html> <head><title>Test Post</title></head> <body> <form action="http: //localhost: 8502/servlets/Testform" method="post"> Enter a string: <input type="text" name="param 1"> And then press "Go": <input type="submit" value="Go"> </form> </body> </html>
Extracting Parameters in a Servlet • Next the Java Servlet import javax. servlet. *; import javax. servlet. http. *; import java. io. *; import java. util. *; public class Test. Form extends Http. Servlet { public void do. Post (Http. Servlet. Request req, Http. Servlet. Response resp) throws Servlet. Exception, IOException { // Set the content type of the response. resp. set. Content. Type ("text/html"); // Create a Print. Writer to write the response. java. io. Print. Writer out = new Print. Writer (resp. get. Output. Stream()); // The first part of the response. out. println (""); // Now get the parameters and output them back. out. println ("Request parameters: "); Enumeration e = req. get. Parameter. Names(); while (e. has. More. Elements()) { String name = (String) e. next. Element(); String value = req. get. Parameter (name); if (value != null) out. println (" name=[" + name + "] value=[" + value + "]"); else out. println (" name=[" + name + "] did not have a value"); } // Last part. out. println (""); out. close(); } public void do. Get (Http. Servlet. Request req, Http. Servlet. Response resp) throws Servlet. Exception, IOException { do. Post (req, resp); } }
Notes for Extracting Parameters • We need to import java. util. Enumeration. Whatever parameters were provided are all in the Http. Servlet. Request instance. We can list these parameters by getting an Enumeration instance from the request by calling get. Parameter. Names(): Enumeration e = req. get. Parameter. Names(); • The "name" of a parameter is really the string in the name attribute of the tag for the particular Form element. For example, the name string is param 1 below. Enter a string: <input type="text" name="param 1"> • Now, if we want to retrieve the actual string typed in by the user, we use that name in get. Parameter(): String what. The. User. Typed = req. get. Parameter ("param 1");
- Slides: 10