Servlet and JSP knowledge Review (2) servlet Basics

Source: Internet
Author: User

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.

Related Article

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.