折腾了几天,也算完成这点事情了,不容易啊。。
今天还碰到了类型转换的问题,发生错误了想着问问人,问星哥是否有小范同学的QQ,星哥说:“这些问题还要问,上网查一下就行了,jsp,int之类的” 忽然发现,星哥跟我一样,都有点倔强,小伙C++搞的也还不够火热,可也倔强的很多问题都要弄个清楚,我也折腾的有脾气了,倔强,非得解决了不可,有时候还不给自己问人,非得把它折腾出来不可,这坏脾气。。。
以初学者的身份小结一下:
1 jar文件优先级和class文件位置(未解决)
jar放置位置有好几个地方,有空一定得弄明白,目前对优先级还不太明确,小范同学做过jsp,好像也没留意此问题。
但是有一点要注意,这个包引用的其它jar文件要放一个目录下,否则在别的地方找不来,java的机制蛮复杂。
class文件放在 WEB-INF/classes/下,有一个问题还不太明白,是自身目录下的WEB-INF还是网站根目录的?
似乎我的本地环境需要放置在网站根目录,服务器环境放在自身目录的?
至少,发现找不到类定义时,可以从以上问题开始解决,也算是得益。
2 设置jbuilder环境
打开一个jpx项目文件,发现一堆的错误提示,怎么办?
设置一下就好:

设置library

添加你的所有相关的jar文件即可
jbuilder似乎对UTF8文件不太支持,中文是乱码。另外,对于字符定位也很差劲,跟我的盗版有关?
我只有在editplus里面编辑,切换过来会自动更新了,这点很聪明,然后可以编译成class,更新到服务器上即可。
3 类型转换
做PHP和js都不会有严格的类型定义,这里可不一样了,一点类型的问题都会导致错误。
如何将字串 String 转换成整数 int?
对于这个理解要注意,比如long类型是 Long.parseLong("23231")
如果很长的数字用Integer.parseInt转换就会出错的,看不出问题,我也迷惑了,得小范同学提示以long解决
如何将整数 int 转换成字串 String ?
4 cookie使用
给个示例代码片段,我也还没知道最好的方式,那就这个不错啦:
5 session的使用
参考代码片段好了,这玩意不新鲜:
6 BASE64 编码和解码
看例子吧,看的别人的文章,但不是原出处,我也不知道出处了。
要用的时候要引用两个类:
文中还有其他加密的内容,我没试过,就不介绍了。
7 jsp追加的方式写文件
内容参考自《PrintWriter追加至文本文件》,代码简化一下就是:
//需要的两个类:
//import java.util.*;
//import java.io.*;
String fileName = "/tmp/logs.txt";
FileWriter fw = null;
PrintWriter toFile = null;
String info = "info to write into file...";
try {
fw = new FileWriter(fileName, true); // throw IOException
toFile = new PrintWriter(fw); // throw FileNotFoundException
toFile.println(info +"\n");
toFile.close();
} catch (FileNotFoundException e) {
//System.out.println("PrintWriter error opening the file " + fileName);
//System.exit(0);
} catch (IOException e) {
//System.out.println("FileWriter error opening the file " + fileName);
//System.exit(0);
}
8 使用Mysql
见之前整理的《JSP使用Mysql》
原创内容如转载请注明:来自 阿权的书房
今天还碰到了类型转换的问题,发生错误了想着问问人,问星哥是否有小范同学的QQ,星哥说:“这些问题还要问,上网查一下就行了,jsp,int之类的” 忽然发现,星哥跟我一样,都有点倔强,小伙C++搞的也还不够火热,可也倔强的很多问题都要弄个清楚,我也折腾的有脾气了,倔强,非得解决了不可,有时候还不给自己问人,非得把它折腾出来不可,这坏脾气。。。
以初学者的身份小结一下:
1 jar文件优先级和class文件位置(未解决)
jar放置位置有好几个地方,有空一定得弄明白,目前对优先级还不太明确,小范同学做过jsp,好像也没留意此问题。
但是有一点要注意,这个包引用的其它jar文件要放一个目录下,否则在别的地方找不来,java的机制蛮复杂。
class文件放在 WEB-INF/classes/下,有一个问题还不太明白,是自身目录下的WEB-INF还是网站根目录的?
似乎我的本地环境需要放置在网站根目录,服务器环境放在自身目录的?
至少,发现找不到类定义时,可以从以上问题开始解决,也算是得益。
2 设置jbuilder环境
打开一个jpx项目文件,发现一堆的错误提示,怎么办?
设置一下就好:

设置library

添加你的所有相关的jar文件即可
jbuilder似乎对UTF8文件不太支持,中文是乱码。另外,对于字符定位也很差劲,跟我的盗版有关?
我只有在editplus里面编辑,切换过来会自动更新了,这点很聪明,然后可以编译成class,更新到服务器上即可。
3 类型转换
做PHP和js都不会有严格的类型定义,这里可不一样了,一点类型的问题都会导致错误。
如何将字串 String 转换成整数 int?
引用
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异
对于这个理解要注意,比如long类型是 Long.parseLong("23231")
如果很长的数字用Integer.parseInt转换就会出错的,看不出问题,我也迷惑了,得小范同学提示以long解决
如何将整数 int 转换成字串 String ?
引用
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 转成字串的方法大同小异
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 转成字串的方法大同小异
4 cookie使用
给个示例代码片段,我也还没知道最好的方式,那就这个不错啦:
//cookie名称
String cookieName = "UserInfo";
String cookieValue = null;
Cookie[] userCookie = request.getCookies();
//检查cookie值
if(userCookie != null && userCookie.length>0){
for(int i=0;i<userCookie.length;i++)
{
if (userCookie[i].getName().equalsIgnoreCase(cookieName))
{
cookieValue = userCookie[i].getValue();
break;
}
}
}
//写Cookie
Cookie uCookie = new Cookie(cookieName,"http://www.aslibra.com/");
response.addCookie(uCookie);
String cookieName = "UserInfo";
String cookieValue = null;
Cookie[] userCookie = request.getCookies();
//检查cookie值
if(userCookie != null && userCookie.length>0){
for(int i=0;i<userCookie.length;i++)
{
if (userCookie[i].getName().equalsIgnoreCase(cookieName))
{
cookieValue = userCookie[i].getValue();
break;
}
}
}
//写Cookie
Cookie uCookie = new Cookie(cookieName,"http://www.aslibra.com/");
response.addCookie(uCookie);
5 session的使用
参考代码片段好了,这玩意不新鲜:
String mySession = request.getParameter("mySession");
if(mySession != null ){
session.setAttribute("mySession", mySession);
}else{
session.setAttribute("mySession", "0");
}
if(mySession != null ){
session.setAttribute("mySession", mySession);
}else{
session.setAttribute("mySession", "0");
}
6 BASE64 编码和解码
看例子吧,看的别人的文章,但不是原出处,我也不知道出处了。
// 将 s 进行 BASE64 编码
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(String s) {
if (s == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(String s) {
if (s == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
要用的时候要引用两个类:
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Decoder;
文中还有其他加密的内容,我没试过,就不介绍了。
7 jsp追加的方式写文件
内容参考自《PrintWriter追加至文本文件》,代码简化一下就是:
//需要的两个类:
//import java.util.*;
//import java.io.*;
String fileName = "/tmp/logs.txt";
FileWriter fw = null;
PrintWriter toFile = null;
String info = "info to write into file...";
try {
fw = new FileWriter(fileName, true); // throw IOException
toFile = new PrintWriter(fw); // throw FileNotFoundException
toFile.println(info +"\n");
toFile.close();
} catch (FileNotFoundException e) {
//System.out.println("PrintWriter error opening the file " + fileName);
//System.exit(0);
} catch (IOException e) {
//System.out.println("FileWriter error opening the file " + fileName);
//System.exit(0);
}
8 使用Mysql
见之前整理的《JSP使用Mysql》
原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