Java Web Servlet知识点讲解(二)

一、定义Servlet:

 

public class HelloServlet extends HttpServlet {

@Override  protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("get method invoke");

 PrintWriter out=response.getWriter();

out.print("hello, HTTP GET method!");

}

@Override  protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("post method invoke");

PrintWriter out=response.getWriter();

out.print("hello, HTTP POST method!");

     }

  }

 

 二、发布Servlet:

 
 

方法一:注解方式

@WebServlet(name = "HelloServlet", urlPatterns = {"/hello","/greeting"})

@WebServlet注解属性:

 
 

 Java Web Servlet知识点讲解(二)_第1张图片

 
 

 

  方法二:web.xml配置方式  

 
 

 
 

    This is a hello greeting servlet

 
 

   Hello Servlet

 
 

   HelloServlet

 
 

 
 

 
 

   HelloServlet

 
 

   /greeting

 
 

   /hello

 
 

 
 

web.xml配置标签:  

 
 

<welcome-file-list>  <welcome-file>welcome-file> welcome-file-list>

 
 

<servlet-mapping>  <servlet-name>servlet-name>  <url-pattern>url-pattern> servlet-mapping>

 
 

<session-config>  <session-timeout>session-timeout> session-config>

 
 

<servlet>  <description>description>  <display-name>display-name>  <icon>icon>  <init-param>init-param>  <jsp-file>jsp-file>  <load-on-startup>load-on-startup>  <run-as>run-as>  <security-role-ref>security-role-ref>  <servlet-class>servlet-class>  <servlet-name>servlet-name> servlet>

 
 

<service-ref>  <description>description>  <display-name>display-name>  <handler>handler>  <icon>icon>  <jaxrpc-mapping-file>jaxrpc-mapping-file>  <port-component-ref>port-component-ref>  <service-interface>service-interface>  <service-qname>service-qname>  <service-ref-name>service-ref-name>  <wsdl-file>wsdl-file> service-ref>

 
 

<security-role>  <description>description>  <role-name>role-name> security-role>

 
 

<security-constraint>  <auth-constraint>auth-constraint>  <display-name>display-name>  <user-data-constraint>user-data-constraint>  <web-resource-collection>web-resource-collection> security-constraint>

 
 

<resource-ref>  <description>description>  <res-auth>res-auth>  <res-ref-name>res-ref-name>  <res-sharing-scope>res-sharing-scope>  <res-type>res-type> resource-ref>

 
 

<resource-env-ref>  <description>description>  <resource-env-ref-name>resource-env-ref-name>  <resource-env-ref-type>resource-env-ref-type> resource-env-ref>

 
 

<mime-mapping>  <extension>extension>  <mime-type>mime-type> mime-mapping>

 
 

<message-destination-ref>  <description>description>  <message-destination-link>message-destination-link>  <message-destination-ref-name>message-destination-ref-name>  <message-destination-type>message-destination-type>  <message-destination-usage>message-destination-usage> message-destination-ref>

 
 

<message-destination>  <description>description>  <display-name>display-name>  <icon>icon>  <message-destination-name>message-destination-name> message-destination>

 
 

<login-config>  <auth-method>auth-method>  <form-login-config>form-login-config>  <realm-name>realm-name> login-config>

 
 

<locale-encoding-mapping-list>  <locale-encoding-mapping>  <locale>locale>  <encoding>encoding> locale-encoding-mapping> locale-encoding-mapping-list>

 
 

<listener>  <description>description>  <display-name>display-name>  <icon>icon>  <listener-class>listener-class> listener>

 
 

<jsp-config>  <jsp-property-group>  <description>description>  <display-name>display-name>  <el-ignored>el-ignored>  <icon>icon>  <include-coda>include-coda>  <include-prelude>include-prelude>  <is-xml>is-xml>  <page-encoding>page-encoding>  <scripting-invalid>scripting-invalid>  <url-pattern>url-pattern> jsp-property-group>  <taglib>  <taglib-location>taglib-location>  <taglib-uri>taglib-uri> taglib> jsp-config>

 
 

<icon>  <large-icon>large-icon>  <small-icon>small-icon> icon>

 
 

<filter-mapping>  <dispatcher>dispatcher>  <filter-name>filter-name>  <servlet-name>servlet-name>  <url-pattern>url-pattern> filter-mapping>

 
 

<filter>  <description>description>  <display-name>display-name>  <filter-class>filter-class>  <filter-name>filter-name>  <icon>icon>  <init-param>init-param> filter>

 
 

<error-page>  <error-code>error-code>  <exception-type>exception-type>  <location>location> error-page>

 
 

<env-entry>  <description>description>  <env-entry-name>env-entry-name>  <env-entry-type>env-entry-type>  <env-entry-value>env-entry-value> env-entry>

 
 

<ejb-ref>  <description>description>  <ejb-link>ejb-link>  <ejb-ref-name>ejb-ref-name>  <ejb-ref-type>ejb-ref-type>  <home>home>  <remote>remote> ejb-ref>

 
 

<ejb-local-ref>  <description>description>  <ejb-link>ejb-link>  <ejb-ref-name>ejb-ref-name>  <ejb-ref-type>ejb-ref-type>  <local>local>  <local-home>local-home> ejb-local-ref>

 
 

<display-name>display-name>

 
 

<description>description>

 
 

<context-param>  <description>description>  <param-name>param-name>  <param-value>param-value> context-param>

在发布一个组件时,同时使用web.xml与注解时,则注解优先于web.xml里的配置。

 
三、访问Servlet

HTTP请求最常用的方式有两种:GET和POST。

1、GET:

通过HTTP GET请求访问HelloServlet发布时的url进行访问:http://localhost:8080/hello

 

2、POST:(form表单)

 

hello servlet 实例

Get方式请求servlet


 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 

 

你可能感兴趣的:(Java Web Servlet知识点讲解(二))