﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Sun River-随笔分类-Servlet &amp; Jsp</title><link>http://www.blogjava.net/SunRiver/category/15982.html</link><description>Topics about J2EE XML AJAX SOA DB etc.</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 11:58:25 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 11:58:25 GMT</pubDate><ttl>60</ttl><item><title>JSP quiz</title><link>http://www.blogjava.net/SunRiver/archive/2007/02/22/100337.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Thu, 22 Feb 2007 03:59:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2007/02/22/100337.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/100337.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2007/02/22/100337.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/100337.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/100337.html</trackback:ping><description><![CDATA[
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td valign="top" align="left" width="418" height="86">
										<p>(1)假设在helloapp应用中有一个hello.jsp，它的文件路径如下:<br />%CATALINA_HOME%/webapps/helloapp/hello/hello.jsp    <br />那么在浏览器端访问hello.jsp的URL是什么? （单选）<br />选项:<br />(A) http://localhost:8080/hello.jsp<br />(B) http://localhost:8080/helloapp/hello.jsp<br />(C) http://localhost:8080/helloapp/hello/hello.jsp<br /><br />(2)假设在helloapp应用中有一个HelloServlet类，它位于org.javathinker包下，那么这个类的class文件应该放<br /><br />在什么目录下? （单选）<br />选项:<br />(A) helloapp/HelloServlet.class<br />(B) helloapp/WEB-INF/HelloServlet.class<br />(C) helloapp/WEB-INF/classes/HelloServlet.class<br />(D) helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class<br /></p>
								</td>
								<td valign="top" width="266">
										<div align="center">
												<script type="text/javascript">
														<!--
