⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 counterservlet.java

📁 关于Ultraseek的一些用法,刚初学,所以都是比较简单
💻 JAVA
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- *  $RCSFile$ $Revision: 1.12 $ $Date: 2006/02/06 21:26:47 $ *  Copyright (c) 2002-2004 Autonomy Corp.  All Rights Reserved. */import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;/** * Example of a click counting Servlet, for use with * <code>CounterSearchable</code>. * * The <code>CounterSearchable</code> rewrites  * the URL of each result to redirect through this Servlet which * logs which hit was selected. * <p> * This Servlet should be installed so that it is accessed * via the URL specified in the <code>setCounterLocation()</code> * method of the <code>CounterSearchable</code>. * <p> * If this Servlet is installed as * <tt>http://search.corp.com/count.html</tt> * Then <code>CounterSearchable</code> would rewrite the search * result URL <tt>http://www.corp.com/<tt> as follows: * <tt>http://search.corp.com/count.html?q=home&n=4&url=http://www.corp.com/</tt> * Indicating that for the query "home", the 4th result was selected. * The Servlet should redirect to the desired page <tt>http://www.corp.com/</tt>. * @serial exclude *  */public class CounterServlet  extends HttpServlet{  public void doGet(HttpServletRequest req, HttpServletResponse resp)       throws ServletException, IOException  {    String query = null;    String url = null;    int num = -1;    String id = "none";         // optional argument    Enumeration enumeration = req.getParameterNames();    while (enumeration.hasMoreElements()) {      String name = (String)enumeration.nextElement();      String[] values = req.getParameterValues(name);      if (name.equals("q")) {        query = decode(values[0]);      } else if (name.equals("n")) {        num = parseInt(decode(values[0]), -1);      } else if (name.equals("url")) {        url = decode(values[0]);      } else if (name.equals("id")) {        id = decode(values[0]);      }    }    // Send the redirect before we log this, so the user doesn't    // have to wait.    if (url != null) {      resp.sendRedirect(url);    } else {      error(req, resp);    }    if (url == null) {      log("error: missing url parameter " +          req.getRequestURI() + req.getQueryString());    } else if (query == null) {      log("error: missing q parameter " +          req.getRequestURI() + req.getQueryString());    } else if (num == -1) {      log("error: missing n parameter " +          req.getRequestURI() + req.getQueryString());    } else {      // Everything OK, log it.      log(id + " " +url + " " + num + " " + query);    }  }  private void error(HttpServletRequest req, HttpServletResponse resp)    throws IOException  {    // Buffer output to set Content-length and support 1.1 persistent conn.    ByteArrayOutputStream bytes = new ByteArrayOutputStream(4096);    PrintWriter out = new PrintWriter(bytes,true);    out.println("<html><head>");    out.println("<title>Unable to redirect</title>");    out.println("</head><body>");    out.println("Unable to redirect, no URL provided as parameter.");    out.println("<p>");    out.println("Please return to the previous page with your browser's");    out.println("<i>Back</i> button.");    out.println("</body></html>");    resp.setStatus(HttpServletResponse.SC_OK);    resp.setContentType("text/html");    resp.setContentLength(bytes.size());    bytes.writeTo(resp.getOutputStream());  }  private static String decode(String value)     throws UnsupportedEncodingException {    if (value==null) return value;    byte[] bytes = value.getBytes();    return new String(bytes, "UTF-8");  }  private static int parseInt(String str, int def) {    try {      return Integer.parseInt( str );    } catch (Exception e) {      return def;    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -