java技术博客

jsp博客
数据加载中……

SERVLET(2)

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>
*/


学习资料下载

posted @ 2008-10-23 12:36 郭兴华 阅读(145) | 评论 (0)编辑 收藏
SERVLET

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(服务器关闭,或者长时间不用)
注意
开发框架和配置

posted @ 2008-10-23 11:22 郭兴华 阅读(107) | 评论 (0)编辑 收藏
StudentTest1.java

 

/**
* main方法单独设计,编译是不能通过
*/

public class StudentTest1{
public static void main(String[] args)
{
String[] course
={"计算机原理","编译方法","数据结构"};
Student one
=new Student("Tom","20021024");
one.setStudentCourse(course);
}

}


/**
 * 我们设计的学生基本类
 
*/

class Student
{
 
 
private String strName = "";//学生姓名
 private String strNumber = "";//学号
 private String strSex = "";//性别
 private String strBirthday = "";//出生年月
 private String strSpeciality = "";//专业
 private String strAddress = "";//地址

 
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 String toString()
 
{
  String information 
= "学生姓名=" + strName + ", 学号=" + strNumber;  
  
if!strSex.equals("") )
   information 
+= ", 性别=" + strSex;
  
if!strBirthday.equals(""))
   information 
+= ", 出生年月=" + strBirthday;
  
if!strSpeciality.equals("") )
   information 
+= ", 专业=" + strSpeciality;
  
if!strAddress.equals("") )
   information 
+= ", 籍贯=" + strAddress;
  
return information;
 }

public void setStudentCourse(final String[] strCourse){
class Course{
private int courseNumber=strCourse.length;
private void getCourse(){
for(int i=0;i<courseNumber;i++)
{System.out.println("\t"+strCourse[i]);
}

}

public void getDescription(){
System.out.println(
"学生:"+strName+" student number is :"+strNumber+"。一共选了"+courseNumber+"门课,分别是:");
getCourse();
}
}

Course course
=new Course();
course.getDescription();
}

}

posted @ 2008-10-23 10:18 郭兴华 阅读(224) | 评论 (1)编辑 收藏
StaticInnerClass.java

/**
*静态内部类的测试
*/
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;
}



}

posted @ 2008-10-23 09:40 郭兴华 阅读(121) | 评论 (0)编辑 收藏
Student.java

     摘要: /** *//**  * 我们设计的学生基本类  */ class Student {     private String strName = "";//学生姓名     private String&nb...  阅读全文

posted @ 2008-10-23 00:08 郭兴华 阅读(191) | 评论 (0)编辑 收藏
Person.java

public interface Person{
String getName();
String getSex();
String getBirthday();
String getAddress();
void setAddress(String strAddress);
}

posted @ 2008-10-22 23:57 郭兴华 阅读(94) | 评论 (0)编辑 收藏
ObjectVarTest.java

/**
*测试这个程序,测试对象类型的动态绑定
*/
public class ObjectVarTest
{
public static void main(String[] args){
B b=new B();
C.method3(b);
}
}
class A{
public void method1(){
System.out.println("this is class A method1");
}
}
class B extends A{
public void method1(){
System.out.println("this is class B method1");
public void method2(){
System.out.println("this is class B method2");
}
}
class c{
public static void method3(A a){
a.method1();
}}
}

posted @ 2008-10-22 23:53 郭兴华 阅读(78) | 评论 (0)编辑 收藏
ObjectTypeTest.java

 

/**
*通过这个程序,测试对象类型的多态实现
*/

public class ObjectTypeTest{
public static void main(String[] args){
B a
=new B();
a.method2();
}
}

class A{
public void method1(){
System.out.println(
"this is class A method1");}
}

class B extends A{
public void method1(){
System.out.println(
"this is class B method1");}

public void method2(){
System.out.println(
"this is class B method2");}

}

posted @ 2008-10-22 23:46 郭兴华 阅读(101) | 评论 (0)编辑 收藏
BasicTypeTest.java

/**
*测试基本类型的类型转换
*/

public class BasicTypeTest{
public static void main(String[] args){
int a=10;
long b=a;
System.out.println(
"the value of b is "+b);
}

}

posted @ 2008-10-22 23:38 郭兴华 阅读(73) | 评论 (0)编辑 收藏
ArrayListTest.java

/**
*i测试数组列表的使用
*/
import java.util.ArrayList;
public class ArrayList{
public static void main(String[] args){
ArrayList list=new ArrayList();
list.add(new Student("Tom","20020410"));
list.add(new Student("Jack","20020411"));
list.add(new Student("Rose","20020412"));
for(int i =0;i<list.size();i++)
{
System.out.println((Student)list.get(i));
}}}
class Student {
private String strName="";
private String strNumber="";
private String strSex="";
private String strBirthday="";
private String strAddress="";
public Student(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public String getStudentNumber() {
return strNumber;
}
public String 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 setStudentSpececiality(String speciality){
strSpeciality=speciality;
}
public void setStudentAddress(String address){
strAddress=address;
}
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+=",籍贯="+strAddress;
return information;
}
}

posted @ 2008-10-22 23:33 郭兴华 阅读(137) | 评论 (0)编辑 收藏
仅列出标题
共9页: 上一页 1 2 3 4 5 6 7 8 9 下一页