您的位置:首页 > Web前端 > JavaScript

jsp学习笔记

2008-07-07 08:51 141 查看
<!% %>页面成员变量

大概意思就是所有浏览该页的人都会看到相同的变量达到共享

例:
<!%
int i=0;
%>
<%i++;%> '值为1,再刷一次为2 别的用户打开时为3,只在这个页面,如果服务器不关闭,该变量一直存在

函数调用

<%!
int i=0;
synchronized void test()
{
i++;
}
%>
<%
test();
%>
<%
out.print(i);
%>

注释 //

学习代码:
<%@ page contentType="text/html;charset=GB2312" %>
<%!
int i=0;
%>
<%i++;%>
<%=i%>
<%out.println(i);%>

<%
int x,y,w=100;
String tom=null,jerry="ok";
out.println(tom);
%>

try{}
catch()
{}

catch参数 NumberFormatException e 数值转化错误

Exception ee 通用

Integer.parseInt("") 里面的参数只能为string型

如果为别的型得用(Integer)orders来进行强制转换

request.getRequestURI() /eclipse1/test/t1.jsp 相对路径

application.getRealPath(request.getRequestURI()) 真实路径

E:/zhao/jsp/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/eclipse1/eclipse1/test/t1.jsp

String strDirPath = new File(application.getRealPath(request.getRequestURI())).getParent();

E:/zhao/jsp/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/eclipse1/eclipse1/test

调用access数据库
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*,java.io.*"%>
<html>
<body>
<%
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e){
out.print(e);
}
try {
String strDirPath = new File(application.getRealPath(request.getRequestURI())).getParent();

strDirPath = strDirPath.substring(0, strDirPath.lastIndexOf('//')) + "/";
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + strDirPath + "data/foreign_trade.mdb";

Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from association";
ResultSet rs = stmt.executeQuery(sql);

while (rs.next()) {
out.print(rs.getString(1) + "<br>");
out.print(rs.getString(2) + " ");
}
rs.close();
stmt.close();
conn.close();

} catch (Exception ex) {
out.print(ex);
}
%>
</body>
</html>

conn.asp被包含文件

<%@ page import="java.sql.*,java.io.*"%>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String strPath1 = request.getRealPath(""); //为空应该是主目录
String strPath2 = request.getRequestURI(); //当前程序路径
String strDirPath = strPath1 + strPath2.substring(0,strPath2.lastIndexOf("/"));
strDirPath = strDirPath.replace("//","/") + "/";

String path = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + strDirPath + "data/foreign_trade.mdb";
//out.print(strPath1+"<br>");
//out.print(strPath2 +"<br>");
//out.print(strDirPath +"<br>");
//out.print(path);

Connection conn = DriverManager.getConnection(path);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
%>
new java.util.Date().toLocaleString() 2008-0-1 12:33:22秒的格式

时间格式 2008-1-1 12:33:10
rs.getString("adate") =2008-1-1 12:33:10
rs.getDate("adate")=2008-1-1
rs.getDate("adate").toLocaleString() 2008-1-1 00:00:00

编码问题

get时需要转码post不用但要加入以下代码

<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%request.setCharacterEncoding("gb2312"); %>

get的话要加上

username=new String(usename.getBytes("ISO8859-1"));

<a href="login.jsp?orders=2&username=a&username=b&msg=你好呀">传递测试</a> 这样也可以

response.sendRedirect("login.jsp?msg="+new String("是国".getBytes("gb2312"),"ISO8859-1")); 可以传递啦

return; 相当于asp中的response.end 功能

更新 sql
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

分页 sql

con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

ResultSet中的值只能读取一次 rs.getInt("id") 可以用下面的方式

int id=rs.getInt("id") 然后在程序中调用

我的分页代码
<%
int ordersid=1;
String orders="";
orders=request.getParameter("orders");

if(orders==null)
{
ordersid=1;
}
else
{
ordersid=Integer.parseInt(orders);
}

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
out.print("数据库连接失败!");
}

Connection cn=null;
Statement sql=null;
ResultSet rs=null;

int p=1;
int pagesize=20;
int pagecount=0;
int recordcount=0;
String gjz="";
gjz=request.getParameter("gjz");
if(gjz==null)
{
gjz="";
}
else
{
gjz=new String(gjz.getBytes("ISO8859-1"),"gb2312");
gjz=gjz.replace("'","''");
}

if(request.getParameter("p")==null)
{
p=1;
}
else
{
try{
p=Integer.parseInt(request.getParameter("p"));

}
catch(Exception e)
{
p=1;
}
}

