Java Web Servelt

Source: Internet
Author: User
Tags java web

Verification Code:

1. length is 5; from "a-z;0-9." selected in the.

Registered:

The user fills out the form (user name, real name, password, gender), and the server is processed according to the following process:

First: Verify that the verification code is correct. Not correct, prompt the user.

1. Check if the user name exists and prompt the user if it exists. Otherwise proceed to the next step.

2. Insert user-submitted information into the database and jump to the landing page.

2. Filter


(1) What is a filter?

A component defined in the servlet specification that is used to intercept a container's invocation process.

(2) How to write a filter?

A. Write a Java class that implements the filter interface.

B. Implement interception processing logic in the Dofilter method.

C. Configure the filter (Web. xml).

(3) Priority of filter

When there are multiple filters to meet the filter conditions

The container invokes the filter according to the order of <filter-mapping>.


Practice:

Write a filter, check the number of comments, if more than 20, comment length too many words.

(4) Filter initialization parameters

A. Configuring initialization parameters

B. Read

Filterconfig.getinitparamter ("");

(5) The advantages of the filter.

A. Add new functionality without modifying the original code.

B. The same processing logic of multiple components can be centrally written in the filter for easy code maintenance.

Listener:

(1) What is a listener?

A special custom component, defined in the servlet specification, used to listen for events generated by a container;

Note:

Two major categories of events

1) life-Cycle related events:

The container creates or destroys events generated by the Request,session,servlet context.

Number of people online.

2) binding data-related events:

The container strip uses the setattribute of the Request,session,servlet context,

RemoveAttribute method.

(2) What is the servlet context?

Once the container is started, a unique object that meets the ServletContext interface requirements is created for each web app.

Characteristics:

A. Uniqueness

A Web application that corresponds to a context;

B. Persistence as long as the application is not unloaded, the container is not closed.

2) How to get the servlet context.

Genericservlet,servletconfig,filterconfig,httpsession

The Getservletcontext method is provided.

3) The role of the servlet context

A. Data that is bound to the context will persist.

The Request<session<servlet context.

B. Data bound to the context can be shared by all users.

C. The global initialization parameters can be accessed with context.

To access the global initialization parameters:

1. Configure:<context-param><param-name><param-value>

2. Visit: Servletcontext.getinitparameter ();



How do I write a listener?

1. Write a Java class that implements the appropriate interface based on the type of event you want to listen to:

2. In the Listener interface method, implement the listening processing logic.

3. Registration (web. xml)

Count the number of people online.

1.HttpSessionListener

2.ServletContentListener

3ServletRequestListener

2. Thread safety issues

(1) Why does the servlet have a thread-safe problem?

A. Container by default, only one instance is created for a servlet of a type.

B. After the container receives the request, a thread is started to process the request.

For example, if there are multiple requests to access the same servlet, there will be multiple threads calling the Servlet object's service method.

There is a potential for thread safety issues (for example, these threads are modifying the properties of the servlet).

(2) How to solve

A. Mode one: Use the synchronized Code yoke for thread safety issues.

B. Let the servlet implement the Singlethreadmodel interface. (Not recommended)

Marking interface Note: The container creates multiple instances of a servlet that implements the interface (one thread assigns an instance).

Servlet Knowledge Summary:

(1) Servlet Basics: A. What is servlet?b. How to write a servlet?c. How does it work? D.http Protocol (understanding) E. What is a servlet container?

(2) servlet core: A.servlet output Chinese B. The form contains the Chinese parameter how to handle C. How to obtain the request parameter value string[] request.getparametervalues (String paramname);

? interest=cooking&interest=snoking.

D. How the container handles the request resource path. E. How to combine multiple servlets into one.

F. Forwarding and redirection. The G.servlet life cycle h.servlet context. I.servlet thread safety issues.

(3) state management

1. What is state management

2.cookie

A. What is a cookie?

B. How do I add cookies?

C. Considerations for adding cookies: coding problems, Time-to-live, path problems.

D. Read cookies

Restrictions on cookies

3.session

What is a session?

How do I get it?

Several common methods of session

Session Timeout

Delete session

(4) Database access

A. Use JDBC to access the database.

Use DAO to access the database.

(5) Filter a listener

A. What is a filter

How do I write a filter?

Priority of the filter

Initialize parameters

Advantages of filters

What is a listener

How to write.

(6) Typical cases

A. Employee management

B. Login and Session Verification

C. Verification code

Jsp

1.jsp

How does it work?

A. The container converts the JSP into a servlet class.

HTML (CSS,JS)--->service method with out.write output

<%%>--->

Java code?

<%! declares a variable or method%>

As a new member variable or a member method.

3. Hidden objects?

Why can I use implicit objects directly?

What are the hidden objects?

Out,request,response,session,application,

PageContext, Exception,config,page.

Page (the JSP instance itself).

PageContext

A. The container creates a unique object for each JSP instance that meets the requirements of the PageContext interface. The object will persist (unless the JSP instance is deleted)

The role of PageContext

A. Role 1: Binding data (data bound to the PageContext context, only the corresponding JSP instance can access it)

Role 2: Provides some methods to get all the other hidden objects.


exception

Used to obtain exception information that is generated by the JSP during operation.


Config

ServletConfig Object


Instructions

What is an instruction?

What are the instructions?

Page directive, include directive, taglib directive. JSP tags.

The properties of the page directive

Import: Guide Pack

Pageencoding

ContentType

ErrorPage Property: Specifies an exception handling page.

When the JSP runtime produces an exception, the page specified by the property is called.

Iserrorpage property: True/false can be used to suppress objects when the default value is true. Exception.

Session Property: True/false

The value is false and the container no longer adds the code that gets the session object (that is, you cannot use the session implicit object.) ).

Include directives

File property: Inserts the contents of the files specified by the document property into the location of the directive.


Comments:

1) <!--the contents of the note--

Content of <%--annotations--%>


Note: If the comment content is Java code, the Java code in the first comment is executed.

-------------------------------------------------------------------------





Java Web Servelt

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.