google_ad_client = "pub-1832179689702023";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "3366FF";
google_color_bg = "FFFFFF";
google_color_link = "3366FF";
google_color_url = "008000";
google_color_text = "000000";
//-->
												</script>
												<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
												</script>
										</div>
								</td>
						</tr>
				</tbody>
		</table>
		<p>(3)假设在helloapp应用中有一个HelloServlet类，它在web.xml文件中的配置如下:<br />&lt;servlet&gt;<br />  &lt;servlet-name&gt; HelloServlet &lt;/servlet-name&gt;<br />  &lt;servlet-class&gt;org.javathinker.HelloServlet&lt;/servlet-class&gt;<br />&lt;/servlet&gt;<br /><br />&lt;servlet-mapping&gt;<br />  &lt;servlet-name&gt; HelloServlet &lt;/servlet-name&gt;<br />  &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;<br />&lt;/servlet-mapping&gt;<br />那么在浏览器端访问HelloServlet的URL是什么? （单选）<br />选项:<br />(A) http://localhost:8080/HelloServlet<br />(B) http://localhost:8080/helloapp/HelloServlet <br />(C) http://localhost:8080/helloapp/org/javathinker/hello<br />(D) http://localhost:8080/helloapp/hello<br /><br /><br />(4)客户请求访问HTML页面与访问Servlet有什么异同？（多选）<br />选项:<br />(A)相同：都使用HTTP协议<br />(B)区别：前者Web服务器直接返回HTML页面，后者Web服务器调用Servlet的方法，由Servlet动态生成HTML页面<br />(C)相同：前者Web服务器直接返回HTML页面,后者Web服务器直接返回Servlet的源代码。<br />(D)区别：后者需要在web.xml中配置URL路径。<br />(E)区别：前者使用HTTP协议，后者使用RMI协议。<br /><br /><br />(5)HttpServletRequest对象是由谁创建的?（单选）<br />选项:<br />(A)由Servlet容器负责创建，对于每个HTTP请求， Servlet容器都会创建一个HttpServletRequest对象<br />(B)由JavaWeb应用的Servlet或JSP组件负责创建，当Servlet或JSP组件响应HTTP请求时，先创建<br /><br />HttpServletRequest对象<br /><br /><br />(6)从HTTP请求中，获得请求参数，应该调用哪个方法? （单选）<br />选项:<br />(A)调用HttpServletRequest对象的getAttribute()方法<br />(B)调用ServletContext对象的getAttribute()方法<br />(C)调用HttpServletRequest对象的getParameter()方法<br /><br />(7)ServletContext对象是由谁创建的?（单选）<br />选项:<br />(A)由Servlet容器负责创建，对于每个HTTP请求， Servlet容器都会创建一个ServletContext对象<br />(B)由JavaWeb应用本身负责为自己创建一个ServletContext对象<br />(C)由Servlet容器负责创建，对于每个JavaWeb应用，在启动时，Servlet容器都会创建一个ServletContext对象<br /><br /><br />(8)jspForward1.jsp要把请求转发给jspForward2.jsp，应该在jspForward1.jsp中如何实现? （单选）<br />选项：<br />(A) &lt;a href=“jspForward2.jsp”&gt;jspForward2.jsp &lt;/a&gt;<br />(B) &lt;jsp:forward page=“jspForward2.jsp”&gt;<br /><br />(9)当浏览器第二次访问以下JSP网页时的输出结果是什么?（单选）<br /><br />&lt;!% int a=0;     %&gt;<br />&lt;%<br />     int b=0; <br />     a++;<br />     b++; <br />%&gt;<br /><br />a:&lt;%= a %&gt; &lt;br&gt;<br />b:&lt;%= b %&gt;<br />   <br />选项：<br />(A)  a=0  b=0<br />(B) a=1  b=1<br />(c) a=2  b=1<br /><br />(10)下面哪个说法是正确的? （单选）<br />选项：<br />(A) 对于每个要求访问maillogin.jsp的HTTP请求，Servlet容器都会创建一个HttpSession对象<br />(B)每个HttpSession对象都有惟一的ID。<br />(C)JavaWeb应用程序必须负责为HttpSession分配惟一的ID<br /><br />(11)如果不希望JSP网页支持Session，应该如何办? （单选）<br />选项：<br />(A) 调用HttpSession的invalidate()方法 <br />(B) &lt;%@ page session= “false\"&gt;<br /><br /><br />(12)在标签处理类中，如何访问session范围内的共享数据? （多选）<br />选项：<br />(A)在TagSupport类中定义了session成员变量，直接调用它的getAttribute()方法即可。<br />(B)在标签处理类TagSupport类中定义了pageContext成员变量，先通过它的getSession()方法获得当前的<br /><br />HttpSession对象，再调用HttpSession对象的getAttribute()方法。<br />(C)pageContext.getAttribute(“attributename”,PageContext.SESSION_SCOPE)<br /><br /><br />(13)在下面的选项中，哪些是TagSupport类的doStartTag()方法的有效返回值? （多选）<br />选项：<br />(A) Tag.SKIP_BODY<br />(B) Tag.SKIY_PAGE<br />(C) Tag.EVAL_BODY_INCLUDE<br />(D) Tag.EVAL_PAGE<br /><br /><br />(14)以下代码能否编译通过，假如能编译通过，运行时得到什么打印结果？（单选）<br />request.setAttribute(\"count\",new Integer(0));<br />Integer count = request.getAttribute(\"count\");<br />选项：<br />A)不能编译通过  B)能编译通过，并正常运行  <br />C) 编译通过，但运行时抛出ClassCastException<br /><br /><u><font color="#0000ff">》</font></u>答案：<br />(1)C (2)D  (3)D  (4)A,B,D  (5)A  (6)C  (7)C  (8)B  (9)C   (10)B<br />(11)B  (12)B,C  (13)A,C  (14)A <br /></p>
		<p class="style1">判断题：（其它试题请下载）</p>
		<p align="left">1 ． Java 程序里 , 创建新的类对象用关键字 new ，回收无用的类对象使用关键字 free 。 </p>
		<p>2 ．对象可以赋值，只要使用赋值号（等号）即可，相当于生成了一个各属性与赋值对象相同的新对象。 </p>
		<p align="left">3 ．有的类定义时可以不定义构造函数，所以构造函数不是必需的。 </p>
		<p align="left">4 ．类及其属性、方法可以同时有一个以上的修饰符来修饰。 </p>
		<p align="left">5 ． Java 的屏幕坐标是以像素为单位，容器的左下角被确定为坐标的起点 </p>
		<p align="left">6 ．抽象方法必须在抽象类中，所以抽象类中的方法都必须是抽象方法。 </p>
		<p align="left">7 ． Final 类中的属性和方法都必须被 final 修饰符修饰。 </p>
		<p align="left">8 ．最终类不能派生子类，最终方法不能被覆盖。 </p>
		<p align="left">9 ．子类要调用父类的方法，必须使用 super 关键字。 </p>
		<p align="left">10 ．一个 Java 类可以有多个父类。 </p>
		<p align="left">11 ．如果 p 是父类 Parent 的对象，而 c 是子类 Child 的对象，则语句 c = p 是正确的。 </p>
		<p align="left">12 ．一个类如果实现了某个接口，那么它必须重载该接口中的所有方法。 </p>
		<p align="left">13 ．当一个方法在运行过程中产生一个异常，则这个方法会终止，但是整个程序不一定终止运行。 </p>
		<p align="left">14 ．接口是特殊的类，所以接口也可以继承，子接口将继承父接口的所有常量和抽象方法。 </p>
		<p align="left">15 ．用“ + ”可以实现字符串的拼接，用 - 可以从一个字符串中去除一个字符子串。 </p>
		<p align="left">16 ．使用方法 length( ) 可以获得字符串或数组的长度。 </p>
		<p align="left">17 ．设 String 对象 s=”Hello ” ，运行语句 System.out.println(s.concat(“World!”)); 后 String 对象 s 的内容为 ”Hello world!” ，所以语句输出为 </p>
		<p align="left">Hello world! </p>
		<p align="left">18 ．创建 Vector 对象时构造函数给定的是其中可以包容的元素个数，使用中应注意不能超越这个数值。 </p>
		<p align="left">19 ．所有的鼠标事件都由 MouseListener 监听接口的监听者来处理。 </p>
		<p align="left">20 ．一个容器中可以混合使用多种布局策略。 </p>
		<p align="left">21 ． Java 中，并非每个事件类都只对应一个事件。 </p>
		<p align="left">22 ．一个线程对象的具体操作是由 run() 方法的内容确定的，但是 Thread 类的 run() 方法是空的 , 其中没有内容 ; 所以用户程序要么派生一个 Thread 的子类并在子类里重新定义 run() 方法 , 要么使一个类实现 Runnable 接口并书写其中 run() 方法的方法体。 </p>
		<p align="left">23 ． Java 的源代码中定义几个类，编译结果就生成几个以 .class 为后缀的字节码文件。 </p>
		<p align="left">24 ． Java Applet 是由独立的解释器程序来运行的。 </p>
		<p align="left">25 ． Java Applet 只能在图形界面下工作。 </p>
		<p align="left">26 ． Java 的字符类型采用的是 ASCII 编码。 </p>
		<p align="left">27 ． Java 的各种数据类型占用固定长度，与具体的软硬件平台环境无关 </p>
		<p align="left">28 ． Applet 是一种特殊的 Panel ，它是 Java Applet 程序的最外层容器。 </p>
		<p align="left">29 ．子类的域和方法的数目一定大于等于父类的域和方法的数目。 </p>
		<p align="left">30 ． System 类不能实例化，即不能创建 System 类的对象。 </p>
		<p align="left">31 ．用户自定义的图形界面元素也可以响应用户的动作，具有交互功能 </p>
		<p align="left">32 ． Java 中数组的元素可以是简单数据类型的量，也可以是某一类的对象。 </p>
		<p align="left">33 ． Vector 类中的对象不能是简单数据类型。 </p>
		<p align="left">34 ． Java 中的 String 类的对象既可以是字符串常量，也可以是字符串变量。 </p>
		<p align="left">35 ．容器是用来组织其他界面成分和元素的单元，它不能嵌套其他容器。 </p>
<img src ="http://www.blogjava.net/SunRiver/aggbug/100337.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2007-02-22 11:59 <a href="http://www.blogjava.net/SunRiver/archive/2007/02/22/100337.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EL implicit objects</title><link>http://www.blogjava.net/SunRiver/archive/2006/10/16/75332.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Sun, 15 Oct 2006 17:19:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2006/10/16/75332.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/75332.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2006/10/16/75332.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/75332.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/75332.html</trackback:ping><description><![CDATA[
		<div>
		</div>
		<div class="section" lang="en">
				<div class="titlepage">
						<div>
								<div>
										<h2 class="title" style="CLEAR: both">
												<a name="c7s1">
												</a>
												<tt class="filename">pageContext</tt> - the <tt class="filename">PageContext</tt> object. Provides an API to access various objects including: </h2>
								</div>
						</div>
				</div>
				<div class="itemizedlist">
						<ul type="disc">
								<li>
										<div class="itemizedlist">
												<ul type="circle">
														<li>
																<p>
																		<tt class="filename">context</tt> - the context for the JSP page's servlet and any Web components contained in the same application. </p>
														</li>
														<li>
																<p>
																		<tt class="filename">session</tt> - the session object for the client. </p>
														</li>
														<li>
																<p>
																		<tt class="filename">request</tt> - the request triggering the execution of the JSP page. </p>
														</li>
												</ul>
										</div>
										<p>
										</p>
								</li>
								<li>
										<p>
												<tt class="filename">pageScope</tt> - a <tt class="filename">java.util.Map</tt> that maps page-scoped attribute names to their values. </p>
								</li>
								<li>
										<p>
												<tt class="filename">requestScope</tt> - a <tt class="filename">java.util.Map</tt> that maps request-scoped attribute names to their values. </p>
								</li>
								<li>
										<p>
												<tt class="filename">sessionScope</tt> - a <tt class="filename">java.util.Map</tt> that maps session-scoped attribute names to their values. </p>
								</li>
								<li>
										<p>
												<tt class="filename">applicationScope</tt> - a <tt class="filename">java.util.Map</tt> that maps application-scoped attribute names to their values. </p>
								</li>
								<li>
										<p>
												<tt class="filename">param</tt> - a <tt class="filename">java.util.Map</tt> that maps parameter names to a single <tt class="filename">String</tt> parameter value (obtained by calling <tt class="function">ServletRequest.getParameter(String name)</tt>). </p>
								</li>
								<li>
										<p>
												<tt class="filename">paramValues</tt> - a <tt class="filename">java.util.Map</tt> that maps parameter names to a <tt class="filename">String[]</tt> of all values for that parameter (obtained by calling <tt class="function">ServletRequest.getParameterValues(String name)</tt>). </p>
								</li>
								<li>
										<p>
												<tt class="filename">header</tt> - a <tt class="filename">java.util.Map</tt> that maps header names to a single String header value (obtained by calling <tt class="function">HttpServletRequest.getHeader(String name)</tt>). </p>
								</li>
								<li>
										<p>
												<tt class="filename">headerValues</tt> - a <tt class="filename">java.util.Map</tt> that maps header names to a <tt class="filename">String[]</tt> of all values for that header. </p>
								</li>
								<li>
										<p>
												<tt class="filename">cookie</tt> - a <tt class="filename">java.util.Map</tt> that maps cookie names to a single <tt class="filename">Cookie</tt> object. Cookies are retrieved according to the semantics of <tt class="function">HttpServletRequest.getCookies()</tt>. If the same name is shared by multiple cookies, an implementation must use the FIRST one encountered in the array of <tt class="filename">Cookie</tt> objects returned by the <tt class="function">getCookies()</tt> method. However, users of the <tt class="filename">cookie</tt> implicit object must be aware that the ordering of cookies is currently unspecified in the servlet specification. </p>
								</li>
								<li>
										<p>
												<tt class="filename">initParam</tt> - a <tt class="filename">java.util.Map</tt> that maps context initialization parameter names to their <tt class="filename">String</tt> parameter value (obtained by calling <tt class="function">ServletContext.getInitParameter(String name)</tt>). </p>
								</li>
						</ul>
				</div>
				<p>
				</p>
				<p>Examples: </p>
				<p>The request's URI (obtained from <tt class="filename">HttpServletRequest</tt>): </p>
				<pre class="programlisting">${pageContext.request.requestURI}
					</pre>
				<p>
				</p>
				<p>The value of the <tt class="filename">numberOfItems</tt> property of the session-scoped attribute named <tt class="filename">cart</tt>: </p>
				<pre class="programlisting">${sessionScope.cart.numberOfItems}
					</pre>
				<p>
				</p>
				<p>The context path: </p>
				<pre class="programlisting">${pageContext.request.contextPath}
					</pre>
				<p>
				</p>
				<p>The session-scoped attribute named '<tt class="filename">profile</tt>' (<tt class="filename">null</tt> if not found): </p>
				<pre class="programlisting">${sessionScope.profile} 
					</pre>
				<p>
				</p>
				<p>The <tt class="filename">String</tt> value of the <tt class="filename">productId</tt> parameter, or <tt class="filename">null</tt> if not found: </p>
				<pre class="programlisting">${param.productId} 
					</pre>
				<p>
				</p>
				<p>The value of the <tt class="filename">productId</tt> request parameter: </p>
				<pre class="programlisting">${param["productId"]}
					</pre>
				<p>
				</p>
				<p>The <tt class="filename">String[]</tt> containing all values of the <tt class="filename">productId</tt> parameter, or <tt class="filename">null</tt> if not found: </p>
				<pre class="programlisting">${paramValues.productId} 
					</pre>
				<p>
				</p>
				<p>A collection's members can be accessed using square brackets as shown by retrieval of the <tt class="filename">userName</tt> parameter from the <tt class="filename">param</tt> object. Members of an array or <tt class="filename">List</tt> can be accessed if the value in square brackets can be coerced to an <tt class="filename">int</tt>. </p>
				<pre class="programlisting">&lt;html&gt;
	&lt;head&gt;&lt;title&gt;Customer Profile for ${param["userName"]}&lt;/title&gt;&lt;/head&gt;
	&lt;body&gt;
		...
	&lt;/body&gt;
&lt;/html&gt;

					</pre>Maps can be accessed using the dot operator OR square brackets. For example, <tt class="filename">${param.userName}</tt> is EQUIVALENT to <tt class="filename">${param["userName"]}</tt>. 
<p></p><p>The <tt class="filename">host</tt> HTTP attribute: </p><pre class="programlisting">${header["host"]}
					</pre><p></p><p>Here is an example of accessing a page-scoped object that is called <tt class="filename">pageColor</tt>: </p><pre class="programlisting">&lt;body bgcolor="${pageScope.pageColor}"&gt; 

					</pre>it is equivalent to: <pre class="programlisting">&lt;body bgcolor="${pageScope['pageColor']}"&gt;

					</pre><p></p></div>
<img src ="http://www.blogjava.net/SunRiver/aggbug/75332.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2006-10-16 01:19 <a href="http://www.blogjava.net/SunRiver/archive/2006/10/16/75332.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>thread-safe</title><link>http://www.blogjava.net/SunRiver/archive/2006/10/08/73752.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Sun, 08 Oct 2006 04:41:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2006/10/08/73752.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/73752.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2006/10/08/73752.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/73752.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/73752.html</trackback:ping><description><![CDATA[
		<font color="#ff0000">Identify which attribute scopes are thread-safe: </font>
		<ol>
				<li>
						<font color="#ff0000">Local variables </font>Yes, thread-safe <font color="red"><li>Instance variables </li></font>Not thread-safe<br />Since a single servlet instance may be handling multiple service requests at any given time. <font color="red"><li>Class variables </li></font>Not thread-safe<br />Since multiple servlets and /or service requests may try to access a class variable concurrently. <font color="red"><li>Request attributes </li></font>Yes, thread-safe<br />Since the request object is a local variable <font color="red"><li>Session attributes </li></font>Not thread-safe<br />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 <font color="red"><li>Context attributes </li></font>Not thread-safe<br />Since the same context object can be accessed concurrently by multiple servlets and their service requests </li>
		</ol>
