JSP & servlet knowledge Q &

Source: Internet
Author: User

1ThreeStatmentDifferences and usage

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

2 cookie

A: The temporary cookic exists in the memory (in public speaking). The long term cookic exists in the coolkie and temporary Cookie Path problems when the hard disk is used.

3 ServletDebugging methods

A: Use the print statement and tomcat log to return the error page. Use ide integration to restart the server and view the htmlSource codeTo process the request and response data respectively.

4.CookieAndSessionThe difference is:Session is short dialog, and cookie can set the validity period at any time.

5. GetRequest andPostRequest difference

A: A. Post is based on the post mechanism in HTTP to submit data in the form to action.ProgramThe get method submits form data through URL requests.

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

6. servletLifecycle

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. IncludingLoadAndInstantiation,Initialization,Process requestsAndEnd of service. This survival period is causedJavax. servlet. servletInterfaceInit, ServiceAndDestroyMethod Expression.

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,ServletAndJSP?

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

8. jspArchitecture Model

Model1: difficult to maintain JSP files,CodePoor 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. jspImplicit variable

Request Client request, which contains parameters from the get/POST request

ResponseResponse from the webpage to the client

PagecontextThe webpage attribute is managed here

SessionRequest-related Session Period

ApplicationContent being executed by Servlet

ConfigServlet architecture Components

OutSend Response output (used to output data to the client)

PageJSP page itself

ExceptionUncaptured exceptions (Exceptions) for error webpages)

10.Four sharing scopes

A:A,Page.

B,Session within the same website.

C,Request goes from the previous page to the next page.

D,Application in the same website.

 

 

11. MVC --How to understandMVC

A:MVCYesModel-View-Controller.

"Model"Which indicatesApplication business logic (PassJavaBean,EJBComponent implementation ),

"View"YesApplication Representation(JSPPage generation ),

"Controller"YesProcess Control of applications(GenerallyServlet)Using 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. jspLifecycle

12,JSPMedium dynamicIncludeAnd staticInclude?
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. forwordAndSendredirectDifferences

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 moreEfficientWhen the former can meet the needs, try to use the forward () method, and this will also helpHide 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.SlaveBeanfactoyOrApplicationcontextIn what mode is the Instance obtained? 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. jspActions?What are their roles??

A: JSP has the following6Basic 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: Transfer the request to a new page.

JSP: plugin: Generate an object or embed tag for the Java Plug-in based on the browser type

16.Brief IntroductionSessionWhat are the features?

Answer:

(1) Not thread-safe

(2) The session instance is lightweight.

(3) (3) in the session, each database operation is performed in a transaction.

17.DescriptionSessionThe role of Cache

Answer:

(1) reduce the frequency of accessing the database.

(2) ensure that the objects in the cache are synchronized with relevant records in the database.

18, How to set/ObtainBeanAttribute Value in?

A: Set the attribute value.<JSP: setpropertyname = "Haha", property = "Haha attributes" value = "variable value"/>

<JSP: setproperty name = "Haha", property = "*"/> obtain all attributes submitted from the previous form with the same variable name as bean.

Get property value:<JSP: getproperty name = "Haha" property = "bean attributes"/>

Equivalent to: <% = geta () %>

19,JavaBeanUsage:<JSP: usebean id = "name" class = "sist. MD5 "Scope =" page "/> or <JSP: usebean id =" name "class =" sist. MD5 "Scope =" page "> </jsp: usebean>

20Two methods to achieve page Jump:

A:A,<JSP: Forward page = "for2.jsp"/>

B,<% Response. sendredirect ("for2.jsp"); %>

21,IncludeCommand label:

<% @ Include file = "inc1.jsp" %> simple code replication.

Include action Tag: <JSP: Include page = "inc2.jsp"> obtain 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

23Built-in object scopes are divided4Type:

A:A,Page.

B,Session within the same website.

C,Request goes from the previous page to the next page.

D,Application in the same website.

24, GetSession ID:<% = Session. GETID () %>; judge whether the session is valid: Session. isnew ();

25, UseCookieObject

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 ());

}

26What isServlet:InWebServers running in containersJavaProgram, mainly used for responseHTTPRequest.ServletGenerally usedMVCIn the controller section.

27What isServletContainer:Used for managementServletFor example (Tomcat).

28What isJSP page:Java Server Page is an extension of servlet, emphasizing web page expression. After compilation, It is a class servlet.

29,JSPTag

Action Tag: JSP: Include JSP: forword JSP: usebean JSP: Set/get property

Command label: <% @ page ...... %> <% @ Include ..... %> <% @ Taglib ..... %>

30,ServletHow

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, SuchHow 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 = new cookie ("object", "jklj ");

Cookie. setmaxage (time );

Response. addcookie (cookie );

31, DescriptionServletWho will control the lifecycle?ServletLifecycle,ServletLifecycle inJ2EEHow is the specification defined?

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 createRequest dispatcherObject,How to forward requests to otherWebResources (including otherWebResources), describes how to useRequestStorage status, and what is the difference with other storage 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, HowWeb. xmlConfiguringServletDefinition

<Servlet>

<Servlet-Name> name </servlet-Name>

<Servlet-class> package </servlet-class>

</Servlet>

Ing

<Servlet-mapping>

<Servlet-Name> name </servlet-Name>

<URL-Patten>/Patten </url-Patten>

</Servlet-mapping>

34, ObtainServletconfigObject

Servletconfig SC = getservletconfig ();

35, How to obtainServletcontextObject

Servletcontext SC = getservletcontext ();

36,<HTML> <body>

<A href = "/servlet/helloservlet"> post </a>

</Body>

Which method of dopost, doget, doform, and dohref is used to call the servlet?

Doget Method

37, Describes the followingWebResources should be placed inWebUnder what directory in the container, static pageJSP,Servlet class,Web. xml,Tag Libraries,JAR File,Java classAnd 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, DescriptionRequest session Web ApplicationCreate a listener class to listen to eachScopeLifecycle

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,JSPBuilt-in 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,JSPCommon commands
<% @ Page Language = "Java" contentype = "text/html; charset = gb2312 "session =" true "buffer =" 64kb "autoflush =" true "isthreadsafe =" true "info =" text "errorpage =" error. JSP "iserrorpage =" true "iselignored =" true "pageencoding =" gb2312 "Import =" Java. SQL. * "%>
Iserrorpage (whether the exception object can be used), iselignored (whether to ignore the expression)
<% @ Include file = "FILENAME" %>
<% @ Taglib prefix = "C" uri = "http: // ......" %>

41,JSPAndServletWhat are the similarities and differences between them and 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.

42Four session tracking technologies
Description of the session scope servletsjsp page
PageNo 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.
RequestIs the object and attribute related to a request sent by the 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)
SessionIs an object and attribute related to a user experience used for a Web Client. A web session can also frequently span requests from multiple clients.
ApplicationIs 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, AccordingJSPCompleted the following table

 

 

 

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.