Глава 1
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class CookieExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); // print out cookies Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; String name = c.getName(); String value = c.getValue(); out.println(name + " = " + value); } // set a cookie String name = request.getParameter("cookieName"); if (name != null && name.length() > 0) { String value = request.getParameter("cookieValue"); Cookie c = new Cookie(name, value); response.addCookie(c); } } }
Так выглядит страница cookies.html в браузере. Эта страница демонстрирует код сервлета. Запустить сервлет можно следующей командой: http://localhost:8080/servlets-examples/servlet/CookieExample Ниже на рисунке показана страница, которая образуется в результате работы этого сервлета.
Вот html-код этой страницы (то, что фактически сервер после обработки сервлета послал браузеру):
<html>
<body bgcolor="white">
<head>
<title>Cookies Example</title>
</head>
<body>
<a href="../cookies.html">
<img src="../images/code.gif" height=24 width=24 align=right border=0 alt="view code"></a>
<a href="../index.html">
<img src="../images/return.gif" height=24 width=24 align=right border=0 alt="return"></a>
<h3>Cookies Example</h3>
Your browser is sending the following cookies:<br>
Cookie Name: test<br> Cookie Value: 1<br><br>
Cookie Name: counter<br> Cookie Value: 7<br><br>
<P>
Create a cookie to send to your browser<br>
<form action="CookieExample" method=POST>
Name: <input type=text length=20 name=cookiename><br>
Value: <input type=text length=20 name=cookievalue><br>
<input type=submit></form>
</body>
</html>
Более точно код сервлета выглядит так:
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.HTMLFilter;
/**
* Example servlet showing request headers
*
* @author James Duncan Davidson <duncan@eng.sun.com>
*/
public class CookieExample extends HttpServlet {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
String title = rb.getString("cookies.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body>");
// relative links
// XXX
// making these absolute till we work out the
// addition of a PathInfo issue
out.println("<a href=\"../cookies.html\">");
out.println("<img src=\"../images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"../index.html\">");
out.println("<img src=\"../images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");
out.println("<h3>" + title + "</h3>");
Cookie[] cookies = request.getCookies();
if ((cookies != null) && (cookies.length > 0)) {
out.println(rb.getString("cookies.cookies") + "<br>");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
out.print("Cookie Name: " + HTMLFilter.filter(cookie.getName())
+ "<br>");
out.println(" Cookie Value: "
+ HTMLFilter.filter(cookie.getValue())
+ "<br><br>");
}
} else {
out.println(rb.getString("cookies.no-cookies"));
}
String cookieName = request.getParameter("cookiename");
String cookieValue = request.getParameter("cookievalue");
if (cookieName != null && cookieValue != null) {
Cookie cookie = new Cookie(cookieName, cookieValue);
response.addCookie(cookie);
out.println("<P>");
out.println(rb.getString("cookies.set") + "<br>");
out.print(rb.getString("cookies.name") + " "
+ HTMLFilter.filter(cookieName) + "<br>");
out.print(rb.getString("cookies.value") + " "
+ HTMLFilter.filter(cookieValue));
}
out.println("<P>");
out.println(rb.getString("cookies.make-cookie") + "<br>");
out.print("<form action=\"");
out.println("CookieExample\" method=POST>");
out.print(rb.getString("cookies.name") + " ");
out.println("<input type=text length=20 name=cookiename><br>");
out.print(rb.getString("cookies.value") + " ");
out.println("<input type=text length=20 name=cookievalue><br>");
out.println("<input type=submit></form>");
out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
Назад |
Начало урока |
Вверх |
Вперед
Содержание