JAVA培訓題庫-Servlet__JAVA

來源:互聯網
上載者:User

1.Servlet程式的進入點是:( )

A  init() 

B  main() 

C  service() 

D  doGet()

參考答案:A

2. Severlet中,HttpSeverletResponse的什麼方法用來把一個HTTP請求重新導向到另外的URL。( )

A  sendURL()

B  redirectURL()

C  sendRedirect()

D  redirectResponse()

參考答案:C

3. J2EE中,當把來自客戶機的HTTP請求委託給servlet時,會調用HttpServlet的什麼方法。( )

A  service()

B  doGet()

C  doPost()

D  init()

參考答案:A

4. 給定一個Servlet程式的程式碼片段,如下:

Public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException{

request.getSession().getAttribute(“A”);}

假定第二行返回的對象引用不是null,那麼這個Object Storage Service在什麼範圍中。( )

A  page

B  session

C  request

D  application

參考答案:B

5. 給定某程式的片斷如下,使用者在瀏覽器地址欄中鍵入正確的請求URL並斷行符號後,在控制台上顯示的結果是:( )

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException{

System.out.println(“get”);

}

Public void doPost(HttpServletRequest request,HttpServletResponse response)

Throws ServletException,IOException{

System.out.println(“post”);

}

A  get

B  post

C  get Post

D  post Get

參考答案:A

6. 在J2EE中,下列哪個語句可以擷取頁面請求中一個文字框的輸入(文字框的名稱為title)。( )

A  request.getParameter("title")

B  request.getAttribute("title")

C  request.getParameterValues("title")

D  request.getParameters("title")

參考答案:A

7. 在Severlet過濾器的生命週期方法中,每當傳遞請求或響應時,web容器會調用什麼方法。()

A  init

B  service

C  doFilter

D  destroy

參考答案:C

8. 不能在不同使用者之間共用資料的方法是:( )

A  通過cookie 

B  利用檔案系統

C  利用資料庫 

D  通過ServletContext對象

參考答案:A

9. 假設在helloapp應用中有一個HelloServlet類,它在web.xml檔案中的配置如下:

<servlet>

  <servlet-name> HelloServlet </servlet-name>

  <servlet-class>com.xdfyb.HelloServlet</servlet-class>

</servlet>

 

<servlet-mapping>

  <servlet-name> HelloServlet </servlet-name>

  <url-pattern>/hello</url-pattern>

</servlet-mapping>

那麼在瀏覽器端訪問HelloServlet的URL是什麼。 ( )

A  http://localhost:8080/HelloServlet

B  http://localhost:8080/helloapp/HelloServlet

C  http://localhost:8080/helloapp/com/xdfyb/hello

D  http://localhost:8080/helloapp/com/xdfyb/HelloServlet

E  http://localhost:8080/helloapp/hello

參考答案:E

 

1. 請描述一下一個Servlet組件的生命週期。

參考答案:Servlet有良好的生存期的定義,包括載入和執行個體化、初始化、處理請求以及服務結束。這個生存期由javax.servlet.Servlet介面的init,service和destroy方法表達。 Servlet被伺服器執行個體化後,容器運行其init方法,請求到達時運行其service方法,service方法自動派遣運行與請求對應的doXXX方法(doGet,doPost)等,當伺服器決定將執行個體銷毀的時候調用其destroy方法。

2. 請回答:Servlet的init()方法和service()方法的區別是什麼。

參考答案:初始化時調用init()方法有請求到達時調用service()方法,service()根據請求的類型,調用doget()或depost()等方法

3. 請回答:Servlet執行時一般實現哪幾個方法。

參考答案:Servlet在執行的時候一般需要實現以下的幾個方法:

public void init(ServletConfig config)

public ServletConfig getServletConfig()

public String getServletInfo()

public void service(ServletRequest request,ServletResponse response)

public void destroy()

4. 請回答:Servlet API中forward() 與redirect()兩個方法的區別是什麼。

參考答案:前者僅是容器中控制權的轉向,在用戶端瀏覽器地址欄中不會顯示出轉向後的地址;後者則是完全的跳轉,瀏覽器將會得到跳轉的地址,並重新發送請求連結。這樣,從瀏覽器的地址欄中可以看到跳轉後的連結地址。所以,前者更加高效,在前者可以滿足需要時,盡量使用forward()方法,並且,這樣也有助於隱藏實際的連結。在有些情況下,比如,需要跳轉到一個其它伺服器上的資源,則必須使用sendRedirect()方法。

5. Servlet的基本架構是什麼樣的,請寫個簡單的樣本。

參考答案:

public class ServletName extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException  {

}

public void doGet(HttpServletRequest request, HttpServletResponse

response) throws servletException, IOException  {

  }

6. 簡答:在什麼情況下調用doGet()方法,什麼情況下調用doPost()方法。

參考答案:Jsp頁面中的form標籤裡的method屬性為get時或預設沒有method屬性時調用doGet(),Jsp頁面中的form標籤裡的method屬性為post時調用doPost()。

7. 我們在web應用開發過程中經常遇到輸出某種編碼的字元,如iso8859-1等,請寫一個能夠輸出符合某種編碼的字串的小例子。

參考答案:

public String translate (String str) {

  String tempStr = "";

  try {

    tempStr = new String(str.getBytes("ISO-8859-1"), "GBK");

    tempStr = tempStr.trim();

  }

  catch (Exception e) {

    System.err.println(e.getMessage());

  }

  return tempStr;

}

8. 在一個Servlet應用中的設定檔web.xml 的作用是什麼。

參考答案:在一個Servlet應用中的設定檔web.xml用於配置web 應用的資訊;如listener、filter 及servlet 的配置資訊等。

9. 請描述:Httprequest的 getParameter()方法與getAttribute()方法的區別。

參考答案:Httprequest的 getParameter()方法與getAttribute()方法的區別主要列為以下幾點:(1)getParameter得到的都是String類型的。或者是http://a.jsp?id=123中的123,或者是某個表單提交過去的資料。 getAttribute則可以是對象。(2)getParameter()是擷取POST/GET傳遞的參數值;getAttribute()是擷取對象容器中的資料值;(3)getParameter:用於用戶端重新導向時,即點擊了連結或提交按扭時傳值用,即用於在用表單或url重新導向傳值時接收資料用。 getAttribute:用於伺服器端重新導向時,即在sevlet中使用了forward函數,或struts中使用了mapping.findForward。getAttribute只能收到程式用setAttribute傳過來的值。(4)getParameter()是擷取POST/GET傳遞的參數值; getAttribute()是擷取SESSION的值;另外,可以用setAttribute,getAttribute發送接收對象.而getParameter顯然只能傳字串。(5)getParamete返回的是String, 用於讀取提交的表單中的值;getAttribute 返回的是Object,需進行轉換,可用setAttribute設定成任意對象,使用很靈活,可隨時用;從更深的層次考慮,request.getParameter()方法傳遞的資料,會從Web用戶端傳到Web伺服器端,代表HTTP請求資料。request.getParameter()方法返回String類型的資料。 (6)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.