try{

String con="";
if(gjz!="")
{
con="SELECT * FROM proctype WHERE title like '%"+gjz+"%' order by id desc";
}
else
{
con="SELECT * FROM proctype WHERE id>0 order by id desc";
}
cn=DriverManager.getConnection(path);
sql=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery(con);
rs.last();
recordcount=rs.getRow();
pagecount=(recordcount%pagesize==0)?(recordcount/pagesize):(recordcount/pagesize+1);

if(p<=1)
{
p=1;
}
if(p>=pagecount)
{
p=pagecount;
}
int postion;
postion=(p-1)*pagesize+1;
rs.absolute(postion);
%>
<form name="form5">
<input type="button" value="增加" onclick="location.href='proctype.jsp?orders=3'"> <input type="button" value="删除" onclick="checkdel()">
关键字<input type="text" name="gjz" size="13"><input type="submit" value="搜索" name="B1"> 
<input type="button" value="全选" onclick="checkall(document.form1.delid)"> 
 
</form><form name="form1" method="post" action="proctype.jsp?orders=2">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#C0C0C0" width="92%" id="AutoNumber1" height="28">
<tr>
<td width="11%" height="24" bgcolor="#F5F5F5">编号</td>
<td width="49%" height="24" bgcolor="#F5F5F5">主题</td>
<td width="23%" height="24" bgcolor="#F5F5F5">类别</td>
<td width="16%" height="24" bgcolor="#F5F5F5">日期</td>
</tr>
<%
int i=0;
while(i<pagesize && !rs.isAfterLast())
{
int zid=rs.getInt("id");
%>
<tr>
<td width="11%" height="23"> <input type="checkbox" name="delid" value="<%=zid%>" ><%=zid%></td>
<td width="49%" height="23"> <a href="proctype.jsp?orders=5&id=<%=zid%>&p=<%=p%>&gjz=<%=gjz%>"><%=rs.getString("title")%></a></td>
<td width="23%" height="23"> </td>
<td width="16%" height="23"> </td>
</tr>
<%
rs.next();
i=i+1;
}
%>
</table>
<input type="hidden" name="gjz" value="<%=gjz%>">
<input type="hidden" name="p" value="<%=p%>">
</form>
共<%=recordcount %>条,每页显示<%=pagesize%>,<%=p%>/<%=pagecount%><bR><Br>

<a href="proctype.jsp?p=1&gjz=<%=gjz%>">首页</a>
<%if((p+1)<=pagecount){%><a href="proctype.jsp?p=<%=p+1%>&gjz=<%=gjz%>">下一页</a><%}%>
<%if((p-1)>=1){ %><a href="proctype.jsp?p=<%=p-1%>&gjz=<%=gjz%>">上一页</a><%}%>
<a href="proctype.jsp?p=<%=pagecount%>&gjz=<%=gjz%>">尾页</a>

<%
}
catch(Exception e){}

%>

if else语法的简写

gjz=(gjz==null)?(""):(new String(gjz.getBytes("ISO8859-1"),"gb2312"));

如何获取checkbox的值

String[] delid=request.getParameterValues("delid");

if(delid!=null)
{
for(int i=0;i<delid.length;i++)
{
out.print(delid[i]);
}

}

有很多地方需要给变量附初值

String title=""; //这种情况下不附初值就会出错
try{
cn=DriverManager.getConnection(path);
sql=cn.createStatement();
rs=sql.executeQuery("select title from proctype where id="+id);
if(rs.next())
{
title=rs.getString("title");
//out.print(title);
}

}
catch(Exception e)
{}

out.print(title); ?//这里出错

The local variable title may not have been initialized 错误提示

这样取值最好传为int型
int fid;
fid=request.getParameter("fid")
<select name="fid">
<option value="0"></option>

上传文件
jspsmartupload.jar

考到tomcat lib文件夹下
或考到eliese 的WebContent/WEB-INF/lib下就可使用
<%@ page import="java.io.File,com.jspsmart.upload.*"%>
<%
SmartUpload su=new SmartUpload();
su.initialize(pageContext);
//su.setMaxFileSize(50000);
//su.setAllowedFilesList("doc,txt");
su.upload();
int count=su.save("/upload/");
out.println("成功上传"+count+"个文件");
%>
<%
for(int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file=su.getFiles().getFile(i); //com.jspsmart.upload.File file = (com.jspsmart.upload.File)su.getFiles().getFile(i); 如果不兼容这么写
if(file.isMissing()) continue;
%>
<%=file.getFieldName()%>
<%=file.getSize()%>
<%=file.getFileName()%>
<%=file.getFileExt()%>
<%
}%>

<textarea name="content" style="display:none"></textarea> 使用ewebedit时双隐号解决问题

<%
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.util.Date currentTime = new java.util.Date();//得到当前系统时间

String str_date1 = formatter.format(currentTime); //将日期时间格式化
String str_date2 = currentTime.toString(); //将Date型日期时间转换成字符串形式
%>

<%@ page import="java.util.Date"%><% Date myDate = new Date(); int thisYear = myDate.getYear() + 1900;//thisYear = 2003 int thisMonth = myDate.getMonth() + 1;//thisMonth = 5 int thisDate = myDate.getDate();//thisDate = 30%>

right函数
right(str,2) vb中的
str.substring(str.length()-2)

String year1=(d.getYear()+1900)+""; 数值型换为字符型就+""就可以
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: