⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0101.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
    <p align="center"><big><strong>初学jsp心得</strong></big></p>

<div align="right">摘自互联网</div>

<p>
废话不多说!<br>
<br>
测试环境为 jdk1.2.2&nbsp;&nbsp;jswdk-1.0&nbsp;&nbsp;winnt4.0中文版。<br>
<br>
1。java是大小写敏感的,用过其他编程语言的人最容易犯这个错误,尤其是刚上手的时候。我刚开始调试jsp的时50%以上的编译错误是都是因为这个。<br>
<br>
2。java的调用过程都是要加括号的,一开始比较容易忽视,如title=request.getParameter(&quot;title&quot;).trim();<br>
<br>
3。jsp中对应asp中的request.form()和request.querystring()的解决方法。<br>
jsp中取得参数没有form和queryString之分,都是通过request.getParameter(&quot;XXXX&quot;)来取得。虽然jsp也有request.getQueryString()方法,但测试结果是 test.jsp?id=1&amp;page=20&nbsp;&nbsp;得到 id=1&amp;page=20。<br>
&nbsp;&nbsp;&nbsp;如果url和form有相同的参数名称呢?下面是一段测试代码:<br>
&lt;form method=&quot;POST&quot; action=&quot;query.jsp?id=2&quot;&gt;<br>
&nbsp;&nbsp;&lt;input type=&quot;text&quot; name=&quot;id&quot; value=&quot;1&quot; size=&quot;60&quot;&gt;<br>
&lt;/form&gt;<br>
name都是id,结果是url的参数优先得到,jsp的这种处理方式和asp相比我觉的各有所长。<br>
<br>
4。头疼的汉字处理问题。<br>
在其他的文章里曾说到在中文NT环境下如下语句输出会得到乱码,<br>
&lt;%=&quot;你好&quot;%&gt; 及 out.print(&quot;你好&quot;);等。解决方法是只要对字符串变量进行编码就可以得到正确结果,如下代码可以得到正确的输出:<br>
&lt;%&nbsp;&nbsp;String title=&quot;你好&quot;;<br>
&nbsp;&nbsp;byte[] tmpbyte=title.getBytes(&quot;ISO8859_1&quot;);<br>
&nbsp;&nbsp;title=new String(tmpbyte);<br>
&nbsp;&nbsp;out.print(title); %&gt;<br>
或者&lt;%=title%&gt;<br>
<br>
关于sql语句汉字问题,例句为 select * from test where title='谁是傻瓜'<br>
在jdbc-odbc驱动下连db2,不管是原句还是对sql语句进行编码后都死活通不过。<br>
换了ibm的jdbc直接驱动后,对sql语句编码后程序可以通过。<br>
<br>
这个问题的产生大概是中文NT的原因,在其他环境下可能就没汉字处理问题了,据说ibm的web sphere对中文支持的很好,这也给jsp的开发带来一定的通用性问题。据说对字符串编码是一种通用的解决方法,不过没有这么多环境来测试。<br>
<br>
5。在asp中经常使用到字符串判断语句如 if state=&quot;真是傻瓜&quot; then.....<br>
&nbsp;&nbsp;&nbsp;在java中String变量不是一个简单的变量而是一个类实例,不同的方法会得到不同的结果<br>
a.<br>
String&nbsp;&nbsp;str1=&quot;我是傻瓜&quot;;<br>
String&nbsp;&nbsp;str2=&quot;我是傻瓜&quot;; (or String&nbsp;&nbsp;str2=&quot;我是&quot;+&quot;傻瓜&quot;; )<br>
if (str1==str2)<br>
&nbsp;&nbsp;out.print(&quot;yes&quot;);<br>
else&nbsp;&nbsp;<br>
&nbsp;&nbsp;out.print(&quot;no&quot;);<br>
结果是&quot;yes&quot;。<br>
大概是编译优化,str1,str2指向同一个类实例;<br>
<br>
b.<br>
String str1,str2,str3;<br>
str1=&quot;我是傻瓜&quot;;<br>
str2=&quot;我是&quot;;<br>
str3=str2+&quot;傻瓜&quot;;<br>
if (str1==str3)<br>
&nbsp;&nbsp;out.print(&quot;yes&quot;);<br>
else<br>
&nbsp;&nbsp;out.print(&quot;no&quot;);<br>
结果是&quot;no&quot;。<br>
<br>
String str1=new String(&quot;我是傻瓜&quot;);<br>
String str2=new String(&quot;我是傻瓜&quot;);<br>
if (str1==str2)<br>
&nbsp;&nbsp;out.print(&quot;yes&quot;);<br>
else<br>
&nbsp;&nbsp;out.print(&quot;no&quot;);<br>
结果是&quot;no&quot;。<br>
<br>
String str1=new String(&quot;我是傻瓜&quot;);<br>
String str2=new String(&quot;我是傻瓜&quot;);<br>
if (str1.compareTo(str2)==0)<br>
&nbsp;&nbsp;out.print(&quot;yes&quot;);<br>
else<br>
&nbsp;&nbsp;out.print(&quot;no&quot;);<br>
结果是&quot;yes&quot;。<br>
<br>
所以在jsp中判断字符串要使用compareTo方法,用惯传统语言还真一下子适应不过来,熟悉java的朋友应该没这个问题。<br>
<br>
6。如何判断数据库为空?<br>
&nbsp;&nbsp;&nbsp;result = stmt.executeQuery(sql);<br>
&nbsp;&nbsp;&nbsp;if (result.next()) <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;......<br>
&nbsp;&nbsp;&nbsp;result执行后游标出于一个未明的状态,不能进行状态判断,也不能取值,一定要next()一下才可以用。<br>
&nbsp;&nbsp;&nbsp;<br>
<br>
7。在jsp中实现分页。<br>
page是关键字,不能当变量。<br>
conn.jsp<br>
&lt;%<br>
&nbsp;&nbsp;&nbsp;&nbsp;String sDBDriver = &quot;COM.ibm.db2.jdbc.app.DB2Driver&quot;;<br>
&nbsp;&nbsp;&nbsp;&nbsp;String sConnStr = &quot;jdbc:db2:faq&quot;;<br>
&nbsp;&nbsp;&nbsp;&nbsp;Connection conn = null;<br>
&nbsp;&nbsp;&nbsp;&nbsp;Statement stmt = null;<br>
&nbsp;&nbsp;&nbsp;&nbsp;ResultSet rs=null; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class.forName(sDBDriver);&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(java.lang.ClassNotFoundException e)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print(&quot;faq(): &quot; + e.getMessage());<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;try{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn = DriverManager.getConnection(sConnStr,&quot;wsdemo&quot;,&quot;wsdemo1&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stmt = conn.createStatement();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}catch(SQLException e){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print(e.toString());<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
%&gt;<br>
<br>
query.jsp<br>
<br>
&lt;%@ page language=&quot;java&quot; import=&quot;java.sql.*&quot;&nbsp;&nbsp;%&gt;<br>
&lt;%@ page contentType=&quot;text/html; charset=gb2312&quot; %&gt;<br>
&lt;%@ include file=&quot;conn.jsp&quot; %&gt;<br>
&lt;%<br>
.......<br>
int pages=0;<br>
int pagesize=10;<br>
ResultSet result = null;<br>
ResultSet rcount = null;<br>
<br>
pages = new Integer(request.getParameter(&quot;pages&quot;)).intValue();<br>
<br>
if (pages&gt;0)<br>
{<br>
<br>
String sql=&quot; state='我不傻'&quot;;<br>
int count=0;<br>
try {<br>
rcount = stmt.executeQuery(&quot;SELECT count(id) as id from user where &quot;+sql);<br>
catch(SQLException ex) {<br>
&nbsp;&nbsp;out.print(&quot;aq.executeQuery: &quot; + ex.getMessage());<br>
&nbsp;&nbsp;}<br>
if(rcount.next())<br>
&nbsp;&nbsp;count = rcount.getInt(&quot;id&quot;);<br>
rcount.close();<br>
<br>
if (count&gt;0)<br>
{ <br>
sql=&quot;select * from user where&nbsp;&nbsp;&quot;+sql;<br>
try {<br>
result = stmt.executeQuery(sql);<br>
&nbsp;&nbsp;&nbsp;&nbsp;} <br>
catch(SQLException ex) {<br>
&nbsp;&nbsp;out.print(&quot;aq.executeQuery: &quot; + ex.getMessage());<br>
&nbsp;&nbsp;}<br>
<br>
int i;<br>
String name;<br>
//&nbsp;&nbsp;result.first();<br>
//&nbsp;&nbsp;result.absolute((pages-1)*pagesize);<br>
//&nbsp;&nbsp;此方法jdbc2.0支持。编译通过,但执行不过,不知是不是跟驱动有关,只好用下面的笨办法。<br>
for(i=1;i&lt;=(pages-1)*pagesize;i++)<br>
&nbsp;&nbsp;result.next();<br>
for(i=1;i&lt;=pagesize;i++) {<br>
&nbsp;&nbsp;if (result.next()) {<br>
&nbsp;&nbsp;name=result.getString(&quot;name&quot;);<br>
&nbsp;&nbsp;out.print(name);<br>
&nbsp;&nbsp;}<br>
result.close();<br>
int n= (int)(count/pagesize);<br>
if (n*pagesize&lt;count) n++;<br>
if (n&gt;1)<br>
 {<br>
 for(i=1;i&lt;=n;i++)<br>
&nbsp;&nbsp;&nbsp;out.print(&quot;&lt;a href=query.jsp?pages=&quot;+i+&quot;&gt;&quot;+i+&quot;&amp;nbsp;&lt;/a&gt;&quot;);<br>
 }<br>
}<br>
}<br>
%&gt;<br>

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -