大学生论坛's Archiver

21972 发表于 2007-9-14 20:24

JSP时尚百例(连载)

例1:
<%@ page language="java" %>
<%@ page c%>
<html>
<head>
<title>Hello World</title>
<meta http-equiv="Content-Type" c>
</head>
<body bgcolor="#FFFFFF">
<center>
<font size= 5 color=blue>多种字体大小显示</font>
<center>
<br>
<hr>
<br>
<div align="center">
<%
//控制文字大小的循环语句
for( int i=1; i<=6; i++ )
   out.println( "<h" + i + ">Hello World!</h" + i + ">" );
%>
</div>
</body>
</html>

21972 发表于 2007-9-14 20:25

<%@ page c import="java.util.*" %>
<HTML>
<HEAD>
<META NAME="GENERATOR" C>
<META HTTP-EQUIV="Content-Type" c>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
<center><font color=blue size=10 face="隶书">
<%
Date today = new Date();
int hours = today.getHours();
int minute = today.getMinutes();
if(hours>=0 && hours<12){
  out.println("早上好!");
}else{
  out.println("下午好!");
}
String[] weekdays = {"日","一","二","三","四","五","六"};
out.println("\n今天是" + (today.getYear()+1900) + "年" + (today.getMonth()+1) + "月" + (today.getDate()) + "日星期" + weekdays[today.getDay()]);
%>
</font></center>
</BODY>
</HTML>

21972 发表于 2007-9-14 20:26

<%@ page c %>
<%@ page language="java" %>
<%
  Integer count = null;
  //同步处理
  synchronized (application)
  {
    //从内存当中读取访问量
    count = (Integer) application.getAttribute("basic.counter");
    if (count == null)
    count = new Integer(0);
    count = new Integer(count.intValue() + 1);
    //将访问量保存到内存当中
    application.setAttribute("basic.counter", count);
  }
%>
<html>
<head>
<title>简单计数器</title>
</head>
<body>
<center>
<font size=10 color=blue>简单计数器</font>
<br>
<hr>
<br>
<font size=5 color=blue>您好!您是本站的第 <%= count %> 位客人</font>
</center>
</body>
</html>

21972 发表于 2007-9-14 20:28

<%@ page c %>
<%@ page language="java" %>
<!-- 设置页面属性 -->
<%@ page info="这就是我们预设的网页属性, 这里我们可以学习到page属性的设置与使用" %>
<HTML>
<HEAD>
<TITLE>设置页面属性</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 10 COLOR = BLUE>设置页面属性</FONT>
</CENTER>
<br>
<HR>
<br>
<!--获得设置的页面属性-->
<FONT SIZE = 5 COLOR = BLUE>
<%= getServletInfo() %>
</FONT>
</BODY>
</HTML>

21972 发表于 2007-9-14 20:30

<%@ page c %>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>加载文件</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 10 COLOR = blue>加载文件</FONT>
</CENTER>
<br>
<HR>
<br>
<center>
<font size=5 color=blue>
<!-- 加载文件 htminsert.html-->
<%@ include file="htminsert.html" %>
</font>
</center>
</BODY>
</HTML>

21972 发表于 2007-9-14 20:33

例6:
Include.jsp:
<%@ page c %>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>动态加载文件</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 10 COLOR = blue>动态加载文件</FONT>
</CENTER>
<br>
<HR>
<br>
<font size=5>
<!-- 用jsp:include指令动态加载文件 -->
<jsp:include page="jspInsert.jsp">
<jsp:param name="name" value="jsp:include"/>
<jsp:param name="file" value="jspInsert.jsp"/>
</jsp:include>
</font>
</BODY>
</HTML>

jspInsert.jsp:
<HTML>
<BODY>
您好!这里我们用到了
<Font Color=Blue>
<%= request.getParameter("name")%>
</Font>
指令!
<BR>
我们在这里加载了文件:
<Font Color=Blue>
<%= request.getParameter("file")%>
</Font>。<br>
</HTML>
</BODY>

21972 发表于 2007-9-14 20:35

例7:
APPLET.JSP:
<%@ page c%>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>利用<applet>载入applet</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>利用&ltapplet&gt载入applet</FONT>
</CENTER>
<BR>
<HR>
<BR>
<CENTER>
<!-- 载入applet -->
<applet code="HelloWorld.class" height="50" width="320" >
  <param name="name" value="Applet Demo">
</applet>
</CENTER>
<BR>
</BODY>
</HTML>

HelloWorld.java:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet
{
    String name;
    public void init()
    {
     name = getParameter("name");
    }
    public void paint(Graphics g)
    {
        g.drawString(" This is " + name + "!", 60, 25);
    }
}
注意请将HelloWorld.java编译成HelloWorld.class后使用

21972 发表于 2007-9-16 16:42

