1. The entry point for the servlet program is: ()
A Init ()
B Main ()
C Service ()
D doget ()
Reference Answer: A
2. In Severlet, what httpseverletresponse means to redirect an HTTP request to another URL. ( )
A Sendurl ()
B RedirectURL ()
C Sendredirect ()
D Redirectresponse ()
Reference Answer: C
3. In Java EE, when the HTTP request from the client is delegated to the servlet, what method of HttpServlet is invoked. ( )
A Service ()
B doget ()
C DoPost ()
D Init ()
Reference Answer: A
4. Given a code fragment for a servlet program, as follows:
public void DoPost (httpservletrequest request,httpservletresponse response)
Throws servletexception,ioexception{
Request.getsession (). getattribute ("A");
Assuming that the second row returns an object reference that is not NULL, the object is stored in a range. ( )
A page
Session B
C Request
D Application
Reference Answer: B
5. Given a piece of the program below, the user in the browser address bar type the correct request URL and enter the table, the results displayed on the console: ()
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
Reference Answer: A
6. In Java EE, which of the following statements can get the input of a text box in a page request (the name of the text box is title). ( )
A Request.getparameter ("title")
B Request.getattribute ("title")
C request.getparametervalues ("title")
D request.getparameters ("title")
Reference Answer: A
7. In the life-cycle method of the Severlet filter, what method the Web container invokes whenever a request or response is passed. ()
A Init
B Service
C Dofilter
D Destroy
Reference Answer: C
8. Methods that cannot share data among different users are: ()
A through Cookies
B using the file system
C using the database
D through the ServletContext object
Reference Answer: A
9. Suppose there is a HelloServlet class in the HelloApp application, which is configured in the Web.xml file as follows:
<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>
So what is the URL that accesses the helloservlet at the browser end. ( )
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
Reference Answer: E
1. Please describe the lifecycle of a servlet component.
Reference answer: The servlet has a good life-cycle definition, including loading and instantiating, initializing, processing requests, and service completion. 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. Call its Destroy method when the server decides to destroy the instance.
2. Please answer: What is the difference between the init () method of the servlet and the Service () method.
Reference answer: Invoke the Init () method on initialization to invoke the service () method when the request arrives, and the service () invokes Doget () or depost (), depending on the type of request
3. Please answer: When the servlet executes, which methods are generally implemented.
Reference answer: The servlet generally needs to implement several of the following methods when executing:
public void init (servletconfig config)
Public ServletConfig Getservletconfig ()
Public String Getservletinfo ()
public void Service (ServletRequest request,servletresponse response)
public void Destroy ()
4. Please answer: What is the difference between the forward () and redirect () Two methods in the Servlet API.
Reference answer: The former is only the control of the container in the steering, in the client browser address bar will not show the direction of the address, the latter is a complete jump, the browser will get the address of the jump, and resend the request link. In this way, you can see the link address after the jump from the address bar of the browser. So, the former is more efficient, when the former can meet the need, try to use the forward () method, and 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.
5. What is the basic architecture of the servlet, please write a simple example.
Reference Answer:
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. Jane Answer: Under what circumstances call the Doget () method, and under what circumstances the Dopost () method is invoked.
Reference answer: The method attribute in the form label in the JSP page is called doget () when the method property is not in Get or by default, and the method property in the form label in the JSP page calls Dopost () at post.
7. We often encounter the output of some encoded characters, such as Iso8859-1, in the process of Web application development, write a small example that can output strings that match some kind of encoding.
Reference Answer:
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. What is the role of the configuration file in a servlet application web.xml.
Reference answer: The configuration file in a servlet application web.xml the information used to configure Web applications, such as listener, filter, and servlet configuration information.
9. Please describe: The difference between the HttpRequest getparameter () method and the GetAttribute () method.
Reference Answer: The difference between the GetParameter () method of HttpRequest and the GetAttribute () method is mainly listed as follows: (1) All getparameter get is string type. Either 123 of the http://a.jsp?id=123, or a form that submits past data. GetAttribute can be an object. (2) GetParameter () is the parameter value that gets the Post/get pass; GetAttribute () is the value of the data in the object container, (3) GetParameter: When used for client redirection, that is, click on the link or submit the button newsletters value, This is used to receive data when a value is passed with a form or URL redirection. GetAttribute: For server side orientation, the forward function is used in Sevlet, or mapping.findforward is used in struts. GetAttribute can only receive the value that the program uses setattribute to pass over. (4) GetParameter () is the parameter value that obtains the Post/get pass; GetAttribute () is the value of the session; In addition, you can use SetAttribute, GetAttribute sends the receiving object. and GetParameter obviously can only pass strings. (5) Getparamete returns the string, used to read the value in the submitted form;getattribute returns object, needs to be converted, can be set to any object with setattribute, and is flexible, Can be used at any time; from a deeper level, the data passed by the Request.getparameter () method is uploaded from the Web client to the Web server, representing the HTTP request data. The Request.getparameter () method returns the data of type string. (6)