java技術博客

          jsp博客
          數據加載中……

          DAO設計模式

          產生的原因
          JDBC不能出現在JSP頁面中
          什么是DAO
          J2EE組件層次
          客戶端——》表示層--》業務層--》數據層(DAO)

          --delete
          DROP TABLE person;
          --create 
          CREATE TABLE person{
          }
          --ts
          commit;


          異常 的處理通常由調用它的方法來處理

          posted @ 2008-10-22 23:03 郭興華 閱讀(128) | 評論 (0)編輯 收藏
          MVC設計模式

          jsp+javabean開發(jsp接收參數,調用 javaBean)

          開發速度快,有一個問題,藕合性高,維護困難

          一個人開發使用,人多了不好控制

          jsp+DAO設計模式

          jsp+servlet+javabean(servlet調用javabean)

          jsp:UI

          javaBean:重復調用

          servlet:安全性高性能也高,

          jsp兩種跳轉方式

          1. response.sendRedirect():客戶端跳轉,請求不保存
          2. <jsp:forward page="">:服務器端跳轉,請求要保存

           

           

           

          jsp中有四種屬性范圍:page ,response,application,session

          package cn.mldn.lxh.servlet ;

          import java.io.* ;
          import javax.servlet.* ;
          import javax.servlet.http.* ;
          import cn.mldn.lxh.bean.MVCCheck ;

          public class MVCServlet extends HttpServlet
          {
              
          public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
              
          {
                  
          this.doPost(req,resp) ;
              }

              
          public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
              
          {
                  String name 
          = req.getParameter("uname") ;
                  MVCCheck mc 
          = new MVCCheck() ;
                  
          // 將請求內容設置到mc對象之中
                  mc.setName(name) ;
                  String path 
          = null ;
                  
          if(mc.isValidate())
                  
          {
                      
          // 保存名字在request范圍之中
                      req.setAttribute("name",mc.getName()) ;
                      path 
          = "mvc_success.jsp" ;
                  }

                  
          else
                  
          {
                      path 
          = "mvc_failure.jsp" ;
                  }

                  
          // 進行跳轉
                  req.getRequestDispatcher(path).forward(req,resp) ;//向下傳
              }

          }
          ;
          /*
            <servlet>
              <servlet-name>mvc</servlet-name>
              <servlet-class>cn.mldn.lxh.servlet.MVCServlet</servlet-class>
            </servlet>
            <servlet-mapping>
              <servlet-name>mvc</servlet-name>
              <url-pattern>/mvcdemo.mldn</url-pattern>
            </servlet-mapping>
          */
          servlet傳值傳不過去,用Session傳,但是Session占用空間
          解決方法:Dispatcher


          本章資料下載
          PDF筆記

          posted @ 2008-10-22 21:22 郭興華 閱讀(141) | 評論 (0)編輯 收藏
          jsp標簽庫編程


          筆記下載
          代碼下載地址:
          /Files/guohua/TaglibProject.rar
          hello.java
          package org.lxh.demo01;

          import java.io.IOException;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspWriter;
          import javax.servlet.jsp.tagext.TagSupport;

          public class Hello extends TagSupport {

              @Override
              
          public int doStartTag() throws JspException {
                  
          // 向JSP頁面中打印“www.mldn.cn”的字符串
                  String str = "www.mldn.cn" ;
                  
          // 在Servlet里如果要打印,則要使用PrintWriter
                  
          // 如果在標簽庫中打印需要使用pageContext
                  JspWriter out = pageContext.getOut() ;
                  
          try {
                      out.println(str);
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

                  
          // 只要打印完了字符傳,則后面的代碼就不需要執行了
                  return TagSupport.SKIP_BODY;
              }


          }


          hello.jsp
          <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
          <%@ taglib uri="/WEB-INF/hello.tld" prefix="lxh"%>
          <html>
            
          <head>
              
          <title>My JSP 'hello.jsp' starting page</title>
              
          <!--
              
          <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->

            
          </head>
            
            
          <body>
              
          <h1><lxh:mldn/></h1>
            
          </body>
          </html>
          helloxml.jsp
          <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
          <%@ taglib uri="/WEB-INF/hello.tld" prefix="lxh"%>
          <html>
            
          <head>
              
          <title>My JSP 'hello.jsp' starting page</title>
              
          <!--
              
          <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->

            
          </head>
            
            
          <body>
              
          <h1><lxh:mldn/></h1>
            
          </body>
          </html>
          hello.tld
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >
          <taglib>
              
          <tlib-version>1.0</tlib-version>
              
          <jsp-version>2.0</jsp-version>
              
          <short-name>hello</short-name>
              
          <tag>
                  
          <name>mldn</name>
                  
          <tag-class>org.lxh.demo01.Hello</tag-class>
                  
          <body-content>empty</body-content>
              
          </tag>
          </taglib>

          posted @ 2008-10-22 19:20 郭興華 閱讀(172) | 評論 (0)編輯 收藏
          jdbcorache.jsp

          <%@ page contentType="text/html;charset=gb2312"%>
          <%@page import ="java.sql.*"%>
          <%--
          使用JDBC連接Oracle數據庫
          使用MLDN數據庫
          用戶名:scott
          密碼:tiger
          --%>
          <%!
          String DBDRIVER="oracle.jdbc.driver.OracleDriver";
          String DBURL="jadb:oracle:thin:@localhost:1521:mldn";
          String DBUSER="scott";
          String DBPASSWORD="tiger";
          Connection conn=null;
          statement stmt=null;
          %>
          <%try{Class.forname(DBDRIVER);
          conn=DriverManger.getConnection(DBURL,DBUSER,DBPASSWORD);
          //create table
          String sql="CREATE TABLE mldnab(name vachar(20))";
          stmt=conn.createStatement();
          stmt.executeUpdate(sql);
          stmt.close();
          conn.close();
          }catch(Exception e){
          out.println(e);

          }


          posted @ 2008-10-22 16:37 郭興華 閱讀(84) | 評論 (0)編輯 收藏
          ConstructorTest

          /**
          *測試類構造器的調用順序
          */
          public class ConstructorTest{
          public static void main(String[] args){
          C c=new C("hello");
          }
          }
          class A{
          public A(){
          System.out.println("this is A");
          }
          }
          class C extends B{
          public C(String str){
          super(str);
          System.out.println("this is C");
          }
          }


          posted @ 2008-10-22 13:49 郭興華 閱讀(142) | 評論 (0)編輯 收藏
          ToStringTest.java

          public class ToStringTest{
          public static void main(String[] args){
          System.out.println(new Student("youName","20031001"));
          }
          }

          posted @ 2008-10-22 13:41 郭興華 閱讀(108) | 評論 (0)編輯 收藏
          SubStringTest.java

          /**
          *通過這個程序,展示字符串求取子串的方法
          */
          public class SubStringTest{
          public static void main(String[] args){
          String str="I am a Programmer";//定義字符串
          for(int i=0;i<str.length();i++){
          System.out.println("這是第"+i+"個子串:"+str.subString(i));
          }}}

          posted @ 2008-10-22 13:38 郭興華 閱讀(164) | 評論 (0)編輯 收藏
          InputTest.java

          /**
          *讀取鍵盤輸入
          */
          import javax.swing.JOptionPane;
          public class InputTest{
          public static void main(String[] args){
          String strName=JOptionPane.showInputDialog("Input your name:");
          String strAge=JOptionPane.showInputDialog("Input your age:");
          int age=Integer.parseInt(strAge);
          System.out.println("Welcome you :"+strName);
          System.out.println("你還有"+(60-age)+"年可以退休了??!");
          System.exit(0);
          }
          }

          posted @ 2008-10-22 13:30 郭興華 閱讀(143) | 評論 (0)編輯 收藏
          FormateValue.java

          /**
          *通過這個程序,測試格式器的使用方法
          */
          import java.text.NumberFormat;
          import java.util.locale;
          public class FormateValue{
          public static void main(String[] args){
          double x=100000.0/3;
          System.out.println("the value is :"+x);
          //首先以默認地區的格式器來輸出
          System.out.println("接下來輸出的是美國地區的格式器的結果:");
          NumberFormat number=NumberFormat.getNumberInstance(Locale.US);
          NumberFormat number=NumberFormat.getCurrencyInstance(Locale.US);
          NumberFormat percent=NumberFormat.getPercentInstance(Locale.US);


          FormatValue fm=new FormatValue();
          fm.print(number,x);
          fm.print(currency.x);
          fm.print(percent,x);

          System.out.println("接下來輸出的是默認地區的格式器的結果:");
          NubmerFormat nu=NumberFormat.getNumberInstance();
          NubmerFormat cu=NumberFormat.getCurrencyInstance();
          NubmerFormat be=NumberFormat.getPercentIntstance();
          fm.print(nu,x);
          fm.print(cu,x);
          fm.print(pe,x);
          }
          public void print(NubmerFormat nu,double x){
          System.out.println(nu.format(x));
          }
          }

          posted @ 2008-10-22 13:21 郭興華 閱讀(182) | 評論 (0)編輯 收藏
          FindSubstring.java

          /**
          *查找特定的子串
          */
          public class FindSubstring{
          public static void main(String[] args){
          String str="Welcome the body.";
          System.out.println("Find the substring boy occurrence:"+str.indexOf("boy"));//查找子串
          System.out.println("Find the substring by occurrence: "+str.indexOf("by"));//查找子串
          System.out.println("Find the char t occurrence: "+str.indexOf('t'));//查找特定的字符
          System.out.println("Test the string begin with wel.:"+str.startsWith("wel"));//是否以"wel"開始
          System.out.pritnln("Test the string end with body.:"+str.endWith("boy"));//是否以"boy"結束
          }
          }

          posted @ 2008-10-22 13:06 郭興華 閱讀(202) | 評論 (0)編輯 收藏
          僅列出標題
          共9頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 
          主站蜘蛛池模板: 秦安县| 容城县| 东宁县| 黄龙县| 临城县| 临清市| 上杭县| 得荣县| 中超| 兰溪市| 鄄城县| 桓台县| 丰都县| 镇坪县| 波密县| 乃东县| 荣昌县| 南昌市| 山西省| 佛教| 商南县| 郸城县| 淮北市| 浑源县| 长宁县| 太仓市| 五家渠市| 纳雍县| 德庆县| 特克斯县| 蒙阴县| 精河县| 上蔡县| 宁南县| 河间市| 哈尔滨市| 崇左市| 饶阳县| 慈溪市| 太和县| 奉新县|