例9:
getBrowser.jsp:
<%@ page c %>
<%@ page language="java" %>
<%@ page import="Browser.*"%>
<HTML>
<HEAD>
<TITLE>Get Browser</TITLE>
</HEAD>
<BODY>
<center>
<font size=5 color=blue>获得客户端浏览器信息</font>
<center>
<%
Browser eins = new Browser(request, session);
%>
<br>
<hr>
<br>
<font size=4 face="隶书" color=blue>
您使用的浏览器是:<%=eins.getName()%>
</font>
<br>
<font size=r face="隶书" color=blue>
它是<%=eins.getCompany()%>公司的<%=eins.getVersion()%>版
</font>
<br>
<font size=4 face="隶书" color=blue>
您使用的操作系统是:<%=eins.getOs()%>
</font>
<br>
</BODY>
</HTML>
Browser.java:
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Browser extends HttpServlet
{
    protected HttpServletRequest request;
    protected HttpSession session;
    protected String userAgent;
    protected String company; // Firmenname des Herstellers
    protected String name; // Bezeichnung des Browsers
    protected String version; // Version
    protected String os; // Betriebssystem
    private Hashtable supportedLanguages; // Untersttzte Sprachen
    public Browser(HttpServletRequest request, HttpSession session)
    {
        this.initialize();
        this.request = request;
        this.session = session;
        this.setUserAgent(this.request.getHeader("User-Agent"));
        this.setCompany();
        this.setName();
        this.setVersion();
        this.setOs();
    }
    public void initialize()
    {
        this.supportedLanguages = new Hashtable(2);
        this.supportedLanguages.put("en", "");
        this.supportedLanguages.put("de", "");
    }
    public void setUserAgent(String httpUserAgent)
    {
        this.userAgent = httpUserAgent.toLowerCase();
    }
   
// get company's name of browser
    private void setCompany()
    {
        if (this.userAgent.indexOf("msie") > -1)
        {
            this.company = "Microsoft";
        }
        else if (this.userAgent.indexOf("opera") > -1)
        {
            this.company = "Opera Software";
        }
        else if (this.userAgent.indexOf("mozilla") > -1)
        {
        this.company = "Netscape Communications";
        }
        else
        {
        this.company = "unknown";
        }
    }
    public String getCompany()
    {
        return this.company;
    }
// get name of browser
    private void setName()
    {
        if (this.company == "Microsoft")
        {
            this.name = "Microsoft Internet Explorer";
        }
        else if (this.company == "Netscape Communications")
        {
            this.name = "Netscape Navigator";
        }
        else if (this.company == "Operasoftware")
        {
            this.name = "Operasoftware Opera";
        }
        else
        {
            this.name = "unknown";
        }
    }
    public String getName()
    {
        return this.name;
    }
//get version of browser
    private void setVersion()
    {
        int tmpPos;
        String tmpString;
        if (this.company == "Microsoft")
        {
            String str = this.userAgent.substring(this.userAgent.indexOf("msie") + 5);
            this.version = str.substring(0, str.indexOf(";"));
        }
        else
        {
            tmpString = (this.userAgent.substring(tmpPos = (this.userAgent.indexOf("/")) + 1, tmpPos + this.userAgent.indexOf(" "))).trim();
            this.version = tmpString.substring(0, tmpString.indexOf(" "));
        }
    }
    public String getVersion()
    {
        return this.version;
    }
// get Os of your computer
    private void setOs()
    {
        if (this.userAgent.indexOf("win") > -1)
        {
            if (this.userAgent.indexOf("windows 95") > -1 || this.userAgent.indexOf("win95") > -1)
            {
                this.os = "Windows 95";
            }
            if (this.userAgent.indexOf("windows 98") > -1 || this.userAgent.indexOf("win98") > -1)
            {
                this.os = "Windows 98";
            }
            if (this.userAgent.indexOf("windows nt") > -1 || this.userAgent.indexOf("winnt") > -1)
            {
                this.os = "Windows NT";
            }
            if (this.userAgent.indexOf("win16") > -1 || this.userAgent.indexOf("windows 3.") > -1)
            {
                this.os = "Windows 3.x";
            }
        }
    }
    public String getOs()
    {
        return this.os;
    }
}

21972 发表于 2007-9-16 16:43

例10:
ERR.JSP
<%@ page c %>
<%@ page language="java" %>
<%@ page import="java.io.*" %>
<HTML>
<HEAD>
<TITLE>错误检测</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>错误检测</FONT>
</CENTER>
<BR>
<HR>
<BR>
<CENTER>
<FONT SIZE=4 COLOR=RED>
<%
int a = 10, b = 0, c;
try
{
c = a / b; // 这里产生除以零的错误
}
catch(Exception e)
{
out.println(e.toString());
}
finally
{
out.println("<BR>这里产生了除以零的错误");
}
%>
</CENTER>
</FONT>
</BODY>
</HTML>

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.