Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common interview questions of Servlet in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what are the common Servlet interview questions in Java, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

1: what are Bhand S and Chand S

Browser/Server browser / server (thin client)

Custom/Server client / server (fat client)

2: describe the structure of war package, jar package and ear package

War-

-- myweb

-WEB-INF

-web.xml

-lib

-classes

Jar-

-- myjar

-META-INF

-MF

Ear-

-- META-INF

-Application.xml

--. War

--. Jar

3: what is servlet? What does servlet mainly do?

How to request and respond to network services

The method of calling servlet through a WEB browser is mainly to write dynamic code on the server side to communicate with the server side.

What are the advantages of 4:servlet over cgi? Shortcomings of servlet

Advantages:

Performance (threads are faster than processes)

Scalable

Java is robust and object-oriented

Java independent platform

Disadvantages:

Processing code (business logic) and HTML (presentation logic) are mixed together

5: what is the name of the commonly used servlet package?

Javax.servlet

Javax.servlet.http

6: describe the hierarchical structure of the servlet interface?

Servlet

-- genericServlet

-- HttpServlet

-- your own servlet

ServletRequest

ServletResponse

7: compare the get method with the post method?

Get method: the request has no negative impact on the server, the amount of Form data is small, and the interior of the data should be visible in url; plaintext transmission, low security.

Post method: the requested data process changes the state of the server. The amount of Form data is large, the interior of the data should not be visible in url, and the security is high.

8: categorize and describe what functions the HttpServletRequest interfaces perform

1. Read and write HTTP headers

two。 Get and set cookies

3. Get path information

4. Identifies the HTTP session.

9: categorize and describe what functions the HttpServletResponse interfaces perform

HttpServletResponse adds a method for representing status codes, status information and response headers, and it is also responsible for decoding the HTTP session ID written into a Web page in the URL.

10: describe the basic functions of the Service method? Where is the default implemented?

The service method is the service period in the servlet life cycle. According to the HTTP request method (GET, POST, etc.), the request is distributed to doGet, doPost, and so on.

HttpServlet class implementation

11: how to develop your own Servlet? Describe the steps to be done and the work to be done at each step

1. Introduction of jar packet

1) build development environment common package-> lib package-> servlet--- > api.jar

two。 Develop servlet classes

1) inherit HttpServlet first

2) implement doGet () doPost ()

3) define doGet () doPost ()

3. Build a web application

4. Deployment

Install web containers, such as Tomcat

Create a new folder under the webapps directory of Tomcat as the root of the web program

Create a new folder called WEB-INF under the root, and create a web.xml file, a classes folder, and a lib folder to configure the web.xml file according to servlet's DTD.

Copy the compiled class file of servlet to the jar package needed by the lib file storage program in the classes directory.

12: why does servlet need a deployment description?

Servlet needs to configure the web.xml file to make the container aware of the servlet program

The basic description of 13:Servlet should be? Please write it down.

Hello sl314.web.FormBasedHelloHello/greeting

14: how to use servlet in html

FORM marker

ACTION- specifies the destination of the form information (the associated URL)

METHOD-specifies the HTTP method (GET or POST)

Syntax:

{HTML form tags and other HTML content}

15: how to accept parameters in request

String userName = (String) request.getParameter ("userName") method

16: how to accept the value of header in request

Request.getHeader (name)

Request.getHeaders (names); etc.

17: how to output html

PrintWriter pw = response.getWriter ()

Pw.write ("")

Pw.write ("Hello")

Pw.write ("")

18: how to set the output contentType

Response.getContentType ()

19: describe the life cycle of servlet?

The life cycle refers to the initialization period of the servlet instance in the web container from the first creation to the call to the init method, after the run time of the service method, until the end of the destory method destruction period.

The lifecycle of servlet instances is managed by the web container

20: describe the functions and features of the init,service,destroy method

Init method: a method called when an servlet instance is created to create or open any servlet-related resources and initialize the state of the servlet. The Servlet specification ensures that no requests are processed before the init method is called.

Service method: it is the method that servlet actually handles the request sent by the client. It is called by the web container and distributes the request to doGet, doPost and other methods according to the HTTP request method (GET, POST, etc.).

Destory method: it is called by the web container when the servlet instance is destroyed. The Servlet specification ensures that the processing of all requests is completed before the destroy method call, where the destroy method needs to be overridden: release the state of any servlet-related resource store servlet opened in the init method

21: what is callback method? What are the characteristics?

The method of the program is called by the container

It is up to the container to decide when to adjust.

22: how do I set the parameters for initializing servlet?

GreetingText Hello

23: how to get the parameters of servlet initialization

Public void init () {greetingText = getInitParameter ("greetingText"); System.out.println ("> > greetingText ='" + greetingText + "'");}

The 24:ServletConfig interface is implemented there by default

The GenericServlet class implements the ServletConfig interface

25: what is ServletContext? What's the use?

Servlet context

ServletContext object is the runtime representation of Web application, which can be used to realize resource sharing in Web application.

26: how do I access the ServletContext interface? Where did it come true?

Servlet is accessed through the getServletContext () method

GenericServlet class implementation

The functions of 27:ServletContext interface include? Use code examples separately

