JSP enterprise interview questions (4)
5-1. Briefly describe the relationship between Servlet and JSP.
JSP is implemented in another way. servlet is an earlier version of JSP. In JSP, it focuses more on the page performance, while in servlet, it focuses more on the implementation of business logic.
Therefore, JSP is preferred when the page display effect is complex. Or htmlCodeJSP can be selected when Java code is relatively fixed. Servlet is the first choice for processing business logic.
At the same time, jsp can only process browser requests, while servlet can process a client applicationProgramRequest. Therefore, Servlet enhances the functions of web servers.
5-2. Briefly describe the servlet lifecycle. (Enterprise pen exam)
The servlet running mechanism is similar to that of the applet. The servlet runs on the server, but the applet runs on the client. Servlet is a subclass of the httpservlet class in the javax. servlet package. The server creates and initializes the subclass.
The servlet lifecycle consists of three processes.
1. INIT () method: Initialize the servlet on the server.
2. Service () method: After Initialization is complete, the servlet object calls the client's request of this method.
3. Destroy () method: Call this method to destroy servlet objects.
The init () method is called only once when the servlet is requested for loading for the first time. When a customer requests the servlet service again, the web server starts a new thread, in this thread, call the service method corresponding to the customer's request.
5-3. Briefly describe the functions and usage of the httpsession interface. (Enterprise pen exam)
This is an interface in the Java. servelt. http package, which encapsulates the concept of session. It is defined as "public interface httpsession". You can use the getsession () method of the httpservletrequest object to obtain this type of object. Syntax: "httpsession session = request. getsession ();".
5-5. Compile an HTML file and use the dopost method of servelt to read the form content. (Completed on the computer) (Enterprise pen exam)
Case name: Use the doget and dopost Methods
Program name: j5_02.htm
<HTML>
<Body>
<Form action = "/doget" method = "get">
Enter the name:
<Input type = "text" name = "myname"> <br>
Your interests:
<Select name = "love">
<Option value = "Sleep"> sleep </option>
<Option value = "dance"> dance </option>
<Option value = "Travel"> travel </option>
</SELECT> <br>
<Input type = "Submit" name = "mysubmit"> <br>
<Input type = "reset" value = ""> <br>
</Form>
</Body>
</Html>
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class j5_03 extends httpservlet
{
// Override the dopost Method
Public void dopost (httpservletrequest req, httpservletresponse res)
Throws servletexception, ioexception
{
// First set the header information
Res. setcontenttype ("text/html ");
// Return response data using writer
Printwriter out = res. getwriter ();
Out. println ("<HTML>
Out. println ("name:" + Req. getparameter ("myname "));
Out. println ("<br> ");
Out. println ("Love:" + Req. getparameter ("love "));
Out. println ("</body>
Out. Close ();
}
Public void doget (httpservletrequest req, httpservletresponse res)
Throws servletexception, ioexception {
Dopost (req, Res );
}
}
6-1 What is a component? What are the advantages of using components?
Currently, software development has switched to Component-Based Development. Currently, representative component technologies include Microsoft's com and COM +, sun's JavaBean and EJB (Enterprise Java Bean ), in addition, there is a Common Object Request Broker Architecture (Common Object Request Broker structure)
The JavaBean specification introduces the concept of "software components" into the field of Java programming. Components are self-contained and reusable software units, while javajan components can be visually written into Java programs using Visual application development tools. The JavaBean specification provides Java developers with a "componentized" Method for their Java classes.
6-2 What should I pay attention to when writing JavaBean?
1. if the name of the member variable of the class is XXX, two methods are used in the class to change or obtain the value of the member variable:
Getxxx () is used to obtain the property XXX.
Setxxx () is used to modify the property XXX.
2. For Boolean member variables, use is instead of get and set.
3. The access attribute of methods in the class must be public.
4. If there is a constructor in the class, the constructor is public and has no parameters.
6-3 briefly describe the JSP design mode. (Enterprise pen exam)
JSP design patterns include: (1) model1, JSP + JavaBean design patterns. (2) model2, MVC design pattern.
In this mode, the JSP page responds to the request independently and returns the processing result to the customer. All database operations are implemented through JavaBean. Using this mode in large quantities often leads to embedding a large amount of Java code in JSP pages. This situation becomes very bad when the business logic to be processed is very complicated. A large amount of Java code makes JSP pages very bloated. The front-end page designer is a little careless, and may damage the Code related to the business logic.
The "MVC" mode is the "Model-View-controller" mode. In this mode, JSP technology is used to present pages, Servlet technology is used to complete a lot of transaction processing work, and the user's business logic is realized.
In this mode, the servlet is used to process the request transaction and acts as the controller ("C, the servlet is responsible for responding to the customer's request to the business logic and deciding which JSP page to send to the customer based on the user's request behavior. The JSP page is at the presentation layer, that is, the view (View) role. Javabean is responsible for data processing, that is, the role of the Model ("M.