Transcript powerpoint

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 doGet() method of your servlet is called,
otherwise the doPost() 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 HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
// Set the content type of the response.
resp.setContentType ("text/html");
// Create a PrintWriter to write the response.
java.io.PrintWriter out = new PrintWriter (resp.getOutputStream());
// The first part of the response.
out.println (""); out.println (""); out.println ("");
// The greeting.
out.println ("Yo, Hello World!");
// Last part. out.println (""); out.println (""); out.close();
// Screen I/O
System.out.println ("Inside servlet ... servlet complete"); }
public void doPost (HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException { doGet (req, resp); }
}
Hello World Notes
•
The servlet class libraries need to be imported, along with java.io.PrintWriter.
•
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.
•
There are two object parameters to each method, the "request" and the "respponse".
•
The HttpServletResponse instance has an OutputStream that is used to write directly
to the requesting browser.
•
To make writing easier, we wrap a PrintWriter around the OutputStream instance:
// Create a PrintWriter to write the response.
java.io.PrintWriter out = new PrintWriter (resp.getOutputStream());
We set the content-type (as required by the HTTP protocol):
// Set the content type of the response.
resp.setContentType ("text/html");
We write HTML to the output, e.g.,
out.println ("");
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");
•
•
•
•
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="param1"> 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 TestForm extends HttpServlet {
public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Set the content type of the response.
resp.setContentType ("text/html");
// Create a PrintWriter to write the response.
java.io.PrintWriter out = new PrintWriter (resp.getOutputStream());
// The first part of the response.
out.println (""); out.println (""); out.println ("");
// Now get the parameters and output them back.
out.println ("Request parameters: ");
Enumeration e = req.getParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String value = req.getParameter (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.println (""); out.close(); }
public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost (req,
resp); }
}
Notes for Extracting Parameters
• We need to import java.util.Enumeration. Whatever parameters
were provided are all in the HttpServletRequest instance. We
can list these parameters by getting an Enumeration instance
from the request by calling getParameterNames():
Enumeration e = req.getParameterNames();
• 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 param1 below.
Enter a string: <input type="text" name="param1">
• Now, if we want to retrieve the actual string typed in by the user,
we use that name in getParameter():
String whatTheUserTyped = req.getParameter
("param1");