Chinaunix首页 | 论坛 | 博客
  • 博客访问: 341210
  • 博文数量: 329
  • 博客积分: 2633
  • 博客等级: 少校
  • 技术积分: 3633
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-22 15:43
文章分类

全部博文(329)

文章存档

2013年(244)

2012年(46)

2011年(39)

我的朋友

分类: 系统运维

2012-02-19 21:24:28

 精通J2EE整合应用案例-5章笔记.doc   

1.jsp页面间通过request/session传值

1).通过request来拿参数

当某一个页面的form提交之后(form中有"ss"字段),所对应的action便可以用request.getParameter("ss")获取这个"ss"的值。

 

2).通过session来拿参数

也可以在一个form中设置下参数,使用session.setAttribute("ss");再通过form对应的action中获得"ss"的参数值,使用的是(String)session.getAttribute("ss")

可以看:

session1.jsp(从表单输入usernmae,将该参数传递到session2.jsp




   


    name:
   
   
   

session2.jsp(接收session1.jsp传递过来的参数username,存到session中,然后在将参数place通过表单提交到session3.jsp中)

<%@ page language="java" contentType="text/html; charset=gb2312"%>


<%
   String username = request.getParameter("username");
   session.setAttribute("username",username); %>
   hello,
   <%=username%>
  
   !the place you want to go is
  


  


  
  


  
  



session3.jsp(接收session2.jsp传递过来的参数place,并从session中读取参数username),输出

<%@ page language="java" contentType="text/html; charset=gb2312"%>


<%
String username = (String) session.getAttribute("username");
String place = request.getParameter("place"); %>
<%= username %>


the place you want to go is


<%=place %>

 

2.使用JavaBean传值

1).第一种传值方式:调用beanset/get函数传值:


<%
   noticebean.setTitle("
今天星期几");
   noticebean.setContent("
星期一");%>
<%=noticebean.getTitle()%>
<%=noticebean.getContent() %>

2).第二种传值方式 使用 jsp:getProperty 读取值


例子可以看:

NoticeBean.java

package com.sunday.beans;

public class NoticeBean {
int id;
String title;
String content;

public NoticeBean(){}
public int getId() {
   return id;
}
public void setId(int id) {
   this.id = id;
}
public String getTitle() {
   return title;
}
public void setTitle(String title) {
   this.title = title;
}
public String getContent() {
   return content;
}
public void setContent(String content) {
   this.content = content;
}}
beantest.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"%>


Notice Bean Demo



<%
   noticebean.setTitle("
今天星期几");
   noticebean.setContent("
星期一");%>
the first question:
<%=noticebean.getTitle()%>


answer:
<%=noticebean.getContent() %>


<%
   noticebean.setTitle("
明天呢");
   noticebean.setContent("
星期二");%>
  


   the second question:




 

3.Servlet示例

Servletjsp的基础。事实上,在web服务器中,jsp经编译之后都将转换成Servlet

//ServletTest.Java

package demo;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class ServletTest extends HttpServlet {

       private static final long serialVersionUID = 1L;

       public void doGet(HttpServletRequest request, HttpServletResponse response)

                     throws IOException, ServletException {

//代码中直接写出jsp

              response.setContentType("text/html");

              PrintWriter out = response.getWriter();

              out.println("");

              out.println("");

              out.println("");

              out.println("ServletTest");

              out.println("");

              out.println("");

              out.println("

This is a servlet demo!

");

              out.println("");

              out.println("");

       }

}

web.xml中的配置:

  ServletTest 对应类的名称-->

  demo.ServletTest  对应包的名称-->

 

  ServletTest 对应类的名称-->

  /servlettest

 

 

 

4.jdbc示例

有连接mysql,oracle 9isql2000的示例,以及mysql数据库方面的操作,示例如下:

package demo;

import java.sql.*;

import demo.NoticeBean;

public class JDBCTest {

       public static void main(String args[]) {       

              // 连接Oracle 9iSQL Server 2000的例程

              /*    try {

                     String name = "oracle.jdbc.driver.OracleDriver";

                     String url = "jdbc:oracle:thin:@localhost:1521:kevin";

                     Class.forName(name).newInstance();

                     Connection con = DriverManager.getConnection(url, "kevin", "kevin");

                     System.out.println("连接Oracle 9i成功!");

                     con.close();

              } catch (Exception e) {

                     e.printStackTrace();

              }

              try {

                     String name = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

                     String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JavaWeb";

                     Class.forName(name).newInstance();

                     Connection con = DriverManager.getConnection(url, "sa", "sa");

                     System.out.println("连接SQL Server 2000成功!");

                     con.close();

              } catch (Exception e) {

                     e.printStackTrace();

              }*/

 

              Connection connection = null;

              Statement statement = null;

              ResultSet resultSet = null;

//连接mysql

              try {

                     String driverName = "org.gjt.mm.mysql.Driver";

                     Class.forName(driverName).newInstance();

                     String url = "jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=gb2312";

                     connection = DriverManager.getConnection(url, "root", "root");

                     System.out.println("连接MySQL 5.0.18成功!(驱动3.1.12)");

                     statement = null;

                     resultSet = null;

                     String strSql = null;

                     NoticeBean bean = null;

                     String title = null;

                     String content = null;

                     // 对数据库的插入操作

                     try {

                            title = "标题";

                            content = "内容";

                            strSql = "INSERT INTO notice(title, content) VALUES('" + title

                                          + "','" + content + "')";

                            statement = connection.createStatement();

                            statement.executeUpdate(strSql);

                            System.out.println("插入语句执行成功:" + strSql);

                     } catch (SQLException ex1) {

                            System.out.println("插入失败!");

                     }

                     // 对数据库的查询操作

                     try {

                            strSql = "SELECT * FROM notice";

                            statement = connection.createStatement(

                                          ResultSet.TYPE_SCROLL_INSENSITIVE,

                                          ResultSet.CONCUR_READ_ONLY);

                            resultSet = statement.executeQuery(strSql);

                            if (resultSet.next()) {

                                   int id = resultSet.getInt("id");

                                   title = resultSet.getString("title");

                                   content = resultSet.getString("content");

                                   if (resultSet.next()) {

                                          bean = new NoticeBean(id, title, content);

                                   }

                                   System.out.println("notice表的第一行数据是:" + bean.getId() + " "

                                                 + bean.getContent() + " " + bean.getTitle());

                            }

                     } catch (SQLException ex1) {

                            System.out.println("查询失败!");

                     }

                     // 对数据库的删除操作

                     try {

                            strSql = "DELETE FROM notice";

                            statement = connection.createStatement();

                            statement.executeUpdate(strSql);

                            System.out.println("notice表的所有数据已被删除!");

                     } catch (SQLException ex1) {

                            System.out.println("删除失败!");

                     }

              } catch (Exception e) {

                     e.printStackTrace();

              } finally {

                     try {

                            if (resultSet != null) {

                                   resultSet.close();

                                   resultSet = null;

                            }

                            if (statement != null) {

                                   statement.close();

                                   statement = null;

                            }

                            if (connection != null) {

                                   connection.close();

                                   connection = null;

                            }

                     } catch (SQLException ex) {

                            System.err.println(ex.getMessage());

                     }

              }

       }

}

阅读(588) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~