SCWCD(二)

来源:互联网 发布:美国研究生预科 知乎 编辑:程序博客网 时间:2024/06/14 15:07

226.Given:
3. public class MyTagHandler extends TagSupport {
4. public int doStartTag() {
5. // insert code here
6. // return an int
7. }
8. // more code here
...
18. }
There is a single attribute foo in the session scope.
Which three code fragments, inserted independently at line 5, return the value of the attribute? (Choose
three.)
A. Object o = pageContext.getAttribute("foo");
B. Object o = pageContext.findAttribute("foo");
C. Object o = pageContext.getAttribute("foo",
PageContext.SESSION_SCOPE);
D. HttpSession s = pageContext.getSession();
Object o = s.getAttribute("foo");
E. HttpServletRequest r = pageContext.getRequest();
Object o = r.getAttribute("foo");

解析:findAttribute能在所以范围中查找属性,pageContext为上下文,getSession能从上下文中获取会话

Answer: B,C,D

 

225.You have built your own light-weight templating mechanism. Your servlets, which handle each
request, dispatch the request to one of a small set of template JSP pages. Each template JSP controls the
layout of the view by inserting the header, body, and footer elements into specific locations within the
template page. The URLs for these three elements are stored in requestscoped variables called,
headerURL, bodyURL, and footerURL, respectively. These attribute names are never used for other
purposes. Which JSP code snippet should be used in the template JSP to insert the JSP content for the
body of the page?

你建立了自己的轻量级模板机制。你的servlet,处理每个请求,将请求分配给一个小套模板JSP页面。每个模板JSP控件的布局视图通过插入头,身体,和页脚模板页面中的元素到特定的位置。这三个元素的url存储在requestscope变量分别称为headerURL,bodyURL和footerURL。这些属性名称不用于其他目的。在JSP模板插入JSP页面的主体内容应该使用哪个JSP代码片段?
A. <jsp:insert page='${bodyURL}' />
B. <jsp:insert file='${bodyURL}' />
C. <jsp:include page='${bodyURL}' />
D. <jsp:include file='${bodyURL}' />
E. <jsp:insert page='<%= bodyURL %>' />
F. <jsp:include page='<%= bodyURL %>' />
解析:
<jsp:include  page=“ “/>动作是在请求时,装入该page所指页面。
<%@ include file=“ ”%>转译时与该页面一起翻译成java代码;

Answer: C

224.Click the Exhibit button.
A servlet sets a session-scoped attribute product with an instance of com.example.Product and
forwards to a JSP.
Which two output the name of the product in the response? (Choose two.)

单击按钮。一个servlet内设置有session作用域的属性com . example。产品的一个实例产品和前锋一个JSP。这两个输出响应中产品的名称?(选择两个。)

1.package com.example;
2.
3.public class Product{
4. private String name;
5. private double price;
6.
7. public Product(){
8.  this( "Default",0.0);
9. }
10. public Product(String name, double
price){
12.  this.name = name;
13.  this.price = price;
14. }
15.
16. public String getName(){
17.  return name;
18. }
19.
20. public void setName(String name){
21.  this.name = name;
22. }
23.
24. public double getPrice(){
25.  return price;
26. }
27. 
28. public void setPrice(double price){
29.  this.price = price;
30. }
31.}

A. ${product.name}
B. <jsp:getProperty name="product" property="name" />
C. <jsp:useBean id="com.example.Product" />
<%= product.getName() %>
D. <jsp:getProperty name="product" class="com.example.Product"
property="name" />
E. <jsp:useBean id="product" type="com.example.Product">
<%= product.getName() %>
</jsp:useBean>

解析:只问怎么输出bean的值,C、E没指定scope,D jsp:getProperty没有class属性
Answer: A,B

 

223.Click the Exhibit button.
A session-scoped attribute, product, is stored by a servlet. That servlet then forwards to a JSP page. This
attribute holds an instance of the com.example.Product class with a name property of
"The Matrix" and price property of 39.95.
Given the JSP page code snippet:
1. <jsp:useBean id='product' class='com.example.Product'>
2. <jsp:setProperty name='product' property='price' value='49.95'/>
3. </jsp:useBean>
4. <%= product.getName() %> costs <%= product.getPrice() %>
What is the response output of this JSP page code snippet?

 

1.package com.example;
2.
3.public class Product{
4. private String name;
5. private double price;
6.
7. public Product(){
8.  this( "Default",0.0);
9. }
10. public Product(String name, double
price){
12.  this.name = name;
13.  this.price = price;
14. }
15.
16. public String getName(){
17.  return name;
18. }
19.
20. public void setName(String name){
21.  this.name = name;
22. }
23.
24. public double getPrice(){
25.  return price;
26. }
27. 
28. public void setPrice(double price){
29.  this.price = price;
30. }
31.}


A. Default costs 0.0
B. Default costs 49.95
C. Default costs 39.95
D. The Matrix costs 0.0 
E. The Matrix costs 49.95
F. The Matrix costs 39.95

解析:jsp:useBean指定id和scope能找已存在的实例,因为scope默认是page所以E不对。
这里应该用scope='session'

Answer: B 

 

 

0 0
原创粉丝点击