2012年2月16日星期四

Web Development Tips

Web Development Tips

1.    Tomcat

a)    FAIL - Application at context path /app could not be started"

When redeploy your webapp via tomcat manager, sometimes tomcat seems not able to copy .war to deploy correctly. After click deploy button, if you check webapps/yourapp.war content, you will find it still use old version. So you have to manually copy your .war file to webapps/ and unrar it. Here is how to do it

1. suppose you have aa.war at somewhere/
2. mkdir aa
3. cd aa
4. jar -xvf aa.war
5. cd webapps
6.cp -r somewhere/aa .
7. restart tomcat

I just found another faster way to do it. In eclipse, when exporting war file, directly select the tomcat webapps dir. The tomcat will automatically deploy it, so you don’t need to use tomcat manager to deploy

b)    数字证书和数字签名

数字证书:证书包含了公钥
数字签名:用私钥加密了的messagemd5
https过程:浏览器发出https请求 | 网站送出数字证书+用私钥加密了的文本 | 浏览器验证数字证书是否经过第三方认证 | 如果是,安装证书,解密文本 | 浏览器送出用证书中公钥加密了的用户名和密码 + 数字签名 | 网站用用户的公钥解密数字签名 | 如成功 则对比文本的md5 | 如文本没有篡改 进行下一步

c)     One-Way Https Configuration

第一步:Tomcat5.5使用SSL功能、证书生成(使用JDK1.5自带的keytool.exe工具)
注意必须使用tomcat使用的jdk版本去生成
命令行中C:\Java\jdk1.5.0_09\bin> keytool -genkey -alias tomcat -keyalg RSA -validity 365
注:-alias tomcat 设定别名;   -validity 365 证书有效期设为365天;
生成证书过程中,提示设定<tomcat>的密码时,使用和keystore相同的密码。默认回车就可以了。
生成的证书默认是放在当前系统用户的主目录下(C:\Documents and Settings\Administrator),复制至Tomcat的主目录以便于下一步中的server.xmlkeystoreFile的值设定。



第二步:Tomcat5.0.28server.xml,打开SSL连接器选项
  <Connector port="8443" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" scheme="https" secure="true"
               keystoreFile=".keystore"  keystorePass="******"
               clientAuth="false" sslProtocol="TLS"/>
注:即添加后面那两属性值,keystoreFile的路径,keystorePass的密码。

重起Tomcat服务器后, 就只能用HTTPS协议访问了, 只是端口是8443, 需要在URL中显示指定. 最后,https 的默认端口是443 (Usually is taken by apache)

d)    强制https访问

  tomcat\conf\web.xml中的</welcome-file-list>后面加上这样一段:appweb.xml里也可以

    <login-config> 
        <!-- Authorization setting for SSL --> 
        <auth-method>CLIENT-CERT</auth-method> 
        <realm-name>Client Cert Users-only Area</realm-name> 
    </login-config> 
    <security-constraint> 
        <!-- Authorization setting for SSL --> 
        <web-resource-collection > 
            <web-resource-name >SSL</web-resource-name> 
            <url-pattern>/*</url-pattern> 
        </web-resource-collection> 
        <user-data-constraint> 
            <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
        </user-data-constraint> 
    </security-constraint> 

完成以上步骤后,在浏览器中输入http的访问地址也会自动转换为https

e)     Two-Way Https Configuration And More Details



2.    MySql

a)    Database Permission Setup

Please Type the following in Linux console

mysql -p -u root
grant all on phlex.* to 'username'@'%';
show grants for username@'%';
grant select,insert,update on phlex.* to 'osqa'@'localhost';
show grants for osqa@localhost;

b)    Show Database Schema in Eclipse

1. Switch to “Database Development” Perspective
2. Creating a new Database Connection: add database name at the end of your URL, Some thing like: jdbc:mysql://yourhost:3306/phlex. Otherwise you won’t see the columns of tables.

3.    Eclipse

a)    Deployment or Export war file in Eclipse

You can view and modify the setup in right click of Your Project à Deployment Assembly. Default setup is
Copy all stuff under WebContent
Copy all .class under /src to WEB-INF/classes

b)    Using external Jar Libraries

In eclipse, you need libraries in two places:
1) To compile java files. These libraries are shown in YourProject/JavaResource/Libraries. You can add them via right click of YourProject à Java Build Path à Add External Jars OR Add Libraries. Here Jars means individual libs while Libraries are group of libs
2) To run .class file on your http server. These libraries are located in WEB-INF/lib. You can add your Libraries into it as well.
So the best way to do it is as follows
a) Create User Libraries to contain all external libraries: Window à Perferences à Java à Build Path à User Libraries à New
b) Add this Library into your project for build
c) Add this Library into your War file for http server

4.    HTML

5.    CSS

a)    Force CSS changes to “go live” immediately

If you update your .css file, you may have noticed that you have to “force-reload” your site in your browser to see the changes. This is because your browser keeps a copy of the CSS cached on your hard drive. Depending on how your server is set up, it may not check for a new version of the stylesheet for a couple hours, or longer! One way to solve this is to “version” your CSS file, by adding ?v=123 to the URL in the to your stylesheet.

<link rel="stylesheet" type="text/css" href="mystyle.css?v=1.1" />

b)    Control table and its content position using CSS

table{
       border-collapse: collapse;
       border: 1px solid gray;
       width:1px;
       white-space:nowrap
       text-align: left;
       margin-left:auto;                èwhole table center
    margin-right:auto;                     èwhole table center
}

6.    Hibernate

a)    Install Hibernate Tool Plug-in in Eclipse Indigo

1. When Eclipse starts, go to Help > Install New Software...
2. Paste in the URL of the update site you've chosen, such as:
    http://download.jboss.org/jbosstools/updates/development/indigo/ or
    http://download.jboss.org/jbosstools/updates/development/indigo/soa-tooling/
3. Select all in “JBOSS Tools”, Hibernate in “Application Development”, “Data Services”, “Web and Java EE Development”
4. Install

b)    Using Hibernate in Eclipse Indigo

1. Create your Java EE project
2. Create a database and table in MySql
3. Create a database connection in eclipse’s Database Development perspective
4. Switch to eclipse’s Hinbernate perspecitve
5. Create Hibernate Configuration file by: File à New à Other à Hibernate à Hibernate Configuration File and place the hibernate.cfg.xml at the src root of your Java EE project.
6. Create consol configuration which defines how hibernate works.
7. Create POJO class by reveseing engineering using hibernate tools: File à  New à Hibernate à Hibernate Reverse Engineering file:  Select the table you want to reverse engeering. And then click tool bar icon ( run new configuration) to create pojo class automatically
8. Place hbm.xml file with your pojo class
Details are on
http://www.packtpub.com/article/developing-applications-with-jboss-and-hibernate-1

c)     Get the ID after Save

The object is automatically filled with the ID after save

7.    Java

a)    Date, SimpleDateFormat and Caleander

JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理。

Date startTime = new Date();//当前时间
System.out.println(startTime); // Sun Mar 08 16:35:58 CST 2009
Date endTime = new Date(startTime.toTime() + 60 * 60 * 24 * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String endStr = sdf.format(endTime);
System.out.println(endStr); // 2009-03-09 16:35:38.1234

Calendar c = Calendar.getInstance(); //当前时间
int month = c.get(Calendar.MONTH) + 1;
c.add(Calendar.DATE, 100);

Date d = c.getTime();
Calendar c = Calendar.getInstance();
c.setTime(d);

8.    Java Mail

9.    Java System

10.           Java Files





没有评论: