加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

几则JSP入门知识汇总

发布时间:2023-05-27 13:48:59 所属栏目:教程 来源:
导读:从去年9月份,我就开始着手学jsp,以前也只有一点程序的意识,一路上摸索过来,经过了很多磨难,终于有一天,我就像一个旱鸭子学会游泳一样,心里无比高兴,熬了几天夜,终于写成了这个纯jsp的文章发布程序。

相信
从去年9月份,我就开始着手学jsp,以前也只有一点程序的意识,一路上摸索过来,经过了很多磨难,终于有一天,我就像一个旱鸭子学会游泳一样,心里无比高兴,熬了几天夜,终于写成了这个纯jsp的文章发布程序。
 
相信下面的几则小知识对向我这样水平的菜鸟有一定的帮助!
 
==============================================================================
 
1.传递表单参数:
 
string name = new string(request.getparameter("name"));
 
2.数据库连接:
 
~~mysql
 
//设置数据库的url
 
string url = "jdbc:mysql://localhost:3306/jspsky";
 
try
 
//加载驱动程序
 
class.forname("org.gjt.mm.mysql.driver").newinstance();
 
//建立连接
 
java.sql.connection connection = java.sql.drivermanager.getconnection(url);
 
java.sql.statement statement = connection.createstatement();
 
//sql语句
 
string sqlstringi ="insert into commu(name,tel,mobile,oicq,email)values(‘"+name+"',‘"+tel+"',‘"+mobile+"',‘"+oicq+"',‘"+email+"')";
 
//运行sql语句,并建立结果集
 
java.sql.resultset rsi = statement.executequery(sqlstringi);
 
//在屏幕上输出库中的内容
 
while(rss.next())
 
{
 
string a_name = rss.getstring(1);
 
out.println(a_name);
 
{}
 
//关闭连接
 
connection.close();
 
}
 
//捕捉异常
 
catch(java.sql.sqlexception e)
 
out.println(e.getmessage());
 
{}
 
catch(classnotfoundexception e)
 
out.println(e.getmessage());
 
{}

~~db2
 
//定义数据库的url
 
string url = "jdbc:db2:portal";
 
try
 
//加载驱动程序
 
class.forname("com.ibm.db2.jdbc.app.db2driver");
 
//建立连接,
 
java.sql.connection connection = java.sql.drivermanager.getconnection(url,"user","password");
 
java.sql.statement statement = connection.createstatement();
 
//sql语句
 
string sqlstring = "select * from client";
 
//执行sql语句
 
java.sql.resultset rs = statement.executequery(sqlstring);
 
//在屏幕上显示所连表中的内容
 
while(rs.next())
 
{
 
string name = rs.getstring(1);
 
out.println(name);
 
{}
 
//关闭连接
 
connection.close();
 
}
 
//捕捉异常
 
catch(java.sql.sqlexception e)
 
out.println(e.getmessage());
 
{}
 
catch(classnotfoundexception e)
 
out.println(e.getmessage());
 
{}
 
3.文件操作
 
~~将一个字符串写到一个指定的文件中,如果该文件不存在,则新建一个文件,并完成写入;如果存在,则用此字符串覆盖原文件的所有内容
 
import java.io.*;
 
string str = "print me 雪峰!";
 
//定义好打印的目标文件名
 
//取得当前主机存放web页面的绝对路径
 
string hostdir = system.getproperty("user.dir");
 
//取得当前主机所采用的路径分隔符
 
string filebar = system.getproperty("file.separator");
 
//书写完整的目标文件存放路径
 
string nameoffile=hostdir+filebar+"test.html";
 
try
 
//实例化一个文件输出流对象
 
fileoutputstream afile = new fileoutputstream(nameoffile);
 
//将文件输出流,创建一个打印输出流对象
 
printwriter pw = new printwriter(afile);
 
pw.println(str);
 
//clean up
 
pw.close();
 
{}
 
catch(ioexception e)
 
out.println(e.getmessage());
 
{}
 
~~列出指定目录下的文件列表
 
import java.io.*;
 
string cdur = system.getproperty("user.dir");
 
string filebar = system.getproperty("file.separator");
 
string mydir =cdur+filebar+"doc"+filebar+"jspsky";
 
file my = new file(mydir);
 
string d[] = my.list();
 
int i;
 
int l=d.length;
 
for(i=0;i out.print(d[i]);
 
{}
 
4.计数器
 
integer count = null;
 
synchronized (application)
 
count =(integer) application.getattribute("d");
 
if (count ==null)
 
count =new integer("0");
 
count = new integer(count.intvalue()+1);
 
application.setattribute("d",count);
 
{}
 
out.println(count);
 
// 首先定义一个整形对象,并初始化为:null,
 
// 取回application对像的属性d的值,并强制转化为整形对象,赋给count
 
// 判断count是否为空,为空时,将o赋给count对象,
 
// 否则,通过count。intvalue()方法,实现count对象加1,并赋值给count
 
// 最后,将count对象保存在application对象的d变量中。
 
+++++++++++++++++++
 
下一步学习重点
 
文件的删除
 
文件内容的修改
 
图片的上传
 
邮件列表
 
javabeans
 
ejb
 
xml
 
javascript
 
对数据库的操作及维护
 
了解商业项目开发流程
 
实例练习
 
留言板
 
聊天室
 
发送邮件
 
新闻系统
 
截取网页内容
 
购物车

多做练习,在实践中不断熟悉java包的使用!
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!