JSP servlet Basics

Source: Internet
Author: User
Tags server memory

Describe what a servlet is

A servlet is a Java class that implements a particular interface or parent class.

A servlet is a small program that runs on the server side. A servlet is a Java class and can be accessed through the request-response programming model, a servlet program that resides in server memory.

Tomcat Container LevelTomcat's container hierarchy is divided into four tiers, the servlet container manages the context container, and a context corresponds to a Web project. Engine is a container of engines. Host is a master container

Implement the servlet three scenarios:

01.Servlet Interface: 5 methods

Init () {

Initialization

}

Service () {

Processing requests

}

The mudslide destroyed the whole village.

Destory () {

Destroyed

}

Getservletconfig () {

Get servlet configuration information

}

Getservletinfo () {

Get information about Servlets, such as version authors.

}

5 methods: Init (): initialized, only executed once

Destory (): Tomcat executes when it is closed, frees resources, executes once

Service (): Handles client requests and responds to client requests

Getservletconfig (): Get Configuration

Getservletinfo (): Version and other information

02. Implement Genericservlet Abstract class

Modify the Servlet class to restart the server, and modify the JSP page can not restart

03. Implement HttpServlet Abstract class

Service (): Scheduling role

If our own Servlet class inherits the HttpServlet abstract class, then there is no need to rewrite the service () of the parent class, and the service () method is just a dispatch function.

Doxxx:dopost (httpservletrequest request,httpservletresponse response) doget ()

steps to manually write a servlet program1. Inherit HttpServlet 2. Rewrite doget () or Dopost () Method 3. Registering a servlet in Web. xml
    • To register a servlet using a common method
  1. <servlet>
  2. <!--servlet的名字-->
  3. <servlet-name>MyServlet</servlet-name>
  4. <!--servlet类的名字(包括完整包名)-->
  5. <servlet-class>MyServlet</servlet-class>
  6. </servlet>
  7. <servlet-mapping>
  8. <!--要与上面的那个名字一一对应-->
  9. <servlet-name>MyServlet</servlet-name>
  10. <!--表示该servlet的访问路径 /表示项目的根目录-->
  11. <url-pattern>/servlet/MyServlet</url-pattern>
  12. </servlet-mapping>
    • JavaEE6 and above can use annotations to register a servlet
Name is required, urlpatterns or value for the specified URL to access the address, or to define multiple URL address access
    1. @WebServlet(name = "MyFirstServlet",urlPatterns = "/servlet/MyFirstServlet")
@WebServlet has a number of properties:
asyncsupported Declares whether the servlet supports asynchronous operation mode
Description Description of the servlet
DisplayName Display name of the servlet
InitParams The init parameter of the servlet
Name Name of the servlet
Urlpatterns Access URL for the servlet
Value Access URL for the servlet
How the servlet gets the session object and gives the inside set data.

Parsing: Request.getsession (). SetAttribute (name, value)

Init () and destory () and service () execution times issues

Important: Init () and Destory () in the servlet are executed only once, and each time the client accesses the corresponding Servlet class, the service () is invoked once

the life cycle of the servlet1. Initialize phase, call Init () Method 2. In response to the client request phase, call the servlet () method.        The servlet () method chooses to execute the doget () or Dopost () method according to the submission method.  3. Termination phase, call Destroy () method the servlet life cycle phase includes initialization, loading, instantiation, service, and destruction. At the following time, the Servlet container is installed in some servlets automatically when the Servlet:1.servlet container is started, so it only needs to add the following code between <Servlet></Servlet> in the Web. xml file:        The smaller the <load-on-startup>1</load-on-startup> number, the higher the priority.        2. After the servlet container is started, the client sends a request to the servlet for the first time. After the 3.servlet class file is updated, remount the servlet. After the servlet is loaded, the servlet container creates a servlet instance and invokes the servlet's init () method for initialization. The init () method is called only once throughout the servlet's life cycle. Ervlet with nine large built-in objectsHow to get the nine built-in objects for JSPs in a servlet
JSP Object How to get
Out Response.getwriter methods (types are inconsistent, but functionally consistent)
Request Parameters in the service method
Response Parameters in the service method
Session Request.getsession () method
Application Getservletcontext () method
exception Throwable
Page This
PageContext PageContext
Config Getservletconfig method

how to get values in a JSP form from a servlet and how to send values to a JSPGet the values from the JSP form from the servlet:
    1. //获取单个值的时候
    2. String context=request.getParameter("username");
    3. //获取复选框等多个选址的时候
    4. String[] parameterValues = request.getParameterValues("username");
The servlet sends the value to the JSP to store the content in the session and sends the content to the JSP sample through request forwarding:
  1. // 将内容存储到session中
  2. request.getSession().setAttribute("a","object");
  3. // 通过请求转发将其发送给jsp页面
  4. request.getRequestDispatcher("index.jsp").forward(request,response);
servlet path JumpAbsolute path: Calculates "/" from the root to indicate the root relative path of the server: the mapping address of the servlet in the XML is calculated from relative to the current resource must begin with "/" when requesting redirection is used in the servlet. You can use the Request.getcontextpath method to get the context object after the target JSP name in the servlet using the server does not jump, you can use "/" to indicate the project's root directory Get initialization ParametersWhen you configure a servlet in Web. XML, you can configure some initialization parameters. In the servlet, these parameters can be obtained through the methods provided by the ServletConfig interface. Set initialization parameters in the servlet tag of XML
  1. <servlet>
  2. <servlet-name>GetInitParameterServlet</servlet-name>
  3. <servlet-class>servlet.GetInitParameterServlet</servlet-class>
  4. <!--设置初始化参数-->
  5. <init-param>
  6. <param-name>username</param-name>
  7. <param-value>admin</param-value>
  8. </init-param>
  9. <init-param>
  10. <param-name>password</param-name>
  11. <param-value>123456</param-value>
  12. </init-param>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>GetInitParameterServlet</servlet-name>
  16. <url-pattern>/servlet/GetInitParameterServlet</url-pattern>
  17. </servlet-mapping>
Overriding the Init () method in a servlet gets the Getinitparameter method by using the
  1. @Override
  2. public void init() throws ServletException {
  3. String username = this.getInitParameter("username");
  4. this.setUsername(username);
  5. }

JSP servlet Basics

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.