JavaWeb编程技术(沈泽刚版)教材习题参考答案(5)

2021-09-24 15:17

import com.model.SampleDAO;

public class SelectCustomerServlet extends HttpServlet{

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException,IOException{

SampleDAO dao = new SampleDAO();

ArrayList<CustomerBean> custs = dao.selectCustomer();

request.setAttribute("custs", custs);

RequestDispatcher view = getServletContext().

getRequestDispatcher("/displayCustomer.jsp");

view.forward (request,response);

}

}

displayCustomer.jsp页面代码如下:

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

<%@ page import="com.model.CustomerBean,java.util.*" %>

<html><head> <title>Display Customer</title></head>

<body>

<%

ArrayList<CustomerBean> custs =

(ArrayList<CustomerBean>)request.getAttribute("custs");

for(CustomerBean cb : custs){

out.println(cb.getCustName() + " "+ cb.getEmail() + " "

+ cb.getPhone() + "</br>");

}

%>

</body></html>

沈泽刚 秦玉平主编清华大学出版社

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rst = null;

CustomerBean customer =null;

try{

conn = dataSource.getConnection();

pstmt = conn.prepareStatement(DELETE_SQL);

pstmt.setString(1,custName);

int n = pstmt.executeUpdate();

if(n ==1){

return true;

}else{

return false;

}

}catch(SQLException se){

return false;

}finally{

try{

pstmt.close();

conn.close();

}catch(SQLException se){}

}

}

// 修改客户记录

public boolean updateCustomer(CustomerBean customer){

Connection conn = null;

PreparedStatement pstmt = null;

try{

conn = dataSource.getConnection();

pstmt = conn.prepareStatement(UPDATE_SQL);

pstmt.setString(1,customer.getEmail());

pstmt.setString(2,customer.getPhone());

pstmt.setString(3,customer.getCustName());

int n = pstmt.executeUpdate();

if(n ==1){

return true;

}else{

return false;

}

}catch(SQLException se){

return false;

}finally{

try{

pstmt.close();

沈泽刚 秦玉平主编清华大学出版社

conn.close();

}catch(SQLException se){}

}

}

11. 首先在PostgreSQL数据库中创建一个求整数阶乘的函数my_factorial(),代码如下: CREATE OR REPLACE FUNCTION my_factorial(value integer)

RETURNS integer AS $$

DECLARE

arg integer;

BEGIN

arg := value;

IF arg IS NULL OR arg < 0 THEN

RAISE NOTICE 'Invalid Number';

RETURN NULL;

ELSE

IF arg = 1 THEN

RETURN 1;

ELSE

DECLARE

next_value integer;

BEGIN

next_value := my_factorial(arg - 1) * arg; -- 函数递归调用

RETURN next_value;

END;

END IF;

END IF;

END;

$$ LANGUAGE 'plpgsql';

创建JSP页面compute.jsp,连接数据库,调用存储过程:

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

<%@ page import="java.sql.*,java.io.*"%>

<html><body>

<form action="" method="post">

请输入一个整数:<input type="text" name="param" size="5" />

<input type="submit" value="确定" />

</form>

<%

String driver = "org.postgresql.Driver";

String dburl = "jdbc:postgresql://127.0.0.1:5432/bookstore";

String username = "bookstore";

String password = "bookstore";

try{

沈泽刚 秦玉平主编清华大学出版社

int result = cstmt.getInt(1);

request.setAttribute("result",result);

}catch(ClassNotFoundException e1){

out.println(e1);

}catch(SQLException e2){

out.println(e2);

}catch(NumberFormatException e3){

out.println("输入不合法!");

}

%>

<p><%=request.getParameter("param")%>的阶乘为:

<%=request.getAttribute("result")%></p>

</body></html> 该页面的运行结果如下:

12. 略。

4.8 第8章习题解析

1. 答:表达式语言是JSP页面中使用的一种简洁的数据访问语言。它定义了运算符实现算术、关系等运算;可以对作用域变量、JavaBeans对象、集合的元素、请求参数、Cookie等进行简单的访问;还可以访问Java语言定义的函数(静态方法)。

2. 答:使用点(.)运算符可以访问Map对象一个键的值和bean对象的属性值。使用方括号([])运算符还可以List对象和数组对象的元素。

3. 答:(1)作用域变量;(2)JavaBeans的属性;(3)访问集合元素;(4)访问隐含变量。

4. 答:(1)定义public类和静态方法;(2)创建标签库描述文件(TLD);(3)在JSP页面中访问EL函数。

5. 答:将page指令的isELIgnored属性设置为true,如下所示:

JavaWeb编程技术(沈泽刚版)教材习题参考答案(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:基于交际的高中英语词汇有效教学模式初探

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: