Jsp & amp; servlet interview questions

Source: Internet
Author: User

1. Three Statment differences and usage

Statment, basic; PreparedStatement is compiled to improve efficiency, callablestatment, stored procedure

2 Cookie

A: The temporary cookie exists in the memory (in public speaking). For long term cookic, coolkie and temporary cookie Path Problems exist on the hard disk.

3. servlet debugging methods

A: print the statement, use TOMCAT logs, return the error page, use IDE integration, restart the server, view the HTML source code, and process the request and response data respectively.

4. The difference between a Cookie and a session is that a session is a short conversation, and a Cookie can be set to a valid period of any time.

5. Differences between Get requests and Post requests

A: a. Post uses the Post mechanism in Http to submit the data in the form to the Get method of the Action program. It uses a Url request to submit the form data.

B. Get is suitable for transmitting data of less than 1 kb, which is highly efficient. The amount of Post data transmitted is large, but it is also limited.

6. servlet Lifecycle

A: The Servlet lifecycle begins when it is loaded into the memory of the Web server and ends when it is terminated or reloaded into the Servlet. Including loading and instantiation, initialization, request processing, and service termination. The lifetime is expressed by the init, service, and destroy methods of the javax. servlet. Servlet interface.

Describes the servlet lifecycle. who controls the servlet lifecycle? In the J2EE specification, how does the servlet lifecycle define the server to instantiate a servlet object and the container runs its init method, when a request arrives, its service method is run. The service method calls the (doGet, doPost) method based on the request type. When the server decides to destroy the instance, its destory method is called.

7. What is the difference between Servlet and Jsp?

A: Servlet is a file that is directly executed. Jsp is executed only after the Servlet is compiled.

8. JSP Architecture Model

Model1: It is difficult to maintain a pure jsp and has poor code reusability.

Model2: jsp + javabean uses the usebean tag in jsp. The code is essentially the same as Model1 in jsp.

Mvc: jsp + servlet + javabean

9. JSP implicit Variables

Request client request, which contains parameters from the GET/POST request

Response returned from the response webpage to the client

The pageContext page is managed here.

Session-related session Period

Content being executed by application servlet

Configuration servlet framework

Out is used to send response output (used to output data to the client)

Page JSP page itself

Exception is an exception that is not captured (exception)

10. Four sharing scopes

A: a. The current page of the page.

B. session within the same website.

C. request from the previous page to the next page.

D. Use the same website.

 

 

11. MVC-how to understand MVC

A: MVC is short for Model-View-Controller.

"Model" indicates the business logic of the application (implemented through the JavaBean and EJB components ),

"View" indicates the application surface (generated by the JSP page ),

"Controller" is to provide application processing process control (generally a Servlet). Through this design model, the application logic, processing process and display logic are divided into different components for implementation. These components can be used for interaction and reuse.

11. JSP Lifecycle

 

12. What is the difference between dynamic INCLUDE and static INCLUDE in JSP?
Dynamic INCLUDE is implemented using the jsp: include action. It always checks changes in the contained files. It is suitable for inclusion of dynamic pages and can contain parameters.
Static INCLUDE is implemented by using the include pseudo code and will not check the changes in the contained files. It is applicable to INCLUDE static pages.

13. Differences between Forword and SendRedirect

A: the former is only the redirection of control in the container, and the redirection address is not displayed in the address bar of the client browser;

The latter is a complete jump, the browser will get the jump address, and re-send the request link. In this way, the link address after the jump is displayed in the address bar of the browser.

Therefore, the former is more efficient. When the former can meet the needs, try to use the forward () method, and this will also help to hide the actual link. However, in some cases, if you want to jump to a resource on another server, you must use the sendRedirect () method.

Forward is a server request resource. The server directly accesses the URL of the target address, reads the response content of that URL, and then sends the content to the browser, the browser does not know where the content sent by the server comes from, so its address bar is still the original address.

Redirect means that the server sends a status code based on logic to tell the browser to request the address again. Generally, the browser will re-request the address with all the parameters just requested, so the session and request parameters can be obtained.

14. In what mode does BeanFactoy or ApplicationContext get the instance? How to map multiple instances in the configuration file

A: The instance is obtained in single-State mode. It can be changed in the preparation file, as if it was isthread... Set to false

15. What actions does jsp have? What are their roles?

A: JSP has the following 6 basic actions:

Jsp: include: Introduce a file when the page is requested.

Jsp: useBean: Find or instantiate a JavaBean.

Jsp: setProperty: Set attributes of JavaBean.

Jsp: getProperty: outputs the attributes of a JavaBean.

Jsp: forward: Transfers requests to a new page.

Jsp: plugin: generate the OBJECT or EMBED tag for the Java Plug-in based on the browser type

18. How do I set/retrieve the attribute values in Bean?

A: Set the attribute value.

Obtain all attributes submitted from the previous form that have the same name as the Bean variable.

Get property value:

Equivalent:

19. Use of JavaBean: or

20. Two methods to achieve page Jump:

A:,

B,

21. include command labels:

Simple code replication.

Include action Tag: Get the execution result of inc2.jsp.

22. value transfer between pages:

A: a. Set the Session variable session. setAttribute ("name", "sist ");

Get the Session variable: session. getAttribute ("name ");

B. Address Transmission: show. jsp? Id = variable value

23. There are four built-in object scopes:

A: a. The current page of the page.

B. session within the same website.

C. request from the previous page to the next page.

D. Use the same website.

24. Obtain the session ID:; to determine whether the session is valid: session. isNew ();

25. Use Cookie objects

A: Cookie ck = new Cookie ("name", "sist ");

Write: response. addCookie (ck );

Read: Cookie [] ck = request. getCookies ();

If (ck = null)

For (int a = 0; a <ck. length; a ++)

{

If ("name". equals (ck [a]. getName ()))

Out. print (ck [a]. getValue ());

}

26. What is servlet? A server-side JAVA program running in a web container is mainly used to respond to HTTP requests. Servlet is generally used in the Controller part of MVC.

27. What is a servlet container: applications used to manage the servlet lifecycle, such as tomcat ).

28. What is JSP Page: java server page is an extension of servlet, emphasizing web Page expression, and compiling is a class servlet.

29. JSP labels

Action Tag: jsp: include jsp: forword jsp: usebean jsp: set/get Property

Command label:

30. In servlet

A. Obtain parameters from HTML Forms

Request. getParameterNames (); enumer;

Request. getParameter (); String

B. How to obtain the request header information

Enumeration enumer = request. getHeaderNames ();

While (enumer. HasMoreElements ())

{

String header = enumer. next ();

String result = request. getHeader (header );

Out. print (result );

}

C. How to obtain customer cookie Information

Request. getCookies (); array []

D. How to set the response header information

Response. setHeader ();

E. How to set the response content type

Response. setContentType ("text/html charset = UTF-8 ");

F. How to obtain the I/O Stream, text stream, and binary stream

G. How to redirect from the current servlet to another URL

Response. sendRedirect ("url ")

RequstDispatcher rd = request. get RequstDispatcher ("url ")

Rd. forword (request, response );

H. How to Write cookies to clients?

Cookie cookie = new Cookie ("object", "jklj ");

Cookie. setMaxAge (time );

Response. addCookie (cookie );

31. describes the servlet lifecycle. who controls the servlet lifecycle? How is the servlet lifecycle defined in the J2EE specification?

The server instantiates a servlet object, the container runs its init method, and runs its service method when the request arrives. The service method calls the (doGet, doPost) method according to the request type, the destory method is called when the server decides to destroy the instance.

 

32. How to Create a Request Dispatcher object, forward requests to other web resources (including other web resources), and describe how to use the request to store the status, and what is the difference with other save status methods?

RequstDispatcher rd = request. get RequstDispatcher ("url ")

Rd. forword (request, response );

Request. setAttribute ("object", "content ");

The lifecycle of a response request is over.

33. how to configure servlet definitions in web. xml

 

Name

Package

 

Ing

 

Name

/Patten

 

34. Obtain the ServletConfig object

ServletConfig SC = getServletConfig ();

35. How to obtain the servletContext object

ServletContext SC = getservletContext ();

36,

POST

 

Which method of doPost, doGet, doForm, and doHref is used to call the servlet?

DoGet Method

37. Description of the directory in which the following web resources should be stored, static pages JSP, servlet class, and web. xml, Tag libraries, JAR File, java class, and resource files to be protected

Tomcat container web. xml, Tag libraries: \ webapps \ Project name \ WEB-INF

Servlet, javaclass: \ webapps \ Project name \ WEB-INF \ classes \

Jar: \ webapps \ Project name \ WEB-INF \ lib \

38. Describe the difference between the request session web application. Create a listener class to listen to the lifecycle of each scope.

When different requests in the life cycle are generated, the life cycle ends. The session sets the Life Cycle Time in web. xml, and manually clears the web container to restart the life cycle. Webapplication container restart and manual cleanup

39. built-in JSP objects and methods.

Request indicates the HttpServletRequest object. It contains information about browser requests and provides several useful methods for obtaining cookie, header, and session data.
Response indicates the HttpServletResponse object, and provides several methods (such as cookies and header information) for setting the response to the browser)
The out object is an instance of javax. jsp. JspWriter. It provides several methods for sending output results to the browser.
PageContext indicates a javax. servlet. jsp. PageContext object. It is used to facilitate access to various namespaces and servlet-Related Object APIs, and encapsulates common servlet-related functions.
Session indicates the javax. servlet. http. HttpSession object of a request. Session can store user status information
Applicaton indicates a javax. servle. ServletContext object. This helps you find information about the servlet engine and servlet environment.
Config indicates a javax. servlet. ServletConfig object. This object is used to access the initialization parameters of the servlet instance.
Page indicates a servlet instance generated from the page.

40. Common JSP commands

IsErrorPage (whether the Exception object can be used), isELIgnored (whether to ignore the expression)

% @ Taglib prefix = "c" uri = "http ://......" %>

41. What are the similarities and differences between JSP and Servlet, and what are their relationships?
JSP is an extension of Servlet technology. It is essentially a simple Servlet method, with more emphasis on the external expression of the application. After JSP compilation, it is "servlet-like ". The main difference between Servlet and JSP is that the application logic of Servlet is in the Java file and is completely separated from the HTML in the presentation layer. In JSP, Java and HTML can be combined into a file with the extension. jsp. JSP focuses on views. Servlet is mainly used for control logic.

42. Four session tracking technologies
Description of the session scope ServletsJSP page
Page No indicates the objects and attributes related to a page. A page is represented by a compiled Java servlet class (which can contain any include command but has no include action. This includes both servlet and JSP pages compiled into servlet.
A request is an object and attribute related to a request sent by a Web Client. A request may span multiple pages and involves multiple Web Components (due to the relationship between the forward instruction and the include action)
A session is an object and attribute associated with a user experience for a Web Client. A Web session can also frequently span requests from multiple clients.
Application represents the objects and attributes related to the entire Web application. This is essentially a global scope that spans the entire Web application, including multiple pages, requests, and sessions

43. Complete the following table based on the JSP Lifecycle

The page is compiled for the first time

Request #1

Request #2

Server restart

Request #3

Request #4

Page modified

Request #5

Request #6

Convert a JSP page to a servlet

Servlet Compilation

Create a servlet instance and load the server's memory

Init (or similar function) is called

DoGet (or similar function) is called

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.