ServletAnd JSP knowledge Review (2) servlet Basics
· Generate HTML Servlet
· Inform the browser of the HTML when the browser sends the message next
-
Response. setcontenttype ("text/html ");
· Modify the println statement to build a valid Web Page
-The print statement application outputs the corresponding HTML Tag.
· Use the form syntax validator to check the generated HTML
-Http://validator.w3.org/
Http://www.htmlhelp.com/tools/validator/
· Servlet Packaging
· Move the file to a subdirectory that matches the expected package name
· Insert a package statement into a class file
-E. g
Package org. waityou. Test. servlet;
· Ensure that classpath specifies the top-level directory
· Include the package name in the URL
-E. g http: // localhost/servlet/org. waityou. Test. servlet. testservlet
· Some simple and practical tools used to build html
Public class
Servletutilities {
Public static
Final string doctype =
"<! Doctype
HTML public/"-// W 3c // DTD
HTML 4.0 "+
"Transitional // en/"> ";
Public static
String headwithtitle (String title ){
Return (doctype +
"/N" +
"<HTML>/N"
+
"<Head> <title>"
+ Title +
"</Title>
}
...
}
· Do not go to extremes
-The complete HTML generation package often does not work well.
-Better solutions when using the JSP framework.
· Servlet Lifecycle
· Init
-
It is executed only once when the servlet is loaded for the first time.
Not every request.
· Service
-
In the new thread, the server calls each request. Send
Doget, dopost, etc.
Do not overwrite this method!
· Doget, dopost, doxxx
-
Process get, post, and other requests.
-
Override these methods to provide the expected behavior.
· Destroy
-
Called when the server deletes the servlet instance.
It is not called after each request.
· Why should I not overwrite the service?
· You can add doput and dotrace to support other services.
· By adding the getlastmodified method, you can modify the date.
· The service method automatically provides the following support:
-Head request
-Options request
-Trace Request
· Other solutions: Let dopost call doget
· Servlet debugging
· Use print statements to run servers on a desktop computer
· Use Apache log4j
· Use the Integrated ide Debugger
· Directly view the HTML source code
· Return the error page to the customer
-
Pre-plan for missing or abnormal data
· Use Log Files
-Log ("message") or log ("message", throwable)
· Process request and response data separately
· Stop and restart the server
· Summary
· The main servlet code is in doget or dopost:
-
Httpservletrequest contains input information
-
Httpservletresponse allows us to set the sent information
· Call setcontenttype to specify the MIME type
· Call getwriter to obtain the client-specific writer
· Put the one-time setting code in init
-Servlet is initialized and loaded only once.
-Multiple servlet calls
-Set initialization parameters in Web. xml.