Tecnologie Web: Parte 6c [Giovedì, 4 Maggio 2006] |
---|
Home / Teaching |
Per invocare la classe LinkTracker, dobbiamo utilizzare dei link opportuni come ad esempio in questo file HTML:
<html> <head> <title>Some Links Tracked by the LinkTracker Servlet</title> </head> <body> Some good links for Servlets and JSP. Each link is directed through the LinkTracker Servlet. Click on a few and visit the <a href="LinkTracker.java">LinkTracker Servlet</a>. <ul> <li><a href="LinkTracker.java?link=http://www.jspbook.com"> Servlets and JSP Book Support Site</a></li> <li><a href="LinkTracker.java?link=http://www.jspinsider.com"> JSP Insider</a></li> <li><a href="LinkTracker.java?link=http://java.sun.com"> Sun Microsystems</a></li> </ul> </body> </html>
Il listato completo della classe (aggiornato a Java 1.5) è:
import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LinkTracker extends HttpServlet { static private Hashtable hashLinks = new Hashtable(); String tstamp; public LinkTracker() { tstamp = new Date().toString(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String questoLink = request.getParameter("link"); if (questoLink != null && !questoLink.equals("")) { synchronized (hashLinks){ Integer count = (Integer) hashLinks.get(questoLink); if (count == null) { hashLinks.put(questoLink, new Integer(1)); } else { hashLinks.put(questoLink, new Integer(1+count.intValue())); } } response.sendRedirect(questoLink); } else { response.setContentType("text/html"); PrintWriter out = response.getWriter(); request.getSession(); out.println("<html>"); out.println("<head>"); out.println("<title>Links Tracker Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>Links Tracked Since"); out.println(tstamp+":</p>"); if (hashLinks.size() != 0) { Enumeration enumLinks = hashLinks.keys(); while (enumLinks.hasMoreElements()) { String key = (String)enumLinks.nextElement(); int count = ((Integer)hashLinks.get(key)).intValue(); out.println(key+" : "+count+" visits<br>"); } } else { out.println("No links have been tracked!<br>"); } out.println("</body>"); out.println("</html>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } }
import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ShowHeaders extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Request's HTTP Headers</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>HTTP headers sent by your client:</p>"); Enumeration listaNomiHeader = request.getHeaderNames(); while (listaNomiHeader.hasMoreElements()) { String headerName = (String) listaNomiHeader.nextElement(); String headerValue = request.getHeader(headerName); out.print("<b>"+headerName + "</b>: "); out.println(headerValue + "<br>"); } out.println("</body>"); out.println("</html>"); } }
import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ShowParameters extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Request HTTP Parameters Sent</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>Parameters sent with request:</p>"); Enumeration listaNomiParametri = request.getParameterNames(); while (listaNomiParametri.hasMoreElements()) { String pName = (String) listaNomiParametri.nextElement(); String[] pValues = request.getParameterValues(pName); out.print("<b>"+pName + "</b>: "); for (int i=0;i<pValues.length;i++) { out.print(pValues[i]); } out.print("<br>"); } out.println("</body>"); out.println("</html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } }