Showing posts with label develop. Show all posts
Showing posts with label develop. Show all posts

Wednesday, December 22, 2010

诡异的jsp编码

      关于jsp,sevlet,tomcat中中文乱码的问题,网上一搜一箩筐,不过有很多的解决方法太过于狭窄,很多用gb2312的方式来解决,结果显然会导致,假如不是中文,俄语,日语,还有其他乱七八糟的语言可能就会是乱码了,所以解决乱码问题,首先要确定的是在每一个地方,使用的encode都应该是UTF-8。
      确定了编码之后,那么就着手设置哪些地方为utf-8的编码

1.jsp页面中,有三处
  • <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
  • <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
2.tomcat config/server.xml中,解决get请求中的乱码问题
          <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               useBodyEncodingForURI="true"
               URIEncoding="
UTF-8" />

3.项目中的WebContent/WEB-INF/web.xml中,添加过滤器(过滤器代码可以随便写个简单的即可),解决post请求中的乱码问题
<filter>
        <filter-name>encoding</filter-name>
        <filter-class>filter.FilterEncoding</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
            <filter-name>encoding</filter-name>
         <url-pattern>/*</url-pattern>
   </filter-mapping>

这样一来,直接部署在tomcat目录下的项目就可以解决乱码的问题,但是,我是在eclipse下面开发的项目,这个时候依然存在乱码问题,思索良久,找到了原因,因为eclipse下的动态项目运行web 应用的时候,并不是直接应用tomcat的配置,而是使用一个server的项目中的配置,如图:

这里面的配置影响了eclipse中的web项目的编码,因此需要更改此地的配置,然后我将其中的server.xml参考tomcat中的修改之后,并没有解决乱码问题(可能是我当时没有刷新),后来的解决办法是,删除这个server,然后重新添加server,目的是得到最新的tomcat的配置,这样,乱码问题即不复存在。



Tuesday, May 25, 2010

PostgreSQL安装使用 and 从MusicBrainz导入数据

    因为研究的需要,需要使用MusicBrainz的数据,它提供的是一个面向对象的数据库,而我则需要RDF的数据,不过它提供了一个教程,虽然很晦涩,而且分布开了。首先安装PostgreSQl,这个几行命令搞定,然后开始使用。PostgreSQL默认帐号是postgres,没有密码,可以设置。最基本的命令
=> psql => It is a terminal-based front-end to PostgreSQL.
=> CREATEUSER - Adds a new user to a PostgreSQL database cluster.
=> CREATEDB - create a new database
psql database,为选择相应的数据库
psql -U musicbrainz_user musicbrainz_db 为进入用户名musicbrainz_user的musicbrainz_db数据库
createdb -O musicbrainz_user musicbrainz_db 为usermusicbrainz_user 创建musicbrainz_db数据库
    这些命令可以man一下就理解了,可怜我一开始忘了help,搞了半天才搞懂,血的教训,血的教训
文件配置:
    其中还有非常重要的两个配置文件可以配置是否能远程访问访问,以及权限设置,在/etc/postgres/postgres.conf和pg_hba.conf
远程访问:
postgresql.conf中的listen_address改为* ,

pg_hba.conf
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
host all all 0.0.0.0/0 md5

其他:
pg_hba.conf中注意是下面这几个的设置,具体可参加http://developer.postgresql.org/pgdocs/postgres/auth-pg-hba-conf.html
# Database administrative login by UNIX sockets
local   all      all    trust
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
# "local" is for Unix domain socket connections only
local   all         all                 md5
# IPv4 local connections:
host    all         all         0.0.0.0/0         md5
# IPv6 local connections:
host    all         all         ::1/128               md5
~                                        

搞好了这些,然后我就follow了MusicBrainz的method,终于完成
参考文献:   
http://defindit.com/readme_files/postgres_utilities.html                      
http://developer.postgresql.org/pgdocs/postgres/index.html

Monday, May 10, 2010

[develop]Nutch 初体验 爬行企业内部网

转自我的javaeye blog:http://xusulong.javaeye.com/blog/663411

前些日子琢磨着想搭建一个搜索引擎,自己写成本有点高,虽然以前写过爬虫,但是索引排序估计要烦得多

nutch   是一个开源的、Java 实现的搜索引擎。它提供了我们运行自己的搜索引擎所需的全部工具。是一个应用程序,可以以 Lucene 为基础实现搜索引擎应用。

选定nutch之后,开始着手学习使用nutch,英文水平还不够,只能看看nutch的简单的tutorial,但是真正当教程,我还是选择了中 文,可以让第一个搜索跑起来之后再选择学习英文的文档,以便更深的理解。

我选择的教程是 nutch入门学习 

准备工作:

我的系统是Ubuntu 9.10,java -version 1.6.0_20-b02,nutch 1.0,以及tomcat 6.0.26

  1. jdk和tomcat一般大家做过java和web开发都会有装,不赘述,有几点需要注意的列出来
    1. tomcat的bin/catalina.sh中加入JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.20, 这点我深受其害,开始没有设置,运行bin/nutch crawl的时候总是说JAVA_HOME is not set,我一想我明明设置了java环境变量的,java-version也是正常的,各种google,确定各种地方可以设置JAVA_HOME的地 方,都无济于事,最后在一个角落找到,在此文件中可以添加JAVA_HOME,然后运行,居然可以,但是我不明白,nutch爬虫的运行应该是不依赖于tomcat的,tomcat只是用于搜索。这点未参透。
  2. tomcat,jdk搞定之后是nutch,我直接将nutch放在用户名下面的nutch目录,然后将其中的nutch.war复制到 tomcat的webapp中,并取代ROOT(解压,重命名目录)


配置nutch:

这里参考nutch入门 学习 ,我把改的地方说明出来。

 

  1. 增加要抓取的页面(以www.163.com为例)
    1. [root@localhost nutch]#mkdir urls
    2. [root@localhost nutch]#echo http://www.163.com/>>urls/163
    3. 163文件中输入http://news.163.com/
  2. 编辑conf/crawl-urlfilter.txt文件,设定要抓取的网址信息。
    [root@localhost nutch]#vi conf/crawl-urlfilter.txt
    修改MY.DOMAIN.NAME为:
    # accept hosts in MY.DOMAIN.NAME
    +^http://([a-z0-9]*\.)*163.com/
  3. 编辑conf/nutch-site.xml文件,增加代理的属性,并编辑相应的属性值
    Xml代码
    1. <property>  
    2. <name>http.agent.name</name>  
    3. <value></value>  
    4. <description>HTTP 'User-Agent' request header. MUST NOT be empty -  
    5. please set this to a single word uniquely related to your  
    6. organization.  
    7. NOTE: You should also check other related properties:  
    8. http.robots.agents  
    9. http.agent.description  
    10. http.agent.url  
    11. http.agent.email  
    12. http.agent.version  
    13. and set their values appropriately.  
    14. </description>  
    15. </property>  
    16. <property>  
    17. <name>http.agent.description</name>  
    18. <value></value>  
    19. <description>Further description of our bot- this text is used in  
    20. the User-Agent header. It appears in parenthesis after the agent  
    21. name.  
    22. </description>  
    23. </property>  
    24. <property>  
    25. <name>http.agent.url</name>  
    26. <value></value>  
    27. <description>A URL to advertise in the User-Agent header. This will  
    28. appear in parenthesis after the agent name. Custom dictates that this  
    29. should be a URL of a page explaining the purpose and behavior of this  
    30. crawler.  
    31. </description>  
    32. </property>  
    33. <property>  
    34. <name>http.agent.email</name>  
    35. <value></value>  
    36. <description>An email address to advertise in the HTTP 'From' request  
    37. header and User-Agent header. A good practice is to mangle this  
    38. address (e.g. 'info at example dot com') to avoid spamming.  
    39. </description>  
     nutch入门 学习 中说这里就算是不修改也无所谓,这里的设置,是因为nutch遵守了robots协议,在获取response时,把自己的相关信息提交给被爬行的网站, 以供识别。但是我这样设置出现了错误提示,即http.agent.name需要设置,我将value设置成 xusulong*(记住有*)即可。其他可以不设置了。

配 置tomcat:

  1. 设定搜索目录
    (是由于默认的segment路径与我们实际的路径不符所造成的)
    [root@localhost nutch]#cd ~/tomcat
    [root@localhost tomcat]#vi webapps/ROOT/WEB-INF/classes/nutch-site.xml
    增加四行代码,修改成为
    Xml代码
    1. <configuration>  
    2. <property>  
    3. <name>searcher.dirname>  
    4. <value>/home/whu/nutch/crawl.demovalue>  
    5. property>  
    6. configuration>  
     这里的/home/whu/nutch/crawl.demo是我的nutch路径,爬虫到时候的数据就会放在程序新建的crawl.demo下面,即 nutch抓取的页面的保存目录。
  2. nutch对中文的支持还不完善,需要修改tomcat文件夹下conf/server.xml文件
    [root@localhost tomcat]#vi conf/server.xml
    增加两句,修改为
    <Connector port="8080"
    maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
    enableLookups="false" redirectPort="8443" acceptCount="100"
    connectionTimeout="20000" disableUploadTimeout="true"
    URIEncoding="UTF-8" useBodyEncodingForURI="true" />

抓取网页:

whu@leopard:~/nutch$ bin/nutch crawl urls -dir crawl.demo -depth 2 -threads 4 -topN 5 >& crawl.log

具体的参数nutch入门 学习 有解释,也可以参见nutch的官方网站。这里只抓取少量站点。

这时候 crawl.log会记录抓取的信息,我中间遇到过

如下几个错误:

  1. http.agent.name需要设置问题
  2. Input path does not exist问题,这个多试几次路径即可,只要这里的crawl.demo和配置tomcat中的路径对应,记得出错的时候把出错的目录删除,否则下次还是 出错。

测试结果:

运行tomcat,进入首页,搜索网易,结果如下:

搞了一个下午和晚上,泪流满面,中途还有其他的错误我记不大清楚了,总之严重的错误我列出来了,仔细看系统如何报错,google之,仔细发现错误 才是王道。