JSP&Servlet--JSP

来源:互联网 发布:巴克利职业生涯数据 编辑:程序博客网 时间:2024/06/07 01:48

1.JSP简介

JSP就是一个Servlet,JSP--Java ServerPages,拥有Servet的特性与优点,直接在HTML中内嵌JSP代码。

JSP程序有JSPEngine先将它转换成Servet代码,接着将它编译成类文件载入执行。只有当客户端第一次请求JSP时,才需要将其转换,编译。

优点:优良的性能由于CGI,PHP,ASP

平台无关性:操作系统无关,Web服务器无关

可扩展性:tag的扩展机制,简化页面开发

 例如:Hellowordd.jsp

<%@page import="java.util.Date" %><html><head></head><body><%    out.println("HelloWorld!");    out.println(new Date());%></body></html>

会在网页上显示Helloworld+打开的时间

JSP的运行机制:生成.jsp文件后会在Workspaces\MyEclipse Professional2014\.metadata\.me_tcat\work\Catalina\localhost\TestHTTP\org\apache\jsp文件夹下生成一个和该Jsp同名的.java文件,即是jsp要先转换成servlet的java的语言。

2.JSP编程-基本语法

Declaration

基本语法:<%!    %>

说明:在此声明的变量,方法都会被保留成唯一的一份,直到JSP程序停止执行

例如:

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head></head><body><h1>JSP Delarations</h1><%! int a = 0; %><% int a2 = 0; %>//其中不能声明方法<h2>Accesses to page server reboot:    <%= ++a %>//输出等号后面的内容,=后面必须是字符串变量或者可以被转换成字符串的表达式,只有一行    <br>    <%= ++a2 %></h2></body></html>

网页上会出现

JSP Delarations

Accesses to page server reboot:1

1

当刷新是第一个数字会增加,第二个不会。

原因:a是成员变量(加!,只被初始化一次,只会产生一个对象),a2是局部变量

Scriptlet

基本语法:<%程序代码区%>可以放入任何的java程序代码

注释格式:<%--   --%><%//   %><%%>

举例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Color Testing</title></head><!--html注释--><%--注释aaaaaaaa --%><%    //注释    String bgColor = request.getParameter("bgColor");    boolean hasExplicitColor;    if (bgColor != null) {        hasExplicitColor = true;    } else {        hasExplicitColor = false;        bgColor = "white";    }%><body bgcolor="<%= bgColor %>"><h2 align="center">Color Testing</h2><%    if (hasExplicitColor) {        out.println("you supplied an explicit backroundcolor of " +                bgColor + ".");    } else {        out.println("using default bancground color ofWHITE." +                "supply the bgColor requestattribute to try" +                "a standard color,an RRGGBBvalue,or to see" +                "if your browser supports X11color names.");    }%></body></html>

 网页默认是白色背景,在网页地址后添加?bgColor=red,背景会变成红色

关于<%= %>

<body><h2>JSP</h2><ul>    <li>Current time:<%= new java.util.Date() %>>    <li>Your hostname:<%= request.getRemoteHost() %>    <li>Your session ID:<%= session.getId() %>    <li>The <code>testParam</code> from parameter:            <%= request.getParameter("testParam") %></ul></body>

网页的显示内容:

Current time:Tue May 12 22:20:40 GMT+08:00 2015>

Your hostname:0:0:0:0:0:0:0:1

Your session ID:FF8F6F5E7396EDE02AC38A23D3E86ECA

The testParam from parameter: null

在地址后输入?textParam=  ,null就会变成  。如果  中有空格,在网址中会自动转换成,输出不会影响。
Directive(编译期间)

Directive(编译指令)相当于在编译期间的命令。

格式<%@Directive 属性="属性值"%>

常见的Directive:page   include   taglib

Driective-page

指明与JSP Container的沟通方式,也就是编译期间指明页面的一些特点

基本格式:

<%@pagelanguage="java"|     //目前只能是java,可以不写默认java

extends="clsaaName"|     //从哪继承

import="inportList"|   //引入进来那些包,那些类        ^

buffer="none|kbsize"|      none表示不缓冲,默认是8k

session="true|false"|      是否可以使用session,默认是true

false"|    缓冲器是否自动清楚,默认true

isThreadSafe="true|false"|不需要指定

info="infoText"|  一些相关的描述性的信息,不常用

errorPage="errorPageUrl"|   页面出错后,会显示那个页面   ^

isErrorPage="true|false"   当前页面是不是一个出错后要显示的页面   ^

contenType="contentTypeInfo"   text/html;charset=gb2312    ^

%>

例子程序:

<%@page import="java.util.*,java.text.*" %><%@page contentType="text/html;charset=gb2312" %><%= new Date() %><%    out.println("你好");%>

输出:时间   你好

 

<%@page errorPage="ErrPage.jsp"%><%    String s = "123wfd";    int i = Integer.parseInt(s);    out.println("s=" + s + " i=" + i);%>

打开此网页时会报错。

所以在该代码前加:<%@page errorPage="ErrPage.jsp"%>

在写一个ErrPage.jap文件

<%@ page isErrorPage="true" contentType="text/html;UTF-8" %><html><body text="red">错误信息:<%= exception.getMessage() %></body></html>

这样就会正常挑战到ErrPage.jsp网页并显示报错信息。

Driective-include

将指定的JSP程序或HTML文件包含进来

格式:<%@include file="fileURL"%>,JSPEngine会在JSP程序的转换时期先把file属性设定的文件包含进来,然后开始执行转换及编译的工作。不能想fileURL中传递参数,不能abc.jsp?user=a

例:

<%@page contentType="text/html;charset=gb2312" %><html><!-- ********TestBar.jsp******** --><head>    <title>        TestBar.jsp    </title></head><body><table width="100%">    <tr>        <td>            <%@include file="TitleBar.jsp"%>        </td>    </tr>    <tr>        <td><% out.println("<p>这是用户显示区</p>"); %></td>    </tr></table></body></html>


接下来是:

<table><!-- ********TitleBar.jsp******** -->    <tr>        <td>        </td>        <td>            <% out.println("Hi: " + request.getParameter("user")); %>        </td>    </tr></table>

界面会输出:Hi:null

这是用户显示区,可以写?user=   ,对user进行赋值。

前面的ErrorPage(运行期间包含)是运行到这句话的时候,才把该页面包含进来,而Include是先包含进来,然后在然后在编译(编译期间包含)

Action(运行期间)

Action(动作指令)在运行期间的命令,如jsp:useBean(jsp:setProperty;jsp:getProperty),jsp:include,jsp:forward(jsp:param),jsp:plugin

 jsp:include/jsp:param

用于动态包含JSP程序或HTML文件等,除非这个指令会被执行到,否则它不会被Tomcat等JSP Engine编译。

格式:

<jsp:include page="  "flush="true"/>  可以在""内写URL地址,也可以接?参数值,include不能

or <jsp:include page=" " flush="true">

<jsp:param name=" " value=" "/>

</jsp:include>jsp:param是用来设定include 文件时的参数和对应的值。

和编译指令include的区别:Include编译指令是在JSP程序的转换时期将file属性所指定的程序内容嵌入,然后在编译执行;而include指令在转换时期是不会被编译的,只有在客户请求时期如果被执行到才会被动态的编译载入。include不能带参数,而《jsp:include可以

例:

<html><head>    <title>include test</title></head><body bgcolor="white"><font color="red">    The current date and time are    <%@ include file="date.jsp" %>    <jsp:include page="date.jsp" flush="true"/></font></body></html>

date.jsp:

<%@page import="java.util.*"%><%= (new java.util.Date()).toLocaleString() %>

运行结果:

The current date and time are 2015-5-2520:07:05 2015-5-25 20:07:05

 

一个计算器的例子:

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Compute</title></head><body bgcolor="#ffffff"><div align="center">    <p>选择要做的运算        <input type="radio" name="compute" value="division" checked>        除法        <input type="radio" name="compute" value="multiplication">        乘法</p>    <p>被除数(被乘数)        <input type="text" name="value1">        除数(乘数)        <input type="text" name="value2">    </p>    <p>        <input type="submit" name="Submit" value="计算结果">    </p></div></body></html>

然后是compute.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%    String value1 = request.getParameter("value1");    String value2 = request.getParameter("value2");%><% if (request.getParameter("compute").equals("division")) {%><jsp:include page="divide.jsp" flush="true">    <jsp:param value="<%=value1%>" name="v1"/>    <jsp:param value="<%=value2%>" name="v2"/></jsp:include><% } else { %><%@include file="multiply.jsp" %><% } %>

然后是除法的jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Divide</title>    <meta http-equiv="content-type" content="text/html;charset=gb2312"></head><body bgcolor="#ffffff"><center>    <h1>        <%            try {                float dividend = Float.parseFloat(request.getParameter("v1"));                float divisor = Float.parseFloat(request.getParameter("v2"));                double result = dividend / divisor;        %>        <%=result%>        <%            } catch (Exception e) {                out.println("不合法的被乘数或除数!");            }        %>    </h1></center></body></html>

结果打开页面是一个可以选择除法或乘法运算的网页,具体为了区分2种include。


0 0
原创粉丝点击