一:编码方式和MIME
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
二:错误页面
<%@ page errorPage="myErrorPage.jsp"%><!-- 自定义的错误页面中要用page指令声明 isErrotPage="true" -->
自定义页面中
<%@ page isErrorPage="true"%>
<%
response.setStatus(200);
%>
三:修改客户端下载jsp页面的名称
<%
response.setHeader("Content-Disposition","attchement;filename=XXX.jsp");/*修改被下载jsp文件的名称*/
%>
四:错误的集中处理
<!-- 集中处理错误:虚拟目录\WEB-INF\web.xml 中加入error-apge标签 -->
<welcome-file-list>
<welcome-file>main.htm</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/myErrorPage.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/myErrorPage.jsp</location>
</error-page>
</web-app>
五:JDBC连接Oracle11g数据库
Java连接Oracle数据库的范例详见:http://blog.csdn.net/ruantao1989/article/details/8012005
能够用import导入sql包之后,操作是大同小异的
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.Date"%>
<html>
<head>
<title> JSP_JDBC_Demo </title>
</head>
<body>
<%!
private static final String DBDriver = "oracle.jdbc.driver.OracleDriver";//驱动
private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:ORCL";//URL命名规则:jdbc:oracle:thin:@IP地址:端口号:数据库实例名
private static final String DBUser = "scott";
private static final String DBPassWord = "890307";
%>
<%
Connection con = null;
Statement st = null;
ResultSet res = null;
try {
//连接
Class.forName(DBDriver);//加载数据库驱动
con = DriverManager.getConnection(DBURL, DBUser, DBPassWord);//连接
st = con.createStatement();
%>
<h2>已连接 <%=con%></h2>
<%
res = st.executeQuery("SELECT pid,name,age,birthday,sex FROM person ");
while(res.next())
{
int p = res.getInt("pid");
String n = res.getString("name");
int a = res.getInt("age");
Date b = res.getDate("birthday");
String s = res.getString("sex");
%>
<h3><%=p%> <%=n%> <%=a%> <%=b%> <%=s%></h3>
<%
}
} catch (Exception e) {
System.out.println(e);
}finally{
//四:关闭连接
res.close();//依次关闭
st.close();
con.close();
}
%>
</body>
</html>