Learning about Web Technology

Source: Internet
Author: User
Tags response code

Servlet

1:Tomcat 's directory structure

2: Publish first project

Under the webapps directory, create a directory that must be created according to the Java EE architecture:

Project

Index.html

Web-inf

Xml

Classes

Lib

1: The Way of publishing 1

Put it in the webapps directory .

2: Release mode 2

Put it in a separate directory, referenced in the server.xml file:

3: Stand-alone configuration file

4: Configure the Virtual host

To configure the above project City:www.inkeeper.com

Configure DNS

To configure a virtual host:

Unpackwars= "true" autodeploy= "true" >

</Host>

The default main project in Tomcat is ROOT

3: integration with Eclipse Step 1: Add the Tomcat server

After adding is complete:

Step 2: Add a project-server project

Start:

4: Development of the first helloword web project in Eclipse Step 1: Create a Web project

Step 2: View the structure of the created project

Directly right-click to run as:

5: three major components in Javaweb development

Filter- Highest level

Listener Level Second

Servlet level third - "learn from here."

The three components above are interfaces that require programmers to implement them. The Servlet container is also called by tomcat/ to instantiate this class.

Time to display the server:

JS script: No, because JS runs on the client side, it shows the customer's time .

When the server is displayed, you must use Java code.

6:Servlet

a Servlet is a component

Used to generate dynamic pages

Receives a user's request to output a response to the user.

A Servlet is a combination of Server+let that refers to a small program on a service.

1: View the servlet API

Jdk.chm was developed by javase .

Servlets are javase developed. So check another set of APIs, and there is no Servlet in the JDK class.

View:

Javax.servlet
Interface Servlet

Defines methods that all servlets must implement. Some methods are defined, and all subclasses must implement these methods.

A servlet is a small the Java program, that runs within a WEB server. Servlets receive and respond to requests from WEB clients, usually across

A Servlet is a Java applet that runs on a javaweb server , such as Tomcat . Receives a response request through the client. Often through the HTTP protocol.

HTTP, the hypertext Transfer Protocol. (Hypertext Transfer Protocol HTML)

HTTP Protocol - text Format:

Agreement for instructions:

GET ( request type get|post)/index.html ( Consult resource ) http/1.1--> request first line

Host: There are many www.baidu.com:80 request headers.

...

..

[ blank line ]

Request Body GET mode There is no data here,POST means there's data here .

Response protocol

http/1.1 (Response code ) OK

Response header

Server: ...

Blank Line

Body

The HTTP protocol is a stateless protocol

Step 1: Develop a class implementation interface

Public class Oneservlet implements Servlet {

/**

* This method is the initialization method when this class, instantiated, is called by the Tomcat container to the party <br>

* will only be called once

*/

@Override

Public void init (servletconfig config) throws servletexception {

System. err. println (" initialized :" + this) ;

}

/**

* methods to provide service to users, each time the user calls, will execute this method

*/

@Override

Public void Service (servletrequest req, servletresponse Res) throws Servletexception, IOException {

set the type of response to

Res.setcontenttype ("Text/html;charset=utf-8");

get output to the browser output stream

PrintWriter out = Res.getwriter ();

String str = new simpledateformat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ());

output to

System. err. println ("Service this:" + this);

Out.print ("<b> current time is :</b>" + str);

}

@Override

Public void Destroy () {

a method called by Tomcat to clean up some resources when Tomcat is closed

will only be called once

System. err. println ("destory:" + this);

}

Step 2: Configure it to Web. XML

<!-- configuration of the first Servlet --

<servlet>

<!-- Declare any name--

<servlet-name>one servlet</servlet-name>

<!-- The name of the specified class--

<servlet-class>cn.inkeeper.OneServlet</servlet-class>

</servlet>

< how!--configuration is accessed --

<servlet-mapping>

<!-- reference the name of the above declaration--

<servlet-name>one servlet</servlet-name>

<!-- configuration access URL--

<url-pattern>/one.html</url-pattern>

</servlet-mapping>

Step 3: Access

Http://localhost/helloworld/one.html

7:theServlet 's access process

8: instantiation time and number of instantiations of Servlet

Instantiation time

All of the default servlets are not instantiated when the project starts.

Instantiated and initialized the first time a user accesses.

Number of instantiations:

The Servlet defaults to a single instance, which is instantiated by Tomcat through reflection and called by Tomcat to initialize the method.

9: The life cycle approach of the Servlet

The life cycle initialization method must be called the following name, and must receive the following types of parameter classes:

This method will only be called once:

@Override

Public void init (servletconfig config) throws servletexception {

System. err. println (" initialized :" + this) ;

}

Here's how the service is provided in the lifecycle, which is called every time a user requests a server:

@Override

Public void Service (servletrequest req, servletresponse Res) throws Servletexception, IOException {

a method called by Tomcat to clean up some resources when Tomcat is closed -it will only execute once

@Override

Public void Destroy () {

a method called by Tomcat to clean up some resources when Tomcat is closed

will only be called once

System. err. println ("destory:" + this);

}

Ten: Through the above explanation

Two points:

1:theServlet is a singleton. This class is instantiated by the Tomcat container, and the Serv Let instance is maintained by the Tomcat server .

2: The life cycle of the Servlet:

Init (ServletConfig)

Service (REQ,RESP)

Destory ();

How to configure:

1: Declaring a class implements an interface

2: Configure this class in Web . XML <servlet><servlet-mapping>

3: Access by <url-pattern>/one is .../one

One: quick example

User Registration function:

Thought:

1: Develop a registered page. <form>

2: receiving data in the background development a Servlet receives the data passed by the user.

Step 1: Develop a registered page

<form method="POST" action="Reg">

<p> Please register </p>

<table>

Step 2: Develop the servlet to receive the user's registration

Public class Regiestservlet implements Servlet {

@Override

Public void Service (servletrequest req, servletresponse Res) throws Servletexception, IOException {

1: Set the encoding type

Req.setcharacterencoding ("UTF-8");

2: receive user's parameters <input type= "text" name= "name" in /> "Name"

String name = Req.getparameter ("name");

String pwd = req.getparameter ("pwd");

String sex = req.getparameter ("Sex");

String major = Req.getparameter ("major");

3: Display

Res.setcontenttype ("Text/html;charset=utf-8");// Set the format of the response

get Output Object

PrintWriter out =

Res.getwriter ();

Out.print (" name is :"+name+ "

Out.print (" password is :"+pwd+ "

Out.print (" Gender is :"+sex+ "

Out.print (" Education :"+major+ "

}

Step 3: Configure

<!-- Configure a second servlet --

<servlet>

<servlet-name>regServlet</servlet-name>

<servlet-class>cn.inkeeper.RegiestServlet</servlet-class>

</servlet>

< how!--configuration is accessed --

<servlet-mapping>

<servlet-name>regServlet</servlet-name>

<url-pattern>/Reg</url-pattern>

</servlet-mapping>

<form method="POST" action="Reg">

<p> Please register </p>

<table>

Learning about Web Technology

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.