01---jsp基本语法02(page指令)

1、主要指令有:
 contentType:定义jsp字符的编码和页面响应的MIME类型,如果是中文的话则使用如下:
   contentType="text/html;charset=gbk";
 errorPage:定义此页面出错时要跳转的显示页,例如:errorPage="error.jsp"
 extends:   主要定义此jsp页面产生的serlet是从哪个父类扩展而来,例:extends=“父类名称


 import:   此jsp页面要导入哪几个操作包,例如:import="java.util.*";
 isErrorPage:可以设置true或者false,表示此页面是否为出错的处理也,如果设置成true,
   则errorPage指定的页面出错时才能跳转到此页面进行错误处理;如果设置成

false,则
   无法处理;
 pageEncoding:jsp页面的字符编码---pageEncoding="utf-8";

2、设置MIME:
 <%@ page  language="java" import="java.util.*" contentType="text/html"

pageEncoding="utf-8"%>
 在jsp中,如果pageEncoding存在,那么jsp的编码将由pageEncoding决定,而如果不存在
 才又charset来决定,如果都不存在,则使用ISO8859-1;
 如果一个jsp文件只是需要按照text/html风格显示的话,只需要指定一个pageEncoding
 即可;

3、错误页的设置:
 ·指定错误出现时的跳转页,errorPage="error.jsp"
 ·错误处理页必须有明确的标识,通过isErrorPage属性指定,isErrorPage="true";

 在实际应用中,常见的错误有404、500和NullPointerException等,要想集中处理这些错误,
 我们得修改web.xml文件;
    <error-page>
      <error-code>500</error-code>
      <location>error.jsp</location>
    </error-page>
    <error-page>
      <error-code>404</error-code>
      <location>error.jsp</location>
    </error-page>
    <error-page>
      <exception-type>java.lang.NullPointerException</exception-

type>
      <location>error.jsp</location>
    </error-page>

4、数据库的连接:
 既然page指令中存在了import属性,所以导包就可以正确的使用了 ,但是需要说明的是,在整

个page指令
 的属性中,只有import可以多次使用,其他的属性只能设置一次;
 既然可以出现,就是用JDBC完成以下操作;

 ·在mysql数据库中创建表;
  mysql -uroot -p123
  use lid;
  drop table emp;
  create table emp(
   empno  Int   auto_increment primary key,
   ename  varchar(10) ,
   job  varchar(10) ,
   hiredate Date,
   sal  float(7,2)
  );
 insert into emp(empno,ename,job,hiredate,sal) values(1024,'lid','销售','1986-06-

06',12.20);
 insert into emp(empno,ename,job,hiredate,sal) values(1034,'yuj','老板','1988-06-

06',13.20);
 insert into emp(empno,ename,job,hiredate,sal) values(1044,'ly','销售','1989-06-

06',14.20);
 希望可以通过jsp进行列表的输出。
 但是需要特别注意的是,mysql数据库连接的时候需要额外的驱动;
 将驱动程序拷贝到tomcat安装目录的lib中去,服务器必须重新启动;
  <%@ page  contentType="text/html"  pageEncoding="gbk"%>
  <%@ page import="java.sql.*"%>
  <html>
   <head>
    <title>这是标题</title>
   </head>
   <body>
    <%!
     public static final String

DBDRIVER="org.gjt.mm.mysql.Driver";
     public static final String

DBURL="jdbc:mysql://localhost:3306/lid";
     public static final String DBUSER="root";
     public static final String DBPASSWORD="123";
     
    %>
    <%
     Connection conn=null;
     PreparedStatement pstmt=null;
     ResultSet rs=null;
     
     try{
      Class.forName(DBDRIVER);
      conn=DriverManager.getConnection

(DBURL,DBUSER,DBPASSWORD);
      String sql="select

empno,ename,job,hiredate,sal from emp";
      pstmt=conn.prepareStatement(sql);  

  
      rs=pstmt.executeQuery();
      
    %>
     <center>
      <table border="1" width="90%" >
       <tr>
        <td>雇员编号</td>
        <td>雇员姓名</td>
        <td>雇员工作</td>
        <td>雇员日期</td>
        <td>雇员工资</td>
       </tr>
     <%
       while(rs.next()){
        int empno=rs.getInt(1);
        String ename=rs.getString

(2);
        String job=rs.getString(3);
        java.util.Date

date=rs.getDate(4);
        float sal=rs.getFloat(5);
        
     %>
      <tr>
       <td><%=empno%></td>
       <td><%=ename%></td>
       <td><%=job%></td>
       <td><%=date%></td>
       <td><%=sal%></td>
      </tr>
     <%
       }
     %>
      </table>
     </center>
    <%
     }catch(Exception e){
      System.out.println(e);
      }
     finally{
      rs.close();
      pstmt.close();
      conn.close();
      }
    %>
    
   </body>
  </html>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bzuld

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值