Read-only initialization parameter: getInitParameter (name:String): String

GetInitParameterNames (): Enumeration

Read-write access application-level attribute: getAttribute (name:String): Object

SetAttribute (name:String, value:Object)

GetAttributeNames (): Enumeration

Read-only access to file resources: getResource (path): URL

GetResourceAsStream (path): InputStream

Write web application log file: log (message:String)

Log (message:String, Throwable:excp)

28: how to set the parameters of Context?

CatalogFileName / WEB-INF/catalog.txt

29: how do I get the parameter values set by Context?

ServletContext context = sce.getServletContext (); String catalogFileName = context.getInitParameter ("catalogFileName")

30: describe the life cycle of Web applications?

Initialize each Web application when the Web container starts

You can create a listener object to trigger these events

Destroy each Web application when the Web container is closed

31: how to use code to monitor the lifecycle of Web applications?

Public class Test implements ServletContextListener {public void contextInitialized (ServletContextEvent sce) {/ /}} com.csy.Test

What does the following error code mean in 32:web application: 400401404500

400 Bad Request

401 Unauthorized

404 Not Found

500 Internal Server Error

33: describe two methods of declarative error handling in Web applications

Use the error-page element to declare a processor for a given HTTP status code

four hundred and four

/ error/404.html

Any number of error pages can be declared, but a given status code can only correspond to one page.

Use the exception-type element to declare the handler for a given Java exception

Java.lang.ArithmeticException

/ error/ExceptionPage

Any number of error pages can be declared, but a given exception type corresponds to only one page.

You cannot use a parent class to catch multiple exceptions

34: describe the methods for logging exceptions. Which interfaces are located?

GenericServlet:

Log (message:String)

Log (message:String, Throwable:excp)

ServletContext:

Log (message:String)

Log (message:String, excp:Throwable)

35: what is conversation?

The Web container saves a "session object" for each user, which is used to store session information for a specific user.

36: how do I get a conversation?

HttpSession session = request.getSesseion ()

37: what are the basic functions of conversational Api?

GetID (): String

IsNew (): boolean

GetAttribute (name): Object

SetAttribute (name,value)

RemoveAttribute (name)

38: how do I destroy a session?

1. You can use deployment descriptors to control the lifecycle of all sessions

ten

2. You can use the HttpSession interface that controls the lifecycle of specific session objects.

Invalidate ()

GetCreationTime (): long

GetLastAccessedTime (): long

GetMaxInactiveInterval (): int

SetMaxInactiveInterval (int)

39: describe the basic principles of session persistence state

When the client requests for the first time, the server creates a session binding to the request, returns the sessionid with the response object response and stores it in the client's cookies. The next time the request is sent, the session of the server is retrieved directly according to the sessionid (all cookies is brought to the server for each request)

40: how to read and write cookie, code example

Write: String name = request.getParameter ("firstName"); Cookie c = new Cookie ("yourname", name); response.addCookie (c); read: Cookie [] allCookies = request.getCookies (); for (int iCookies [I]; I < allCookies.length; iTunes +) {if (allCookies [I] .getName (). Equals ("yourname")) {name = allCookies [I] .getValue ();}

41: what is URL rewriting, how to implement it, code examples

When Cookie cannot be used, you can use URL to override request.encodeURL ()

The customer appends additional data to each URL

The server associates this identifier with the relevant session data it stores

Http://host/path/file;jsessionid=123

41: describe four authentication technologies for web applications

The BASIC-Web browser receives the username and password and sends them to the Web server in clear code

The DIGEST-Web browser receives the username and password and sends this data to the Web server using an encryption algorithm

HTML form sent to Web browsers by FORM-Web applications

CLIENT-CERT-Web containers use SSL to verify link protection for users, servers, and clients

42: what is authorization and what is authentication?

Authorization is the process of dividing web resources by user roles in branch offices, which identifies security domains in web applications and assigns permissions

The web container uses a vendor-specified mechanism to verify the user's role matching permissions

43: what is HTTPS

HTTPS (Secure Hypertext Transfer Protocol) is a HTTP that uses the SSL protocol

44: what is audit?

That is, access tracking, which is the process of keeping records for each access to a web application

45: how to achieve declarative authorization

1. Identify the web resource set

2. Identify the role

3. Map the web resource set to the role

4. Identify the users in each role

Match in web.xml

46: describe the servlet concurrency problem?

Multiple threads of the same kind are running and can share the same Servlet instance. The shared data and resources are not reasonably synchronized, which may cause data conflicts.

47: describe six attribute ranges in Web applications

Local variable (page range)

Instance variable

Class variable

Request attribute (request scope)

Session attributes (session scope)

Context attribute (scope of application)

48: spend the above six, which are thread-safe

Local variables and request properties

49: what is STM? How to achieve it?

SingleThreadModel interface

You can implement the SingleThreadModel interface to ensure that there is only one request to execute the service method at a time

50: how to implement concurrency management?

Use local and request properties whenever possible

Using synchronized syntax to control concurrency

Minimize the use of synchronization blocks and synchronization methods

Use resource classes that are correctly set to thread safety

The above is all the contents of the article "what are the common Servlet interview questions in Java?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report