防止jSP或SERVLET中的输出被浏览器保存在缓冲区中
<%
response.setHeader(“Cache-Control”,”no-store”); //HTTP 1.1
response.setHeader(“Pragma”,”no-cache”); //HTTP 1.0
response.setDateHeader (“Expires”, 0); //prevents caching at the proxy server
%>
使用jSP大约有下列三种跳转方式:
1. response.sendRedirect();
2. response.setHeader(“Location”,””);
3. <jsp:forward page=”” />
1.可以使用:
response.sendRedirect(“http://www.foo.com/path/error.html”);
2.可以手工修改HTTP header的Location属性,如下:
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = “/newpath/index.html”;
response.setHeader(“Location”,newLocn);
%>
3.也可以使用forward:
<jsp:forward page=”/newpage.jsp” />
请注意:只能在任何输出还没有发送到客户端之前使用这种方式。
String curTime = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(new Date());
String text = StringUtils.left(brief.toString().trim(),25);
建立一个实现Filter接口的类。这个类需要三个方法,分别是:doFilter、init和destroy
2006.08.16
java资源文件
<message-resources key=”moreResources” parameter=”com.systemmobile.example.MoreApplicationResources” null=”false” />
key 指定不同的资源文件
parameter 指定资源文件位置
null=”false” 如果某个资源字符串不存在将返回???key??? 而不是仅仅显示null
中国 xxx_zh_CN,xxx_zh_TW
※一般调用
<bean:message bundle=”moreResources” key=”some.message.key” /> 指定资源文件
<bean:message key=”some.message.key” arg0=”no” arg1=”yes” /> 传入参数
<html:message> <html:errors> 错误信息或消息信息要保存在request里才会显示
<html:link> 的”titlekey”属性定义 “altKey”
protected MessageResources getResources(javax.servlet.http.HttpServletRequest request, java.lang.String key);
2006.08.11
参考的开发工具组合
版本控制:Subversion
项目管理、文档:Trac
Bug管理:Testtrack
开发工具:Visual C++,Eclipse
开发框架:Spring Framework、Velocity、ibatis、rails
系统维护:bash、Python
web服务器:Apache、Lighttpd、Resin
数据库服务器:Oracle、Mysql
2006.07.23
config.getServletName() 可以取得Servlet的註冊名稱
config.getInitParameter(“”) 可以取得指定的初始參數設定值
取得所有传递的参数
Enumeration params = request.getParameterNames();
while(params.hasMoreElements()) {
String param = (String) params.nextElement();
out.println(param + ” = ” + request.getParameter(param) + “<br />”);
}
請求的伺服器: <%= request.getServerName() %> <br />
使用協定: <%= request.getProtocol() %> <br />
請求方法: <%= request.getMethod() %> <br />
請求的埠號: <%= request.getServerPort() %> <br />
Context路徑: <%= request.getContextPath() %> <br />
Servlet路徑: <%= request.getServletPath() %> <br />
URI路徑: <%= request.getRequestURI() %> <br />
查詢字串: <%= request.getQueryString() %> <br />
使用者主機IP: <%= request.getRemoteAddr() %> <br />
使用者使用埠號: <%= request.getRemotePort() %>
设定标头 response.setHeader(“名称”,”值”) response.setIntHeader 整数值 response.setDateHeader() Date值
response.addHeader 追加
response.sendError(response.SC_NOT_FOUND, “找不到檔案”); SC_NOT_FOUND代表404状态
response.setStatus(response.SC_MOVED_TEMPORARILY); 必需自行处理
jsp学习
<%@ page
[ language=”java” ]
[ extends=”package.class” ]
[ import=”{package.class | package.*}, …” ]
[ session=”true | false” ]
[ buffer=”none | 8kb | sizekb” ]
[ autoFlush=”true | false” ]
[ isThreadSafe=”true | false” ]
[ info=”text” ]
[ errorPage=”relativeURL” ]
[ contentType=”mimeType [ ;charset=characterSet ]” | “text/html ; charset=ISO-8859-1” ]
[ isErrorPage=”true | false” ]
%>
标签库
<%@ taglib uri=”URIToTagLibrary” prefix=”tagPrefix” %>
<%@ taglib uri=”http://www.jspcentral.com/tags” prefix=”public” %>
<public:loop></public:loop>
页面重定向
<jsp:forward page={“relativeURL” | “<%= expression %>”} >
<jsp:param name=”parameterName” value=”{parameterValue | <%= expression %>}” /></jsp:forward>
获取Bean的属性值,用于显示在页面中
<jsp:getProperty name=”beanInstanceName” property=”propertyName” />
包含一个静态或动态文件
<jsp:include page=”{relativeURL | <%= expression %>}” flush=”true” >
<jsp:param name=”paramet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
货币的格式转换(有问题) //转换数字格式为货币格式; String myString = NumberFormat.getCurrencyInstance().format(myNumber); out.println(myString); %&gt; 控制台输出为: ¥12,345.02 取小数点后几位(四舍五入) double size = 123.1236; DecimalFormat doc_size= new DecimalFormat("0.00"); out.println(doc_size.format(size)); %&gt; 判断空值 if isnull(ss) { } else { } 在jsp中判断字符串要使用compareTo方法,不要用== 防止jSP或SERVLET中的输出被浏览器保存在缓冲区中 response.setHeader("Cache-Control","no-store"); //HTTP 1.1 response.setHeader("Pragma","no-cache"); //HTTP 1.0 response.setDateHeader ("Expires", 0); //prevents caching at the proxy server %&gt; 使用jSP大约有下列三种跳转方式: 1. response.sendRedirect(); 2. response.setHeader("Location",""); 3. 1.可以使用: response.sendRedirect("http://www.foo.com/path/error.html"); 2.可以手工修改HTTP header的Location属性,如下: response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn = "/newpath/index.html"; response.setHeader("Location",newLocn); %&gt; 3.也可以使用forward: 请注意:只能在任何输出还没有发送到客户端之前使用这种方式。 jsp显示当前时间及日期 import java.util.*; public class Mydate { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); String date_now=cal.get(cal.YEAR)+"年"+(cal.get(cal.MONTH)+1)+"月"+cal.get(cal.DATE)+"日 "; String time_now=cal.get(cal.HOUR_OF_DAY)+"时"+cal.get(cal.MINUTE)+"分"+cal.get(cal.SECOND)+"秒"; System.out.println(date_now); System.out.println(time_now); } } import java.util.Date; public class test { public static void main(String[] args){ Date date = new Date(); int year=date.getYear()+1900; int month=date.getMonth()+1; int day=date.getDate(); System.out.println("It is "+year+"/"+month+"/"+day+" (YYYY/MM/DD)today."); } } The current time is: Date now = new Date(); out.println(DateFormat.getTimeInstance().format(now)); %&gt; 在jSP中创建目录 Mkdir(String path) String Mkdir(String path) { String msg=null; java.io.File dir; // 新建文件对象 dir =new java.io.File(path); if (dir == null) { msg = "错误原因: 对不起,不能创建空目录!"; return msg; } if (dir.isFile()) { msg = "错误原因: 已有同名文件<strong>" + dir.getAbsolutePath() + "</strong>存在。"; return msg; } if (!dir.exists()) { boolean result = dir.mkdirs(); if (result == false) { msg = "错误原因: 目录<strong>" + dir.getAbsolutePath() + "</strong>创建失败,原因不明!"; return msg; } // 如果成功创建目录,则无输出。 // msg ="成功创建目录: <strong>" + dir.getAbsolutePath() + "</strong>"; return msg; } else { msg = "错误原因: 目录<strong>" + dir.getAbsolutePath() + "</strong>已存在。"; } return msg; } %&gt; String filepath = "/usr/home/hoyi/html/dir"; String opmsg = Mkdir(filepath); %&gt; 将return 转为 函数 public static String returnToBr(String sStr) { if (sStr == null // sStr.equals("")) { return sStr; } String sTmp = new String(); int i = 0; while (i { if (sStr.charAt(i) == '\n') { sTmp = sTmp.concat(" "); } else { sTmp = sTmp.concat(sStr.substring(i,i+1)); } i++; } return sTmp; } 使用JSP大约有下列三种跳转方式: 1. response.sendRedirect(); 2. response.setHeader("Location",""); 3. 1.可以使用: response.sendRedirect("http://www.foo.com/path/error.html"); 2.可以手工修改HTTP header的Location属性,如下: <% response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn = "/newpath/index.html"; response.setHeader("Location",newLocn); %> 3.也可以使用forward: <jsp:forward page="/newpage.jsp" /> 请注意:只能在任何输出还没有发送到客户端之前使用这种方式。 这里是某些JSP可用的隐含变量: * request – 向HttpServletRequest作参考 * response – 向HttpServletResponse作参考 * pageContext – 在JSP之中提供向名称空间的访问 * session – 向HttpSession作参考 * application – 向ServletContext作参考 * config – 向ServletConfig作参考 * page – 向当前被访问的JSP实例作参考,相似地,对这个JSP的所产生的Servlet使用这个变量 * exception – 向由JSP产生的例外作参考,只在isErrorPage设置为真时在JSP上可用 标准JSP标签是: * – 允许你在页面,请求,session或是应用软件的范围内中放置或查找一个bean(这个标签对于保持状态非常有用)。 * – 允许你对已经定义的bean设置属性。 * -– 获取一个bean的属性的值,将其转换为一个字符串,并放置在输出流之中。 * – 向包含前进和插入等标准活动提供参数和值。 * – 在请求时间加入一个页面代替@include指示的翻译时间。 * –在现有的Web应用软件中使请求前进到另一个资源。 * – 使能JSP创造者使用客户机浏览器的独立架构来产生HTML,可以导致指定的applet或是部件的下载和后续执行行为(标签的值被一个 1、大家给出这么多意见我很高兴 2、ant没有提到是因为在eclipse内置了对ant的支持 3、各软件的下载地址如下 struts--[url]http://struts.apache.org/[/url] eclipse--[url]www.eclipse.org[/url] junit--[url]http://www.junit.org/index.htm[/url] httpunit--[url]http://sourceforge.net/projects/httpunit/[/url] log4j--[url]http://logging.apache.org/log4j/docs/download.html[/url] 一、运行前准备 建议了一个MS SQLServer7数据库 DNS,名称为:Test_DB 数据库中有一个表:guestbook字段为:name(varchar),email(varchar),body(text) 数据库用户为sa 密码空,可以自己修改的。 二、代码 &nbsp; //变量声明 java.sql.Connection sqlCon; //数据库连接对象 java.sql.Statement sqlStmt; //SQL语句对象 java.sql.ResultSet sqlRst; //结果集对象 java.lang.String strCon; //数据库连接字符串 java.lang.String strSQL; //SQL语句 int intPageSize; //一页显示的记录数 int intRowCount; //记录总数 int intPageCount; //总页数 int intPage; //待显示页码 java.lang.String strPage; int i,j,k; //设置一页显示的记录数 intPageSize = 5; //取得待显示页码 strPage = request.getParameter("page"); if(strPage==null){ //表明在QueryString中没有page这一个参数,此时显示第一页数据 intPage = 1; } else{ //将字符串转换成整型 intPage = java.lang.Integer.parseInt(strPage); if(intPage //装载JDBC-ODBC驱动程序 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //设置数据库连接字符串 strCon = "jdbc:odbc:Test_DB"; //连接数据库 sqlCon = java.sql.DriverManager.getConnection(strCon,"sa",""); //创建SQL语句对象 sqlStmt = sqlCon.createStatement(); //获取记录总数 strSQL = "select count(*) from guestbook"; sqlRst = sqlStmt.executeQuery(strSQL); //执行SQL语句并取得结果集 sqlRst.next(); //记录集刚打开的时候,指针位于第一条记录之前 intRowCount = sqlRst.getInt(1); sqlRst.close(); //关闭结果集 //记算总页数 intPageCount = (intRowCount+intPageSize-1) / intPageSize; //调整待显示的页码 if(intPage&gt;intPageCount) intPage = intPageCount; //设置获取数据SQL语句 strSQL = "select name,email,body from guestbook"; //执行SQL语句并取得结果集 sqlRst = sqlStmt.executeQuery(strSQL); //将记录指针定位到待显示页的第一条记录上 i = (intPage-1) * intPageSize; for(j=0;j &nbsp; &nbsp; &nbsp; &nbsp; <p align="center">jdbc-odbc留言版</p> //显示数据i = 0;while(i <table width="600" border="1" cellspacing="0" cellpadding="0" align="center"> <tbody> <tr> <td>姓名:</td> <td>邮件:</td> <td colspan="2"></td> <td colspan="2" align="center">第页 共页 &nbsp; <a href="mssql.jsp?page=<%=intPage+1%>">下一页</a> } %&gt; 1){%&gt; <a href="mssql.jsp?page=<%=intPage-1%>">上一页</a> } %&gt;</td> </tr> </tbody> </table> &nbsp; &nbsp; //关闭结果集 sqlRst.close(); //关闭SQL语句对象 sqlStmt.close(); //关闭数据库 sqlCon.close(); %&gt; 一、运行前准备 建议了一个MS SQLServer7数据库 DNS,名称为:Test_DB 数据库中有一个表:guestbook字段为:name(varchar),email(varchar),body(text) 数据库用户为sa 密码空,可以自己修改的。 二、代码 &nbsp; //变量声明 java.sql.Connection sqlCon; //数据库连接对象 java.sql.Statement sqlStmt; //SQL语句对象 java.sql.ResultSet sqlRst; //结果集对象 java.lang.String strCon; //数据库连接字符串 java.lang.String strSQL; //SQL语句 int intPageSize; //一页显示的记录数 int intRowCount; //记录总数 int intPageCount; //总页数 int intPage; //待显示页码 java.lang.String strPage; int i,j,k; //设置一页显示的记录数 intPageSize = 5; //取得待显示页码 strPage = request.getParameter("page"); if(strPage==null){ //表明在QueryString中没有page这一个参数,此时显示第一页数据 intPage = 1; } else{ //将字符串转换成整型 intPage = java.lang.Integer.parseInt(strPage); if(intPage //装载JDBC-ODBC驱动程序 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //设置数据库连接字符串 strCon = "jdbc:odbc:Test_DB"; //连接数据库 sqlCon = java.sql.DriverManager.getConnection(strCon,"sa",""); //创建SQL语句对象 sqlStmt = sqlCon.createStatement(); //获取记录总数 strSQL = "select count(*) from guestbook"; sqlRst = sqlStmt.executeQuery(strSQL); //执行SQL语句并取得结果集 sqlRst.next(); //记录集刚打开的时候,指针位于第一条记录之前 intRowCount = sqlRst.getInt(1); sqlRst.close(); //关闭结果集 //记算总页数 intPageCount = (intRowCount+intPageSize-1) / intPageSize; //调整待显示的页码 if(intPage&gt;intPageCount) intPage = intPageCount; //设置获取数据SQL语句 strSQL = "select name,email,body from guestbook"; //执行SQL语句并取得结果集 sqlRst = sqlStmt.executeQuery(strSQL); //将记录指针定位到待显示页的第一条记录上 i = (intPage-1) * intPageSize; for(j=0;j &nbsp; &nbsp; &nbsp; &nbsp; <p align="center">jdbc-odbc留言版</p> //显示数据i = 0;while(i <table width="600" border="1" cellspacing="0" cellpadding="0" align="center"> <tbody> <tr> <td>姓名:</td> <td>邮件:</td> <td colspan="2"></td> <td colspan="2" align="center">第页 共页 &nbsp; <a href="mssql.jsp?page=<%=intPage+1%>">下一页</a> } %&gt; 1){%&gt; <a href="mssql.jsp?page=<%=intPage-1%>">上一页</a> } %&gt;</td> </tr> </tbody> </table> &nbsp; &nbsp; //关闭结果集 sqlRst.close(); //关闭SQL语句对象 sqlStmt.close(); //关闭数据库 sqlCon.close(); %&gt; |