`
mrzhangtufu
  • 浏览: 60881 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JSP学习回忆5

    博客分类:
  • jsp
阅读更多

1、用javascript实现用JSP写的图片验证码的换一张:
function changeRnd(){
  //直接写rand.jsp不能形成刷新,就不能改变图片,
  //故特意加上"?Math.random()进行刷新显示.要查询字符串不断更改,即
  //url不断更新
  document.images["rnd"].src="rand.jsp?"+Math.random();
}
<img src="rand.jsp" id="rnd" onclick="changeRnd();" style="cursor:hand" />
2、用java.text.DateFormat类可以本地格式化日期和时间:
a、先用下面的4中方法之一返回一个实例:
static DateFormat getInstance()
static DateFormat getTimeInstance()
static DateFormat getDateInstance()
static DateFormat getDateTimeInstance()
例如:DateFormat usa=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,Locale.US);
b、调用DateFormat的format(Date date)得到格式化后的字符串
如:usa.format(new Date());
3、jsp页面中的中文乱码解决可在头部加入:
<%@page pageEncoding="GBK"%>
<%@page contentType="text/html;charset=GBK"%><!--最主要-->
<%request.setCharacterEncoding("GBK");%>
4、解决页面或URL中传递参数中的中文乱码,可以将传递的字符串用"GBK"或"gb2312"进行重新编码:
<% String str=new String(request.getParameter("str").getBytes("iso8859-1"),"GBK"); %>
5、Servlet中要回应中文时可以:response.setContentType("text/html;charset=GBK")进行解决
6、设置页面不缓存:
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
7、默认情况下,数据库连接是自动提交的。要进行事务处理,则首先用 conn.setAutoCommit(false);
设置自动提交为false,执行完数据库操作后,就用conn.commit()提交成功。如出现异常或有一处错误
在调用conn.rollback()撤销所有SQL操作。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics