1 简述在JSP页面中能够包含哪些内容?各有什么作用?

参考答案

1.JSP页面中可以包含Java代码,包含以下三种形式:

其作用为控制页面中可变内容的产生。

2.JSP页面中可以包含指令,语法规则为“<%@指令名属性=值 %>”。常用指令如下:

其作用为控制JSP在转译成Servlet类时生成的内容。

3. JSP页面中包含隐含对象,所有隐含对象由容器自动创建,在JSP文件中可以直接使用。JSP页面中可使用的隐含对象如图-1所示:

图-1

其作用为JSP预先创建的这些对象可以简化对HTTP请求、响应信息的访问

2 阅读下面的代码,说明序号处代码的含义。

<%@page pageEncoding="utf-8" 	contentType="text/html;charset=utf-8"%>//------1
<table class="table">
	<tr class="table_header">
		<td>ID</td>
		<td>姓名</td>
		<td>薪水</td>
		<td>年龄</td>
		<td>操作</td>
	</tr>

	<%  																//------2
		EmployeeDAO dao = 
			new EmployeeDAO();
		List<Employee> employees = dao.findAll();
				
		for(int i=0;i<employees.size();i++){
		Employee e = employees.get(i);
	%>
	<tr class="row1">
		<td>
<%=e.getId()%>											//------3
		</td>
		<td>
			<%=e.getName()%>
		</td>
		<td>
			<%=e.getSalary()%>
		</td>
		<td>
			<%=e.getAge()%>
		</td>
		<td>
			<a href="emplist.html">删除</a>&nbsp;
			<a href="updateEmp.html">更新</a>
		</td>
	</tr>	
	<%
		}
	%>
</table>

参考答案

本题中三个序号处代码的含义如下:

1.通过page指令设置了容器读取该文件时,按UTF-8进行解码;设置response.setContentType方法的参数值为"text/html;charset=utf-8"。

2. <% %> JSP小脚本,能够写在方法里的Java代码片段都可以作为小脚本。

3. 输出“e.getId()”的值到浏览器。

3 使用JSP实现帐务帐号信息的展示。

使用美工提供的静态页面实现帐务帐号管理的信息列表展示,如图-2:

图– 2

参考答案

将静态页面代码复制到jsp页面中,添加page指令,修改表格的输出,使用循环控制集合的展示,修改行标题,修改行的背景样式。

完成本案例可以按照如下步骤进行。

步骤一:新建listAccount.jsp页面

依据自定义模板,新建listAccount.jsp页面。

步骤二:复制静态页面代码到jsp页面中

打开准备好的静态页面,选择account_list.html页面,将页面的全部代码拷贝到listAccount.jsp页面中,并且styles和images两个文件夹也要拷贝到工程的WebRoot文件夹下。

图 -3

步骤三:修改page指令,添加java代码

修改page指令,添加import及content-Type属性,保证导入DAO和实体类所在的包,以及正确设定浏览器对内容的解码方式。如图-4所示。

图-4

步骤四:设置列表信息

使用JSP小脚本显示表中的动态信息,如图-5所示。

图– 5

步骤五:部署,访问应用

运行结果如图-6所示:

图– 6

本案例的完整代码如下。

listAccount.jsp文件代码如下:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="dao.*,entity.*,java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>达内-NetCTOSS</title>
		<link type="text/css" rel="stylesheet" media="all"
			href="styles/global.css" />
		<link type="text/css" rel="stylesheet" media="all"
			href="styles/global_color.css" />
		<script language="javascript" type="text/javascript">
			//删除
			function deleteAccount() {
				var r = window.confirm("确定要删除此账务账号吗?\r\n删除后将不能恢复,且会删除其下属的所有业务账号。");
				document.getElementById("operate_result_info").style.display = "block";
			}
			//开通或暂停
			function setState() {
				var r = window.confirm("确定要开通此账务账号吗?");
				document.getElementById("operate_result_info").style.display = "block";
			}
		</script>
	</head>
	<body>
		<!--Logo区域开始-->
		<div id="header">
			<img src="../images/logo.png" alt="logo" class="left" />
			<a href="#">[退出]</a>
		</div>
		<!--Logo区域结束-->
		<!--导航区域开始-->
		<div id="navi">
			<ul id="menu">
				<li>
					<a href="../index.html" class="index_off"></a>
				</li>
				<li>
					<a href="../role/role_list.html" class="role_off"></a>
				</li>
				<li>
					<a href="../admin/admin_list.html" class="admin_off"></a>
				</li>
				<li>
					<a href="../fee/fee_list.html" class="fee_off"></a>
				</li>
				<li>
					<a href="../account/account_list.html" class="account_on"></a>
				</li>
				<li>
					<a href="../service/service_list.html" class="service_off"></a>
				</li>
				<li>
					<a href="../bill/bill_list.html" class="bill_off"></a>
				</li>
				<li>
					<a href="../report/report_list.html" class="report_off"></a>
				</li>
				<li>
					<a href="../user/user_info.html" class="information_off"></a>
				</li>
				<li>
					<a href="../user/user_modi_pwd.html" class="password_off"></a>
				</li>
			</ul>
		</div>
		<!--导航区域结束-->
		<!--主要区域开始-->
		<div id="main">
			<form action="" method="">
				<!--查询-->
				<div class="search_add">
					<div>
						身份证:
						<input type="text" value="不验证" class="text_search" />
					</div>
					<div>
						姓名:
						<input type="text" class="width70 text_search" value="不验证" />
					</div>
					<div>
						登录名:
						<input type="text" value="不验证" class="text_search" />
					</div>
					<div>
						状态:
						<select class="select_search">
							<option>
								全部
							</option>
							<option>
								开通
							</option>
							<option>
								暂停
							</option>
							<option>
								删除
							</option>
						</select>
					</div>
					<div>
						<input type="button" value="搜索" class="btn_search" />
					</div>
					<input type="button" value="增加" class="btn_add"
						onclick="location.href='addAccount.html';" />
				</div>
				<!--删除等的操作提示-->
				<div id="operate_result_info" class="operate_success">
					<img src="../images/close.png"
						onclick="this.parentNode.style.display='none';" />
					删除成功,且已删除其下属的业务账号!
				</div>
				<!--数据区域:用表格展示数据-->
				<div id="data">
					<table id="datalist">
						<tr>
							<th>
								账号ID
							</th>
							<th>
								姓名
							</th>
							<th class="width150">
								身份证
							</th>
							<th>
								登录名
							</th>
							<th>
								状态
							</th>

							<th class="width200"></th>
						</tr>
						<%
							AccountDAO dao = new AccountDAO();
							List<Account> accounts = dao.findAll();
							for (Account account : accounts) {
								out.println("<tr>");
								String strStatus = "";
								if ("1".equals(account.getStatus())) {
									strStatus = "暂停";
								} elseif ("2".equals(account.getStatus())) {
									strStatus = "删除";
								} else {
									strStatus = "开通";
								}
						%>
						<tr>
							<td><%=account.getAccountId()%></td>
							<td><a href="account_detail.html"><%=account.getRealName()%></a></td>
							<td><%=account.getIdcardNo()%></td>
							<td><%=account.getLoginName()%></td>
							<td><%=strStatus%></td>
							<td class="td_modi">
								<input type="button" value="暂停" class="btn_pause" onclick="setState();" />
								<input type="button" value="修改" class="btn_modify" onclick="location.href='account_modi.html';" />
								<input type="button" value="删除" class="btn_delete" onclick="deleteAccount();" />
							</td>
						</tr>
						<%
							}
						%>
					</table>
				</div>
				<!--分页-->
				<div id="pages">
					<a href="#">首页</a>
					<a href="#">上一页</a>
					<a href="#" class="current_page">1</a>
					<a href="#">2</a>
					<a href="#">3</a>
					<a href="#">4</a>
					<a href="#">5</a>
					<a href="#">下一页</a>
					<a href="#">末页</a>
				</div>
			</form>
		</div>
		<!--主要区域结束-->
		<div id="footer">
			<p>
				[源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目]
			</p>
			<p>
				版权所有(C)加拿大达内IT培训集团公司
			</p>
		</div>
	</body>
</html>

Account类的完整代码如下所示:

package entity;

publicclass Account {
	privateint accountId;
	private String realName;
	private String idcardNo;
	private String loginName;
	private String status;
	private String telephone;

	publicint getAccountId() {
		return accountId;
	}

	publicvoid setAccountId(int accountId) {
		this.accountId = accountId;
	}

	public String getRealName() {
		return realName;
	}

	publicvoid setRealName(String realName) {
		this.realName = realName;
	}

	public String getIdcardNo() {
		return idcardNo;
	}

	publicvoid setIdcardNo(String idcardNo) {
		this.idcardNo = idcardNo;
	}

	public String getLoginName() {
		return loginName;
	}

	publicvoid setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getStatus() {
		return status;
	}

	publicvoid setStatus(String status) {
		this.status = status;
	}

	public String getTelephone() {
		return telephone;
	}

	publicvoid setTelephone(String telephone) {
		this.telephone = telephone;
	}

}

DBUtil类的完整代码如下所示:

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * JDBC管理连接的工具类,可以获取连接和关闭连接
 */
public class DBUtil {
	/**
	 * 获取连接对象
	 */
	public static Connection getConnection()throws Exception{
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return conn;
	}
	/**
	 * 关闭连接对象
	 */
	public static void close(Connection conn) throws Exception{
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw e;
			}
		}
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(getConnection());
	}
}

AccountDAO类的完整代码如下所示:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.Account;

/**
 * Account的数据操作对象,负责Account的增删改查
 */
public class AccountDAO {
	
	/**
	 * 查询所有帐务帐号信息
	 */
	public List<Account> findAll() throws Exception{
		List<Account> accounts = new ArrayList<Account>();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			conn = DBUtil.getConnection();
			stmt = conn.prepareStatement("select * from account");
			rs = stmt.executeQuery();
			while(rs.next()){
				Account account = new Account();
				int accountId = rs.getInt("account_id");
				String realName = rs.getString("real_name");
				String idcardNo = rs.getString("idcard_no");
				String loginName = rs.getString("login_name");
				String status=rs.getString("status");
				String telephone = rs.getString("telephone");
				
				account.setAccountId(accountId);
				account.setRealName(realName);
				account.setIdcardNo(idcardNo);
				account.setLoginName(loginName);
				account.setStatus(status);
				account.setTelephone(telephone);
				
				accounts.add(account);
			}
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
		return accounts;
	}
	
	/**
	 * 删除帐务帐号信息
	 */
	public void delete(int accountId) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		try{
			conn = DBUtil.getConnection();
			stmt = conn.prepareStatement("delete from account where account_id=?");
			stmt.setInt(1, accountId);
			stmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
	}
	
	/**
	 * 增加帐务帐号信息
	 */
	public void save(Account account) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		try{
			conn = DBUtil.getConnection();
			stmt = conn.prepareStatement("insert into account" +
					"(account_id,login_name,status,real_name,idcard_no,telephone) " +
					"values(account_id_seq.nextval,?,?,?,?,?)");
			stmt.setString(1, account.getLoginName());
			stmt.setString(2, "0");//添加创建即为开通状态
			stmt.setString(3, account.getRealName());
			stmt.setString(4, account.getIdcardNo());
			stmt.setString(5, account.getTelephone());
			stmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
	}
	
	/**
	 * 根据id查询员工信息
	 */
	public Account findById(int id) throws Exception{
		Account account = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			conn = DBUtil.getConnection();
			stmt = conn.prepareStatement("select * from account where account_id=?");
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			if(rs.next()){
				account = new Account();
				int accountId = rs.getInt("account_id");
				String realName = rs.getString("real_name");
				String idcardNo = rs.getString("idcard_no");
				String loginName = rs.getString("login_name");
				String status=rs.getString("status");
				String telephone = rs.getString("telephone");
				
				account.setAccountId(accountId);
				account.setRealName(realName);
				account.setIdcardNo(idcardNo);
				account.setLoginName(loginName);
				account.setStatus(status);
				account.setTelephone(telephone);
			}
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
		return account;
	}

	/**
	 * 保存修改员工信息
	 */
	public void modify(Account account)throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		try{
			conn = DBUtil.getConnection();
			stmt = conn.prepareStatement("update account set login_name=?,real_name=?," +
					"idcard_no=?,telephone=? where account_id=?");
			stmt.setString(1, account.getLoginName());
			stmt.setString(2, account.getRealName());
			stmt.setString(3, account.getIdcardNo());
			stmt.setString(4, account.getTelephone());
			stmt.setInt(5, account.getAccountId());
			stmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
	}
}