Interview Javaweb part

Source: Internet
Author: User

1. Tomcat's optimization experience

A: Remove the watch for Web. XML and edit the JSP into a servlet in advance.

With surplus physical memory, increase the memory of the JVM used by Tomcat

2. The difference between the get and post methods of HTTP requests

A: The servlet has a good definition of lifetime, including loading and instantiation, initialization, processing requests, and end of service. This lifetime is expressed by the init,service and destroy methods of the Javax.servlet.Servlet interface.

3, explain what is a servlet;

A: The servlet has a good definition of lifetime, including loading and instantiation, initialization, processing requests, and end of service. This lifetime is expressed by the init,service and destroy methods of the Javax.servlet.Servlet interface.

4. Say a servlet's life cycle?

A: The servlet has a good definition of lifetime, including loading and instantiation, initialization, processing requests, and end of service. This lifetime is expressed by the init,service and destroy methods of the Javax.servlet.Servlet interface.

When the servlet is instantiated by the server, the container runs its Init method, the service method is run when the request arrives, and the service method automatically dispatches the Doxxx method (Doget,dopost) corresponding to the request, etc. The Destroy method is called when the server decides to destroy the instance.

The Web container loads the servlet, beginning with the life cycle. The servlet is initialized by calling the servlet's init () method. By invoking the service () method implementation, different do*** () methods are called depending on the request. To end the service, the Web container invokes the servlet's Destroy () method.

5, the basic framework of the 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 { /c6>

}

}

6. What is the difference between forward () and redirect () in the SERVLET API?

A: The former is only the control in the container of the steering, in the client browser address bar will not show the post-turn address, the latter is a full jump, the browser will get the address of the jump, and resend the request link. This way, you can see the link address after the jump from the address bar of the browser. Therefore, the former is more efficient, when the former can satisfy the need, try to use the forward () method, and, as such, it also helps to hide the actual link. In some cases, for example, you need to jump to a resource on a different server, you must use the Sendredirect () method.

7, under what circumstances call Doget () and Dopost ()?

The method property in the form tag in the JSP page is called doget () when it is get, and Dopost () is called when it is post.

8. The main method of the Request object:

SetAttribute (String name,object): Sets the parameter value of the request named name

getattribute (String name): Returns the property value specified by name

Getattributenames (): Returns a collection of names of all properties of the request object, resulting in an instance of an enumeration

getcookies (): Returns all cookie objects for the client, as a result of a cookie array

getcharacterencoding (): Returns the character encoding in the request

Getcontentlength (): Returns the length of the requested body

GetHeader (String name): Gets the file header information defined by the HTTP protocol

getheaders (String name): Returns all values of the request header for the specified name, resulting in an instance of an enumeration

getheadernames (): Returns the name of the request header, and the result is an instance of the enumeration

getInputStream (): Returns the requested input stream for obtaining the data in the request

GetMethod (): A way to get clients to send data to the server side

getparameter (String name): Gets the parameter value specified by the client that is passed to the server side by name

getparameternames (): Gets the name of all parameters that the client sends to the server side, and the result is an instance of the enumeration

getparametervalues (String name): Gets all values that have the parameter specified by name

Getprotocol (): Gets the name of the protocol on which the client transmits data to the server side

getquerystring (): Get query string

Getrequesturi (): Gets the client address that issued the request string

getremoteaddr (): Gets the IP address of the client

Getremotehost (): Gets the name of the client

getsession ([Boolean Create]): Returns and requests the relevant session

getServerName (): Gets the name of the server

Getservletpath (): Gets the path of the script file requested by the client

Getserverport (): Gets the port number of the server

removeattribute (String name): Delete One of the properties in the request

9. The difference between forward and redirect

  forward is the server requests the resource, the server directly accesses the URL of the destination address, reads the response content of that URL, and then sends the content to the browser, the browser does not know where the server sends the content from, so its address bar is the original address.

redirect is the server based on logic, send a status code, tell the browser to request the address again, in general, the browser will use all the parameters just requested again, so the session,request parameters can be obtained.

10. What is the difference between request.getattribute () and Request.getparameter ()?

  Difference One:

  Request.getparameter () is a parameter that is passed from the browser to the server
  Request.getattribute () is a value that the server code temporarily retains in the request, and these values are setattribute after passing through the code.

  Difference Two:  

REquest.getparameter is used to accept parameters from the Get method or the Post method and can only accept java.lang.String  

  Request.getattribute is used to accept a variable or action from a servlet, in action, Request.setattribute ("ret", ret); can only accept Java.lang.Object

  Difference Three:  

One is a variable type, 1 object Types "

What are the built-in objects of JSP? What are the roles? What are the different methods?

A: JSP has the following 9 built-in objects:

   Request client requests, this request will contain parameters from the Get/post request

  Response Web page back to the client's response

  The properties of the PageContext Web page are managed here

   session period associated with the request

  Application What the servlet is doing

   output used to transmit a response

  Config Framework parts for Servlets

  page JSP Web page itself

  Exception exceptions for error pages, not captured

The request represents the HttpServletRequest object. It contains information about the browser request and provides several useful ways to get the cookie, header, and session data.

Response represents the HttpServletResponse object and provides several methods for setting the response to the browser (such as cookies, header information, etc.)

The Out object is an instance of Javax.jsp.JspWriter and provides several methods that you can use to echo the output to the browser.

PageContext represents a Javax.servlet.jsp.PageContext object. It is an API for easy access to a wide range of namespaces, servlet-related objects, and a way to wrap generic servlet-related functionality.

The session represents a requested Javax.servlet.http.HttpSession object. Session can store the user's status information

Applicaton represents a Javax.servle.ServletContext object. This helps to find information about the servlet engine and the servlet environment

Config represents a Javax.servlet.ServletConfig object. This object is used to access the initialization parameters of the servlet instance.

Page represents a servlet instance that is generated from this page

What are the actions of JSP? What are the roles ?

(This problem does not seem to be important, I do not understand why there is this question)

A: JSP has the following 6 basic actions

Jsp:include: A file is introduced when the page is requested.

Jsp:usebean: Find or instantiate a JavaBean.

Jsp:setproperty: Sets the properties of the JavaBean.

Jsp:getproperty: Outputs the properties of a JavaBean.

Jsp:forward: Move the request to a new page.

Jsp:plugin: Generates an object or embed tag for a Java plug-in based on browser type

13. Common directives of JSP

(1) page: Used to define global properties in a JSP file

(2) include: Used to include the content of another file in a JSP page

(3) taglib: This command allows the user to customize the new label

What is the difference between a dynamic include and a static include in a JSP?

A: dynamic include uses Jsp:include action to implement <jsp:include page=included.jsp flush=true/> It always checks for changes in the contained files. Suitable for inclusion of dynamic pages, and can take parameters

  Static Include pseudo code implementation, will not check the changes contained in the file, applicable to include static page <%@ include file=included.htm%>

16. How to transfer objects between pages

Request,session,application,cookie, etc.

17. What are the similarities and differences between JSP and servlet, and what are their connections?

JSP is the extension of servlet technology, which is essentially a simple way for servlets to emphasize the appearance of application. The JSP is compiled with a "class servlet".

The main differences between Servlets and JSPs are:

The application logic of the Servlet is in the Java file and is completely detached from the HTML in the presentation layer. The case for JSP is that Java and HTML can be combined into a file with a. jsp extension. JSPs focus on Views, and Servlets are used primarily for control logic.

18. How do the various parts of MVC have the technology to implement ?

Answer: MVC is shorthand for Model-view-controller.

    • The Model represents the business logic of the application (implemented through the JAVABEAN,EJB component),
    • View is the presentation face of the app (generated by the JSP page),
    • The controller is the process control that provides the application (typically a servlet),

Through this design model, the application logic, processing and display logic are divided into different component implementations. These components can be interacted with and reused.

19, we often encountered in the Web application development process output some kind of encoded characters, such as iso8859-1, how to output a certain encoding of the string?

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;

}

Interview Javaweb part

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.