??xml version="1.0" encoding="utf-8" standalone="yes"?> (1)假设在helloapp应用中有一个hello.jspQ它的文件\径如? (3)假设在helloapp应用中有一个HelloServletc,它在web.xml文g中的配置如下: 判断题:(x)Q其它试题请下蝲Q?/p>
1 Q?Java E序?, 创徏新的cd象用关键?new Q回收无用的cd象用关键字 free ? 2 Q对象可以赋|只要使用赋值号Q等P卛_Q相当于生成了一个各属性与赋值对象相同的新对象? 3 Q有的类定义时可以不定义构造函敎ͼ所以构造函C是必需的? 4 Q类?qing)其属性、方法可以同时有一个以上的修饰W来修饰? 5 Q?Java 的屏q坐标是以像素ؓ(f)单位Q容器的左下角被定为坐标的L(fng) 6 Q抽象方法必d抽象cMQ所以抽象类中的Ҏ(gu)都必L抽象Ҏ(gu)? 7 Q?Final cM的属性和Ҏ(gu)都必被 final 修饰W修饰? 8 Q最l类不能z子类Q最l方法不能被覆盖? 9 Q子c要调用父类的方法,必须使用 super 关键字? 10 Q一?Java cd以有多个父类? 11 Q如?p 是父c?Parent 的对象,?c 是子c?Child 的对象,则语?c = p 是正的? 12 Q一个类如果实现了某个接口,那么它必重载该接口中的所有方法? 13 Q当一个方法在q行q程中生一个异常,则这个方法会(x)l止Q但是整个程序不一定终止运行? 14 Q接口是Ҏ(gu)的类Q所以接口也可以l承Q子接口承父接口的所有常量和抽象Ҏ(gu)? 15 Q用?+ ”可以实现字W串的拼接,?- 可以从一个字W串中去除一个字W子丌Ӏ? 16 Q用方?length( ) 可以获得字符串或数组的长度? 17 Q设 String 对象 s=”Hello ?Q运行语?System.out.println(s.concat(“World!?); ?String 对象 s 的内容ؓ(f) ”Hello world!?Q所以语句输Zؓ(f) Hello world! 18 Q创?Vector 对象时构造函数给定的是其中可以包容的元素个数Q用中应注意不能超这个数倹{? 19 Q所有的鼠标事g都由 MouseListener 监听接口的监听者来处理? 20 Q一个容器中可以混合使用多种布局{略? 21 Q?Java 中,q每个事gc都只对应一个事件? 22 Q一个线E对象的具体操作是由 run() Ҏ(gu)的内容确定的Q但?Thread cȝ run() Ҏ(gu)是空?, 其中没有内容 ; 所以用L(fng)序要么派生一?Thread 的子cdƈ在子c里重新定义 run() Ҏ(gu) , 要么使一个类实现 Runnable 接口q书写其?run() Ҏ(gu)的方法体? 23 Q?Java 的源代码中定义几个类Q编译结果就生成几个?.class 为后~的字节码文g? 24 Q?Java Applet 是由独立的解释器E序来运行的? 25 Q?Java Applet 只能在图形界面下工作? 26 Q?Java 的字W类型采用的?ASCII ~码? 27 Q?Java 的各U数据类型占用固定长度,与具体的软硬件^台环境无? 28 Q?Applet 是一U特D的 Panel Q它?Java Applet E序的最外层容器? 29 Q子cȝ域和Ҏ(gu)的数目一定大于等于父cȝ域和Ҏ(gu)的数目? 30 Q?System cM能实例化Q即不能创徏 System cȝ对象? 31 Q用戯定义的图形界面元素也可以响应用户的动作,h交互功能 32 Q?Java 中数l的元素可以是简单数据类型的量,也可以是某一cȝ对象? 33 Q?Vector cM的对象不能是单数据类型? 34 Q?Java 中的 String cȝ对象既可以是字符串常量,也可以是字符串变量? 35 Q容器是用来l织其他界面成分和元素的单元Q它不能嵌套其他容器?
context - the context for the JSP page's servlet and any Web components contained in the same application.
session - the session object for the client.
request - the request triggering the execution of the JSP page.
pageScope - a java.util.Map that maps page-scoped attribute names to their values.
requestScope - a java.util.Map that maps request-scoped attribute names to their values.
sessionScope - a java.util.Map that maps session-scoped attribute names to their values.
applicationScope - a java.util.Map that maps application-scoped attribute names to their values.
param - a java.util.Map that maps parameter names to a single String parameter value (obtained by calling ServletRequest.getParameter(String name)).
paramValues - a java.util.Map that maps parameter names to a String[] of all values for that parameter (obtained by calling ServletRequest.getParameterValues(String name)).
header - a java.util.Map that maps header names to a single String header value (obtained by calling HttpServletRequest.getHeader(String name)).
headerValues - a java.util.Map that maps header names to a String[] of all values for that header.
cookie - a java.util.Map that maps cookie names to a single Cookie object. Cookies are retrieved according to the semantics of HttpServletRequest.getCookies(). If the same name is shared by multiple cookies, an implementation must use the FIRST one encountered in the array of Cookie objects returned by the getCookies() method. However, users of the cookie implicit object must be aware that the ordering of cookies is currently unspecified in the servlet specification.
initParam - a java.util.Map that maps context initialization parameter names to their String parameter value (obtained by calling ServletContext.getInitParameter(String name)).
Examples: The request's URI (obtained from HttpServletRequest):
The value of the numberOfItems property of the session-scoped attribute named cart:
The context path:
The session-scoped attribute named 'profile' (null if not found):
The String value of the productId parameter, or null if not found:
The value of the productId request parameter:
The String[] containing all values of the productId parameter, or null if not found:
A collection's members can be accessed using square brackets as shown by retrieval of the userName parameter from the param object. Members of an array or List can be accessed if the value in square brackets can be coerced to an int. The host HTTP attribute: Here is an example of accessing a page-scoped object that is called pageColor:
%CATALINA_HOME%/webapps/helloapp/hello/hello.jsp
那么在浏览器端访问hello.jsp的URL是什? Q单选)
选项:
(A) http://localhost:8080/hello.jsp
(B) http://localhost:8080/helloapp/hello.jsp
(C) http://localhost:8080/helloapp/hello/hello.jsp
(2)假设在helloapp应用中有一个HelloServletc,它位于org.javathinker包下Q那么这个类的class文g应该?br />
在什么目录下? Q单选)
选项:
(A) helloapp/HelloServlet.class
(B) helloapp/WEB-INF/HelloServlet.class
(C) helloapp/WEB-INF/classes/HelloServlet.class
(D) helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class
<servlet>
<servlet-name> HelloServlet </servlet-name>
<servlet-class>org.javathinker.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> HelloServlet </servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
那么在浏览器端访问HelloServlet的URL是什? Q单选)
选项:
(A) http://localhost:8080/HelloServlet
(B) http://localhost:8080/helloapp/HelloServlet
(C) http://localhost:8080/helloapp/org/javathinker/hello
(D) http://localhost:8080/helloapp/hello
(4)客户h讉KHTML面与访问Servlet有什么异同?Q多选)
选项:
(A)相同Q都使用HTTP协议
(B)区别Q前者Web服务器直接返回HTML面Q后者Web服务器调用Servlet的方法,由Servlet动态生成HTML面
(C)相同Q前者Web服务器直接返回HTML面,后者Web服务器直接返回Servlet的源代码?br />(D)区别Q后者需要在web.xml中配|URL路径?br />(E)区别Q前者用HTTP协议Q后者用RMI协议?br />
(5)HttpServletRequest对象是由谁创建的?Q单选)
选项:
(A)由Servlet容器负责创徏Q对于每个HTTPhQ?Servlet容器都会(x)创徏一个HttpServletRequest对象
(B)由JavaWeb应用的Servlet或JSPlg负责创徏Q当Servlet或JSPlg响应HTTPhӞ先创?br />
HttpServletRequest对象
(6)从HTTPh中,获得h参数Q应该调用哪个方? Q单选)
选项:
(A)调用HttpServletRequest对象的getAttribute()Ҏ(gu)
(B)调用ServletContext对象的getAttribute()Ҏ(gu)
(C)调用HttpServletRequest对象的getParameter()Ҏ(gu)
(7)ServletContext对象是由谁创建的?Q单选)
选项:
(A)由Servlet容器负责创徏Q对于每个HTTPhQ?Servlet容器都会(x)创徏一个ServletContext对象
(B)由JavaWeb应用本n负责己创Z个ServletContext对象
(C)由Servlet容器负责创徏Q对于每个JavaWeb应用Q在启动ӞServlet容器都会(x)创徏一个ServletContext对象
(8)jspForward1.jsp要把h转发ljspForward2.jspQ应该在jspForward1.jsp中如何实? Q单选)
选项Q?br />(A) <a href=“jspForward2.jsp?gt;jspForward2.jsp </a>
(B) <jsp:forward page=“jspForward2.jsp?gt;
(9)当浏览器W二ơ访问以下JSP|页时的输出l果是什?Q单选)
<!% int a=0; %>
<%
int b=0;
a++;
b++;
%>
a:<%= a %> <br>
b:<%= b %>
选项Q?br />(A) a=0 b=0
(B) a=1 b=1
(c) a=2 b=1
(10)下面哪个说法是正的? Q单选)
选项Q?br />(A) 对于每个要求讉Kmaillogin.jsp的HTTPhQServlet容器都会(x)创徏一个HttpSession对象
(B)每个HttpSession对象都有惟一的ID?br />(C)JavaWeb应用E序必须负责为HttpSession分配惟一的ID
(11)如果不希望JSP|页支持SessionQ应该如何办? Q单选)
选项Q?br />(A) 调用HttpSession的invalidate()Ҏ(gu)
(B) <%@ page session= “false\">
(12)在标{֤理类中,如何讉Ksession范围内的׃n数据? Q多选)
选项Q?br />(A)在TagSupportcM定义了session成员变量Q直接调用它的getAttribute()Ҏ(gu)卛_?br />(B)在标{֤理类TagSupportcM定义了pageContext成员变量Q先通过它的getSession()Ҏ(gu)获得当前?br />
HttpSession对象Q再调用HttpSession对象的getAttribute()Ҏ(gu)?br />(C)pageContext.getAttribute(“attributename?PageContext.SESSION_SCOPE)
(13)在下面的选项中,哪些是TagSupportcȝdoStartTag()Ҏ(gu)的有效返回? Q多选)
选项Q?br />(A) Tag.SKIP_BODY
(B) Tag.SKIY_PAGE
(C) Tag.EVAL_BODY_INCLUDE
(D) Tag.EVAL_PAGE
(14)以下代码能否~译通过Q假如能~译通过Q运行时得到什么打印结果?Q单选)
request.setAttribute(\"count\",new Integer(0));
Integer count = request.getAttribute(\"count\");
选项Q?br />A)不能~译通过 B)能编译通过Qƈ正常q行
C) ~译通过Q但q行时抛出ClassCastException
?/font>{案Q?br />(1)C (2)D (3)D (4)A,B,D (5)A (6)C (7)C (8)B (9)C (10)B
(11)B (12)B,C (13)A,C (14)A
]]>
${pageContext.request.requestURI}
${sessionScope.cart.numberOfItems}
${pageContext.request.contextPath}
${sessionScope.profile}
${param.productId}
${param["productId"]}
${paramValues.productId}
<html>
<head><title>Customer Profile for ${param["userName"]}</title></head>
<body>
...
</body>
</html>
Maps can be accessed using the dot operator OR square brackets. For example, ${param.userName} is EQUIVALENT to ${param["userName"]}.
${header["host"]}
<body bgcolor="${pageScope.pageColor}">
it is equivalent to: <body bgcolor="${pageScope['pageColor']}">
]]>
Since a single servlet instance may be handling multiple service requests at any given time.
Since multiple servlets and /or service requests may try to access a class variable concurrently.
Since the request object is a local variable
Since sessions are scoped at the web application level, hence the same session object can be accessed concurrently by multiple servlets and their service requests
Since the same context object can be accessed concurrently by multiple servlets and their service requests
]]>
]]>
You need to create a database connection in your application after reading the username, password, and database server URL from the deployment descriptor. Which will be the best place to do this?
Choices:
Correct choice:
Explanation:
The init() method is invoked once and only once by the container, so the creation of the database connection will be done only once, which is appropriate. The service() , doGet() , and doPost() methods might be called many times by the container.
The username, password, and URL are to be read from the deployment descriptor. These initialization parameters are contained in the ServletConfig object, which is passed to the init() method. That is why we need to use the init() method instead of the constructor for this purpose, even though the constructor is also called only once.
Question 2:
A user can select multiple locations from a list box on an HTML form. Which of the following methods can be used to retrieve all the selected locations?
Choices:
Correct choice:
Explanation:
The getParameterValues(String paraName) method of the ServletRequest interface returns all the values associated with a parameter. It returns an array of Strings containing the values. The getParameter() method returns just one of the values associated with the given parameter, so choice A is incorrect. There are no methods named getParameters() or getParamValues() , so choices B and D are incorrect.
Consider the following servlet code:
public class MyServlet extends HttpServletWhich of the following variables in the above code are thread safe?
Choices:
Correct choices:
Explanation:
The static variable i is thread safe because it is final (cannot be modified), or else it would not have been safe. Request and response objects are scoped only for the lifetime of the request, so they are also thread safe. Session and ServletContext objects can be accessed from multiple threads while processing multiple requests, so they are not thread safe. However, in this case, the ServletContext object is synchronized, so it can be accessed only by one thread at a time. obj is not thread safe because even though the ServletContext object is synchronized, its attributes are not. They need to be synchronized separately. Hence choices B and E are incorrect and choices A, C, D and F are correct.
Question 2:
Which of the following statements are true?
Choices:
Correct choices:
Explanation:
When SingleThreadModel is implemented, the servlet container ensures that only one thread is executing the servlet's method at a time. So what will happen for multiple requests? In that case, the container may instantiate multiple instances of the servlet to handle multiple requests, so option A is correct and B is incorrect.
If the SingleThreadModel interface is not implemented, a servlet uses the multi-threaded model (that is, multiple threads can access the methods of the servlet). Static variables can be accessed through multiple instances of the same class, so they are not always thread safe. Hence choices B and C are incorrect and choices A and D are correct.