JSP 学习手记
来源:互联网 发布:苹果6的蜂窝数据打不开 编辑:程序博客网 时间:2024/05/21 13:54
JSP
1.引入其他包和设置Html参数字体<%@ page lanage="java"%>
<%@ page import="java.io.*","java.util.Date"%>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page buffer="24kb"%>
<返回>
2.包含文件,同时带设置参数
<jsp:param name="userName" value="kes2000">
<%@ include file="filename"%>
String a=request.getParameter("userName");
out.print(a);
<返回>
3.使用页面跳转并带参数
<jsp:forward page="result.jsp">
<jsp:param name="number" value="10">
</jsp:forward>
<返回>
4.使用Applet
<jsp:plugin type="applet" code="B.class" codebase="/myfile" jreversion="1.2" width="200" height="260" > </jsp:plugin>
<返回>
5. 转为为数字
number=Double.parseDouble(textContent);
<返回>
6. 读取中文
String str=request.getParameter("girl");
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
<返回>
7.Request对象(读取页面或者Form参数)
request.getProtocol() getServletPath() getContentLength() getMethod() getHeader(String s) getRemoteAddr() getRemoteHost() getServerName()
request.setContentType("application/msword;charset=GB2312");
<返回>
8.Response对象和out对象(输出值)
response.sendRedirect("example.jsp"); setStatus(500); setStatus(200);
out.print("hello");
<返回>
9.Session 和Application
String s=session.getId(); setAttribute(key,value) getAttribute(key) removeAttribute(key) invalidate() getCreationTime() getLastAccessedTime()
<返回>
10.JSP连接数据库
一、jsp连接Oracle8/8i/9i数据库(用thin模式) <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getInt(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> 二、jsp连接Sql Server7.0/2000数据库 <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getInt(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> 三、jsp连接DB2数据库 <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getInt(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
四、jsp连接Informix数据库 <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getInt(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> 五、jsp连接Sybase数据库 <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
//tsdata为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getInt(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> 六、jsp连接MySQL数据库 <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getInt(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> 七、jsp连接PostgreSQL数据库 <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/soft"
//soft为你的数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getInt(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> <返回>
11.使用同步连接
if(conn!=null){
synchronized(conn){
//do something
}
}
<返回>
12.使用beans Jsp代码 Bean代码 <jsp:useBean id="jsp2BeanId" scope="session" class="myjsp.Jsp2Bean" />
<jsp:setProperty name="jsp2BeanId" property="*" />
<jsp:getProperty name="jsp2BeanId" property="sample" />
<@ import="tom.jiafei.primNumber"> public class Jsp2Bean {
private String sample = "Start value";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
} <返回>
- JSP 学习手记
- JSP调用存储过程手记
- Tomcat JSP配置实例手记
- Tomcat JSP配置实例手记
- Hibernate学习手记1
- Delphi学习手记
- Struts学习手记
- Hibernate学习手记(一)
- EAS 5.2 学习手记
- Hibernate学习手记(一)
- ace 学习手记 20050829
- sql server2000学习手记
- ireport & jasperreports 学习手记
- JavaScript 学习手记
- ASP学习手记
- ASP.NET 学习手记
- Samba 学习手记(1)
- Samba学习手记(2)
- 线性表的顺序实现
- Python:缺失的访问控制?
- ASP学习手记
- ASP.NET 学习手记
- 请教各位高手LINUX9.0安装问题。非常急。谢谢了。
- JSP 学习手记
- [翻译] Effective C++, 3rd Edition, 附录 A. 超越 Effective C++
- 用VB写ASP的组件
- ubuntu编译内核以后生成splash
- 线程 应用程序域 进程 比较区别
- 页面验证码程序(C#)
- ATI非官方驱动安装
- 简单的控制台程序
- RIA 开发感想(二)—— Flash VS Ajax