<img src ="http://www.blogjava.net/SunRiver/aggbug/73752.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2006-10-08 12:41 <a href="http://www.blogjava.net/SunRiver/archive/2006/10/08/73752.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Jsp LifeCycle</title><link>http://www.blogjava.net/SunRiver/archive/2006/10/08/73748.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Sun, 08 Oct 2006 04:21:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2006/10/08/73748.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/73748.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2006/10/08/73748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/73748.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/73748.html</trackback:ping><description><![CDATA[
		<font color="#ff0000"> </font>
		<ol>
				<li>Page translation <br /><font color="red"><li>JSP page compilation </li></font><br /><font color="red"><li>Load class </li></font><br /><font color="red"><li>Create instance </li></font><br /><font color="red"><li>Call jspInit </li></font><br /><font color="red"><li>Call _jspService </li></font><br /><font color="red"><li>Call jspDestroy </li></font><br /></li>
		</ol>
<img src ="http://www.blogjava.net/SunRiver/aggbug/73748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2006-10-08 12:21 <a href="http://www.blogjava.net/SunRiver/archive/2006/10/08/73748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Questions about JSP&amp;Servlet</title><link>http://www.blogjava.net/SunRiver/archive/2006/10/08/73682.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Sat, 07 Oct 2006 21:01:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2006/10/08/73682.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/73682.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2006/10/08/73682.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/73682.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/73682.html</trackback:ping><description><![CDATA[
		<p>
				<strong>Question 1: </strong>
		</p>
		<p>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? </p>
		<p>
				<strong>Choices: </strong>
		</p>
		<ul>
				<li>
						<strong>A. </strong>Servlet constructor 
</li>
				<li>
						<strong>B. </strong>init() method 
</li>
				<li>
						<strong>C. </strong>service() method 
</li>
				<li>
						<strong>D. </strong>doGet() method 
</li>
				<li>
						<strong>E. </strong>doPost() method </li>
		</ul>
		<p>
				<strong>Correct choice: </strong>
		</p>
		<ul>
				<li>
						<strong>B </strong>
				</li>
		</ul>
		<p>
				<strong>Explanation: </strong>
		</p>
		<p>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. </p>
		<p>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. </p>
		<p>
				<strong>Question 2: </strong>
		</p>
		<p>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? </p>
		<p>
				<strong>Choices: </strong>
		</p>
		<ul>
				<li>
						<strong>A. </strong>getParameter() 
</li>
				<li>
						<strong>B. </strong>getParameters() 
</li>
				<li>
						<strong>C. </strong>getParameterValues() 
</li>
				<li>
						<strong>D. </strong>getParamValues() 
</li>
				<li>
						<strong>E. </strong>None of the above </li>
		</ul>
		<p>
				<strong>Correct choice: </strong>
		</p>
		<ul>
				<li>
						<strong>C </strong>
				</li>
		</ul>
		<p>
				<strong>Explanation: </strong>
		</p>
		<p>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. </p>
<img src ="http://www.blogjava.net/SunRiver/aggbug/73682.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2006-10-08 05:01 <a href="http://www.blogjava.net/SunRiver/archive/2006/10/08/73682.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Questions</title><link>http://www.blogjava.net/SunRiver/archive/2006/10/08/73681.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Sat, 07 Oct 2006 20:29:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2006/10/08/73681.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/73681.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2006/10/08/73681.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/73681.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/73681.html</trackback:ping><description><![CDATA[
		<strong>Question 1: </strong>
		<p>Consider the following servlet code: </p>public class MyServlet extends HttpServlet <br />{<br />final static int i=0;<br />public void doGet(HttpServletRequest req, HttpServletResponse res) <br />{<br />private HttpSession session=req.getSession();<br />private ServletContext ctx=getServletContext();<br />synchronized(ctx) <br />{<br />Object obj=ctx.getAttribute(); // code to alter obj <br />}<br />}<br />} 
<p>Which of the following variables in the above code are thread safe? </p><p><strong>Choices: </strong></p><ul><li><strong>A. </strong>i 
</li><li><strong>B. </strong>session 
</li><li><strong>C. </strong>ctx 
</li><li><strong>D. </strong>req 
</li><li><strong>E. </strong>obj 
</li><li><strong>F. </strong>res </li></ul><p><strong>Correct choices: </strong></p><ul><li><strong>A </strong>, <strong>C </strong>, <strong>D </strong>, and <strong>F </strong></li></ul><p><strong>Explanation: </strong></p><p>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. </p><p><strong>Question 2: </strong></p><p>Which of the following statements are true? </p><p><strong>Choices: </strong></p><ul><li><strong>A. </strong>Multiple instances may be created on a servlet implementing SingleThreadModel 
</li><li><strong>B. </strong>No more than one instance is created for a servlet implementing SingleThreadModel 
</li><li><strong>C. </strong>Even static variables in a SingleThreadModel servlet are thread safe 
</li><li><strong>D. </strong>If no threading model is implemented, by default a servlet is executed in a multi-threaded model </li></ul><p><strong>Correct choices: </strong></p><ul><li><strong>A </strong>and <strong>D </strong></li></ul><p><strong>Explanation: </strong></p><p>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. </p><p>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. <br /></p><img src ="http://www.blogjava.net/SunRiver/aggbug/73681.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2006-10-08 04:29 <a href="http://www.blogjava.net/SunRiver/archive/2006/10/08/73681.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>