JavaWeb study notes (8)

JavaWeb deployment project with Eclipse

Here is the Eclipse running web in a path project

C:\Users\10301\Desktop\gz-java01\03.JavaWeb\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0

URL: Uniform Resource Locator (URL)

URI: Uniform Resource Identifier

http://localhost:8080/hello/1.html

Protocol  host IP (port number) URI of the (resource path of the current application)

With Tomcat lifecycle management platform for managing Web applications

Tomcat Tomcat management platform is itself a Web application, the corresponding manager application management platform, which is located in the Tomcat installation directory / webapps / manager directory.

manager application users will be secure authentication. It requires the user to have a manager role. Therefore, you should add user information with the manager role in Tomcat, open the way to Tomcat installation directory / conf /tomcat-users.xml file in which to add the following:

<tomcat-users>

         <role rolename="manager-gui"/>

<user username="tomcat" password="123456" roles="manager-gui"/>

</tomcat-users>

 

Then log in to the background, click below flap ManagerApp

 

Use mysqldump command in the console can be used to generate scripts text specified database, but note that the script contains only text contents of the database, but the database does not exist to create a statement! So when restoring data, you also need to manually go to recover their own data after you create a database.

  mysqldump -u username -p password database name> Script file path generation

Servlet 

  • Servlet effect processing request
  • When the browser to access a http: // localhost: 8080 / hello / hello path, it sends a request to the tomcat

1.4 Servlet life cycle (important)

  • Servlet life cycle of four states: instantiation -> Initialization -> Services -> destruction

 

  • Born :( instantiation -> Initialize) first visit Servlet was born (by default)
  • Alive :( Service) application alive, servlet to live
  • :( death destruction) servlet application is uninstalled on the destruction.
package com.zx.web.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class HelloServlet implements Servlet{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
    }

    public HelloServlet() {
        System.out.println("Hello Servlet");
    }
    
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("销毁");
    }

    @Override
    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("初始化init");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("服务Service");
        //返回结果给客户端
        res.getWriter().write("Hello Servlet");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>hello</display-name>
  <!-- 配置Servlet,也就是配置一个请求路径 -->
  <servlet>
      <servlet-name>HelloServlet</servlet-name>
      <servlet-class>com.zx.web.servlet.HelloServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
  </servlet>
  <!-- 映射 -->
  <servlet-mapping>
      <servlet-name>HelloServlet</servlet-name>
      <url-pattern>/hello</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Servlet implementation of three ways

  • Achieve javax.servlet.Servlet Interface
  • Inherit javax.servet.GenericServlet class (adapter mode)

  • Javax.servlet.http.HttpServlet inherited class (Template Method design pattern)

package com.zx.web.servlet;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class HelloServlet3 extends GenericServlet{

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        // TODO Auto-generated method stub
        res.getWriter().write("hello");
    }

    

}
package com.zx.web.servlet;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet{
    
    /**
     * get请求
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //响应给客户端
        resp.getWriter().write("hello Servlet---");
    }
}

url-pattern:. * .do to the request string * can be accessed Note: Do not add /

url-pattern: / * any string can be accessed

url-pattern: / action / * to request / action beginning can access

Matching rules:

Priority: high to low

Absolute match -> / beginning of the match -> Extensions way match

 

If the url-pattern value is / , showing the implementation of the default mapping . All resources are servlet

 

Guess you like

Origin www.cnblogs.com/zhangwugai/p/11264437.html