2008年10月23日
上海交通大学java视频教程共31讲
If you are interested in java, you can spend time learning about it
public class TestPrintf{
public static void main(String args[]){
System.out.printf("%+8.3f",3.14);
System.out.println();
System.out.printf("%+-8.3f\n",3.14);
System.out.printf("%08.3f\n",3.14);
System.out.printf("%(8.3f\n",-3.14);
System.out.printf("%,f\n",2356.34);
System.out.printf("%x\n",0x4a3b);
System.out.printf("%#x\n",0x4a3b);
System.out.println("------------");
System.out.printf("你好:%s,%3d岁,工资%-7.2f\n","张三",38,15000.00);
System.out.printf("你好:%1$s,%2$3d岁,%2$#x岁\n","张三",38);
System.out.printf("%3d,%#<x",38);
}
}
摘要: Integer[] a = {3,25,12,79,48};
System.out.println(a);
System.out.println(Arrays.toStrin... 阅读全文
摘要: /** *//**
* 通过这个程序,测试散列映像的存储与遍历,方法的使用
*/
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
public&nb... 阅读全文
摘要: /** *//**
* 通过这个程序,测试树映像的使用,表目集合的遍历
*/
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class&... 阅读全文
摘要: /** *//** *//** *//**
* 我们设计的学生基本类
*/
class Student
{
private String strName = "";//学生姓名
... 阅读全文
摘要: /** *//** (C) 北大青鸟APTECH.
* 版权所有
*/
/** *//**
* 本程序导入所需的类.
*/
import java.io.File;
import java.io.BufferedR... 阅读全文
摘要: /** *//** (C) 北大青鸟APTECH.
* 版权所有
*/
/** *//**
* 本程序导入所需的类.
*/
import java.io.File;
import java.io.BufferedR... 阅读全文
摘要: /** *//** (C) 北大青鸟APTECH.
* 版权所有
*/
/** *//**
* 本程序导入所需的类.
*/
import java.io.File;
import java.io.BufferedR... 阅读全文
摘要: /**//* 北大青鸟APTECH
* 版权所有
*/
/** *//**
* 这个程序演示抽象类和方法的用法.
* @版本 1.0 2005 年 5 月 20 日
* @author M... 阅读全文
/**
* 测试数组元素拷贝
*/
public class ArrayCopy
{
public static void main(String[] args)
{
ArrayCopy aCopy = new ArrayCopy();
int[] a = {1, 2, 3, 4, 5};
int[] b = {10,20,30,40,50};
aCopy.copy(a, b);
}
public void copy(int[] from, int[] to)
{
System.out.println("第一个数组中的元素");
for (int i = 0; i < from.length; i++)
{
System.out.print(" " + from[i]);//打印出数组中的每一个元素
}
System.out.println("\n");
System.out.println("第二个数组中的元素");
for (int i = 0; i < to.length; i++)
{
System.out.print(" " + to[i]);//打印出数组中的每一个元素
}
System.out.println("\n\n将第一个数组拷贝到第二个数组\n");
System.arraycopy(from, 2, to, 0, 3);
System.out.println("拷贝完成后第二个数组中的元素");
for (int i = 0; i < to.length; i++)
{
System.out.print(" " + to[i]);//打印出数组中的每一个元素
}
}
}
输出结果是3 4 5 40 50
摘要: <%@page language="java" import="java.util.*,java.sql.*,Oper.*,voo.*" pageEncoding="GBK"%>
<table border=1>
<tr>
<th>编号</th>... 阅读全文
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class SuppressWarningsTest
{
public static void main(String[] args)
{
Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class SuppressWarningsTest2
{
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class SuppressWarningsTest3
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class DeprecatedTest
{
public void doSomething(){
System.out.println("guoxinghua");
}
public static void main(String[] args)
{
DeprecatedTest test=new DeprecatedTest();
test.doSomething();
}
}
class SuppressWarningsTest2
{
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class DeprecatedTest
{
public void doSomething(){
System.out.println("guoxinghua");
}
public static void main(String[] args)
{
DeprecatedTest test=new DeprecatedTest();
test.doSomething();
}
}
class SuppressWarningsTest2
{
@SuppressWarnings({"unchecked","deprecation"})
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
DeprecatedTest test=new DeprecatedTest();
test.doSomething();
}
}
摘要:
/**//*
*测试包含对象的克隆及clone方法的重写
*/
import java.util.GregorianCalendar;
import java.util.Date;
public class CloneTest{
public static void main(String[]&nbs... 阅读全文
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class FormServlet extends HttpServlet
{
private ServletConfig config = null ;
public void init(ServletConfig config) throws ServletException
{
this.config = config ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doGet处理提交参数 ") ;
this.doPost(req,resp) ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
String name = req.getParameter("uname") ;
// 取得application对象
// ServletContext app = this.getServletContext() ;
ServletContext app = this.config.getServletContext() ;
app.setAttribute("addr","www.MLDN.cn") ;
// 取得一个session对象
HttpSession session = req.getSession() ;
session.setAttribute("sname",name) ;
// System.out.println("** Servlet doPost处理提交参数 ") ;
System.out.println("name = "+name) ;
// 重定向
resp.sendRedirect("demo.jsp") ;
}
};
/**//*
<servlet>
<servlet-name>form</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>form</servlet-name>
<url-pattern>/formServlet</url-pattern>
</servlet-mapping>
*/
初始化两种方法
有参数与无参数
有参数的方法优先
多个地址可以映射到同一个SERVLET
配置初始化参数
form表单
<form action="formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>
下面是处理表单的servlet
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class InitParameterServlet extends HttpServlet
{
// 初始化
// 要取得初始化参数,必须使用以下初始化方法
public void init(ServletConfig config) throws ServletException
{
// config对象中有取得初始化参数的方法:getInitParameter("参数名称")
String ref1 = config.getInitParameter("ref1") ;
String ref2 = config.getInitParameter("ref2") ;
String dd = config.getInitParameter("DBDRIVER") ;
System.out.println("REF1 => "+ref1) ;
System.out.println("REF2 => "+ref2) ;
System.out.println("DBDRIVER => "+dd) ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doGet处理 ") ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doPost处理 ") ;
}
// 销毁
public void destroy()
{
// System.out.println("** Servlet 销毁 ") ;
}
};
/**//*
<servlet>
<servlet-name>param</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.InitParameterServlet</servlet-class>
<load-on-startup>0</load-on-startup>
<init-param>
<param-name>ref1</param-name>
<param-value>MLDN</param-value>
</init-param>
<init-param>
<param-name>ref2</param-name>
<param-value>LiXingHua</param-value>
</init-param>
<init-param>
<param-name>DBDRIVER</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>param</servlet-name>
<url-pattern>/ipar</url-pattern>
</servlet-mapping>
*/
学习资料下载
SERVLET是一种特殊的CGI
与CGI不同是多线程,性能很高
package cn.mldn.lxh.servlet ;
import java.io.* ;
// HttpServlet属于javax.servlet.http包下
// ServletException属于javax.servlet包下
import javax.servlet.* ;
// HttpServletRequest、HttpServletResponse存放在javax.servlet.http包下
import javax.servlet.http.* ;
public class SimpleServlet extends HttpServlet
{
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
PrintWriter out = resp.getWriter() ;
out.println("<HTML>") ;
out.println("<HEAD>") ;
out.println("<TITLE>THE FIRST SERVLET</TITLE>") ;
out.println("</HEAD>") ;
out.println("<BODY>") ;
out.println("<H1>Hello World!!!</H1>") ;
out.println("</BODY>") ;
out.println("</HTML>") ;
out.close() ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
this.doGet(request,response) ;
}
};
/**//*
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
*/
输出 Html用out.println()
很难维护
生命周期
生老病死(与人相似)
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class LifeCycleServlet extends HttpServlet
{
// 初始化
public void init(ServletConfig config) throws ServletException
{
System.out.println("** Servlet 初始化 ") ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
System.out.println("** Servlet doGet处理 ") ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
System.out.println("** Servlet doPost处理 ") ;
}
// 销毁
public void destroy()
{
System.out.println("** Servlet 销毁 ") ;
}
};
/**//*
<servlet>
<servlet-name>life</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.LifeCycleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>life</servlet-name>
<url-pattern>/lc</url-pattern>
</servlet-mapping>
*/
init
doservices
destroy
创建要求
继承HttpServlet(先导入javax.servlet.*)必须在包中
在web-inf/class下
重写两个方法
doGet(req,resp)throws IOException(javax.servlet.http.*,java.io.*)
用PrintWriter输出
修改WEB.XML文件,映射SERVLET
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/demo</url-pattern>//在地址中输入的内容</servlet-mapping>
笔记下载
servlet只初始一次(在第一次使用servlet程序后,也可以在
容器启动时初始化servlet程序,通过修改web.xml文件)
<load-on-startup>1</load-on-startup>
doGet,doPost(doget是输入地址,doPost是表单请求)
destroy(服务器关闭,或者长时间不用)
注意
开发框架和配置
/**
*静态内部类的测试
*/
import java.util.Vector;
public class StaticInnerClass{
public static void main(String[] args){
Vector vec=new Vector();
Student tom=new Student("Tom","20020410");
Student jack=new Student("jack","20020411");
Studentsmith=new Student("Smith ","20020412");
Student rose=new Student("Rose","20020413");
tom.setStudentScore(456);
jack.setStudentScore(500);
smith.setStudentScore(634);
rose.setStudentScore(414);
vec.add(tom);
vec.add(jack);
vec.add(smith);
vec.add(rose);
ArrayScore.PairScore pair=ArrayScore.minMax(vec);
System.out.println("最高分数为:"+pair.getMaxScore());
System.out.println("最低的分数为:")+pair.getMinScore();
}}
class ArrayScore{
static class PairScore{
private double maxScore;
private double minScore;
public PairScore(double max,double min){
maxScore=max;
minScore=min;
}
public double getMaxScore(){
return maxScore;
}
public double getMinScore(){
return minScore;
}
}
public static PairScore minMax(Vector studentVec){
double minScore=((Student)studentVec.get(0)).getStudentScore();
double maxScore=((Student)studentVec.get(0)).getStudentScore();
for(int i=1;i<studentVec.size();i++){
double score=((Student)studentVec.get(i)).getStudentScore();
if(minScore>score)
minScore=score;
if(maxScore<score)
maxScore=score;
}
return new PairScore(maxScore,minScore);
}
}
/**
*我们设计的学生基本类
*/
class Student{
private String strName="";
private Stirng strNumber="";
private Stirng strSex="";
private String strBirthday="";
private String strSpeciality="";
private String strAddress="";
private double totalScore;//学生的总分数
public Student(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public String getStudentNumber(){
return strNumber;
}
public void setStudentSex(String sex){
strSex=sex;
}
public String getStudentSex(){
return strSex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;
}
public String getStudentSpeciality(){
return strSpeciality;
}
public void setStudentSpeciality(String speciality) {
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public void setStudentAddress(String address){
strAddress=address;
}
public double getStudentScore(){
return totalScore;
}
public void setStudentScore(double score){
totalScore=score;
}
public String toString(){
String information ="student name="+strName+"student number="+strNumber;
if(!strSex.equals(""))
information+=",sex="+strSex;
if(!strBirthday.equals(""))
information+=",Birthday="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",Address="+strAddress;
return information;
}
}
摘要: /** *//**
* 我们设计的学生基本类
*/
class Student
{
private String strName = "";//学生姓名
private String&nb... 阅读全文