Java Web servlet Understanding

Source: Internet
Author: User
Tags server memory

One, there are two kinds of applications

1, desktop applications: Need to download, install, update and so on. such as Qq,office, etc.

2. Web application: It is the Web project that we develop, this does not need the user to download, only needs the user Client network, accesses the corresponding Internet resources.

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img9.ph.126.net/dC1MBIh0onBBIVqS-uiIhg==/6597816335051501009.jpg "/>

Second, the HTTP protocol

HTTP is an object-oriented Hypertext transfer Protocol belonging to the application layer, which is based on TCP/IP protocol and is suitable for distributed hypermedia information System because of its simple and fast way. HTTP Features:

(1) No connection: divided into 4 phases: Establish a connection, send a request, wait for a response, close the connection

Advantages: Fast, simple, and does not occupy network server resources

Disadvantage: The speed is determined by the network, the amount of data transferred can not be too large

(2) Stateless: The HTTP protocol is only responsible for transmitting data and is not responsible for keeping the transmitted data

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:auto;float:none;height:auto; "Src=" Http://img4.ph.126.net/Gw0SZ9cbJdHgqdWaPUYCng==/6597340246518050054.jpg "/>

How HTTP accesses server resources

1, URL: (uniform/universal Resource Locator) Uniform Resource Locator, the request is a resource on the server. A resource is an abstract concept that refers to all types of files (such as text, pictures, audio, video, etc.) that are stored on the server that can be accessed by the client.

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img9.ph.126.net/D8wx0Wq2y6Jz8EBfm4W6rQ==/6597197310006448239.jpg "/>

2. Form submission

The difference between the Get and post methods

The ⑴get method passes the parameter through the URL, the user is visible in the client address bar, it is unsafe to pass the password, the Post method passes the parameter through the request body, the user is not visible on the client, more secure.

The ⑵get method passes the request through the Address bar URL, and the URL itself has a length limit, typically no more than 255 characters, and the Post method passes the parameter through the request body without a length limit.

Third, Servlet

1. servlet Concept:

First, the servlet is a Java application-----> server-side Java application-----> has platform-and protocol-independent features-----> can generate Dynamic Web pages.

A servlet acts as the middle tier of a customer request (a Web browser or other HTTP client program) and a server response (a database or application on an HTTP server). Customer Request <----->Servlet<------> Server Response

2. servlet life cycle

Refers to the entire process from creation to invocation of a Servlet object in server memory, to destruction.

(1) Instantiation: When a client requests through a URL, the Web container automatically invokes the Servlet's construction method according to the XML configuration, instantiating the object.

(2) Initialize: Call the init () method from the Servlet object, read the configuration information of the servlet in Web. XML, and provide the relevant data for the service method.

(3) Service: Call the Service () method through this object, and if it is inherited HttpServlet, call the corresponding doget ()/dopost () According to the request method in the request header information

(4) Destroy: It is not called immediately after the service () method call, but is determined by the JVM. The Destroy () method of the instance is called when the JVM needs to destroy some objects and free up memory space.

3, HttpServletRequest (interface)

The public interface class HttpServletRequest inherits from ServletRequest. Requests made by the client browser are encapsulated as a HttpServletRequest object. All the information includes the requested address, the requested parameters, the submitted data, the uploaded file of the client's IP, and even the client operating system contained therein.

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;float:none; "Src=" http://img7.ph.126.net/ 6pyoyyktoiu9xpsnqzf_ca==/6597354540169215617.jpg "/>


650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img8.ph.126.net/-8-xgg0UoL53J0-onDGJeA==/6597369933332003888.jpg "/>


650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img6.ph.126.net/s25rFIMfcegxZNhLRyKG_Q==/6597185215378542076.jpg "/>

4, HttpServletResponse (interface)

The response returned by the server to the browser directly outputs the content to the browser, using the servlet to output the HTML page

PrintWriter out = Response.getwriter ();

Out.print ("Hello:");

Set the content type of the response:

Response.setcontenttype ("text/html; Charset=utf-8 ");

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img4.ph.126.net/a2tgDHQuPlF9BRicLLFrxg==/6597528263005024774.jpg "/>


650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img3.ph.126.net/ZomIbvPJtYuj9OVpaLi26A==/6597828429679406893.jpg "/>


650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img2.ph.126.net/Lt8y0-MY3NYYW1m00heupA==/6597737170214300602.jpg "/>


