Java Reference
In-Depth Information
finally {
try {
if (out != null) out.close();
if (in != null) in.close();
if (hc != null) hc.close();
}
catch (IOException ioe) {}
}
}
}
Listing 10-3. A Simple Servlet That Responds to PostServlet
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class PostServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String message = "Received name: '" + name + "'";
response.setContentType("text/plain");
response.setContentLength(message.length());
PrintWriter out = response.getWriter();
out.println(message);
}
}
You'll need to specify the MIDlet property PostMIDlet-URL in order for this example to
work correctly. The URL http://65.215.221.148:8080/wj2/post can be used for testing.
Using Cookies for Session Tracking
HTTP is a stateless protocol, which means that each request and response pair is a separate
conversation. Sometimes, though, you want the server to remember who you are. This can be
done with a session . On the server side, a session is just a collection of information. When the
client sends an HTTP request to the server, it includes a session ID as part of the request. The
server can then look up the corresponding session and have some idea of the identity (or at
least the state) of the client.
The most common way to store a session ID on the client side is using HTTP cookies . A cookie
is just a little piece of data that is passed from the server to the client in an HTTP response. Most
web browsers automatically store cookies and will send them back to the appropriate server
when a new request is made.
Search WWH ::




Custom Search