650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img2.ph.126.net/_1jLIt64raDCw5st8G2KfQ==/6597566745911994882.jpg "/>


Note: You can jump to a URL resource outside the project by using the response object jump

such as: Response.sendredirect ("http://www.baidu.com");

Use the Request object to jump, only in this project resource

such as: Request.getrequestdispatcher ("url"). Forward (Request,response);

Tell the browser not to cache the page:

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img0.ph.126.net/4xjRueitAwu2BuyPkZK_Og==/6597854817958473570.jpg "/>

Page refresh: Unit is seconds

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img5.ph.126.net/ni5wMGULHTyT0Ow9YVrHJA==/6597369933332003945.jpg "/>

5, Javax.servlet.RequestDispatcher (interface)

An interface that complements the request response, primarily responsible for two functions

RequestDispatcher disp = Request.getrequestdispatcher ("login_ok.jsp");

(1) Jump

Disp.forward (Servletrequest,servletresponse);

(2) contains

Disp.include (Servletrequest,servletresponse);

General ligatures in the program:

Request.getrequestdispatcher ("login_ok.jsp"). Forward (Request,response);

6, HttpSession (interface)

Is the most common way to save data on the server side

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img0.ph.126.net/ZTHei7LKAUEs06fGP8vAvQ==/6598191268516398740.jpg "/>

Session: A period of time in which an activity is performed, which can be done with the object. The scope of session information is limited to the current Web application (
ServletContext
), so that information stored in one context is not directly visible in another context.

The difference between httpsession in session and Servlet in Ⅰ.hibernate

Hibernate Session object: The main role is database connection, database operation

HttpSession objects in the servlet:

(1) Help server, identify different browsers

(2) Help the server to pass data to different servlet or JSP pages

(3) Ability to manually control the length of a continuous period of time.

Common methods for Ⅱ.httpsession objects:

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img7.ph.126.net/dUtcp8tKyFNeWDfqL2rMGg==/6597819633586384377.jpg "/>

(1) Create

HttpSession session = Request.getsession (true);

Requires the server side to recreate a session object

HttpSession session = Request.getsession (false);

You do not need to create a new Session object on the server side, just take one from the existing session

HttpSession session = Request.getsession ();

Not required, automatically assigned by the server

(2) Identify the browser

Randomly generate an ID that is not duplicated, and as the response is sent to the browser, bound in the browser, each time the browser requests, the ID is returned and compared to the server-side ID

String id = session.getid ();

(3) Transfer of data

Save data in session

Session.setattribute ("name", object);

Fetching data in other servlet or JSP pages

Object obj = Session.getattribute ("name");

In general, you need to determine whether it is empty, and then take the value

(4) Set the effective time

A Session object defaults to 30 minutes of validity time

To set the maximum effective time for a single Session object

Session.setmaxinactiveinterval (60*60*24 sec);

Set the effective time for all sessions in the entire project

<session-config>

<session-timeout>20000</session-timeout>

</session-config>

(5) Clear data

Session.removeattribute ("name");

(6) Setting session invalidation or passivation

Session.invalidate ();

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img5.ph.126.net/ZgGuSFE4GoLlPxj9dgt6FQ==/6598223154353603495.jpg "/>

For example:

Session like a locker, the main function is to access data

Locker for Bedroom:

(1) One person one

(2) Ability to access things

(3) A general cabinet a key

(4) Special circumstances, can be affixed to the cabinet seal

(5) Sometimes the cupboard may be shared by two or n individuals.

7, ServletContext (interface)

Definition of ⑴servletcontext

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img2.ph.126.net/51LG31RjQArctbfZwFOEKg==/6597695388772445242.jpg "/>

The ⑵servletcontext object has access to the initialization parameters of the Web project, the objects stored in the ServletContext, the resource files in the Web project, the logs, and ServletContext is the servlet container. The provided method can be used in all Servlets under the same Web application.

8, ServletConfig (interface)

650) this.width=650; "alt=" servlet study notes-Liu Runming-Meta Blue Guest "style=" margin:0px 10px;width:500px;float:none;height:auto; "Src=" Http://img3.ph.126.net/8BuvcahaVja-B4Mnc7IYIA==/6597958172051485313.jpg "/>


This article is from the "Program Ape Advanced Siege Lion" blog, please be sure to keep this source http://zhoum1118.blog.51cto.com/10054110/1639322

Java Web servlet Understanding

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.