Servlet学习

Posted bsfl-study

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet学习相关的知识,希望对你有一定的参考价值。

Servlet的三种创建方式

1.实现javax.servlet.Servlet接口(见一)

2.基础javax.servrt.GenericServler类(适配器模式,用哪个方法就写哪个,不需要所有父类方法都重写)

3.继承javax.servlet.http.HttpServlet类(模板方法设计,常用)

tomcat启动不起来的时候注意查看web.xml的配置文件

 

第二种方法例子:

package servlet;

import java.io.IOException;

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

public class ServletDemo2 extends GenericServlet{

    /**
     * 
     */
    private static final long serialVersionUID = -5102796265741373505L;

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        System.out.println("service");
    }

}

第三种方法例子:模板方法设计思想:多态

Servlet s = new ServletDemo3;

s.service()  -->  如果ServletDemo3类中重写doget和dopost方法则调用这两方法,如果没有重写则调用父类的service方法。

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo3 extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = -4123099903592371636L;
    
    //不能重写service方法,看源码service方法里面最后还是调用回了doget和dopost方法
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doget");
        System.out.println(req.getRemoteAddr());//获取requset的ip地址
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("dopost");
    }

}

web.xml配置文件

<?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_2_5.xsd" version="2.5">

<!-- 创建一个servlet实例 -->
<servlet>
    <servlet-name>ServletDemo1</servlet-name>
    <servlet-class>servlet.ServletDemo1</servlet-class>
    <!-- servlet在服务器启动时候就创建 -->
    <load-on-startup>2</load-on-startup>
    <!-- 数字越小优先级越高,0默认配置了一个 -->
</servlet>

<!-- 给servlet提供(映射)一个可供客户端访问的URI -->
<servlet-mapping>
    <servlet-name>ServletDemo1</servlet-name>
    <url-pattern>/demo1</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>ServletDemo2</servlet-name>
    <servlet-class>servlet.ServletDemo2</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ServletDemo2</servlet-name>
    <url-pattern>/demo2</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>ServletDemo3</servlet-name>
    <servlet-class>servlet.ServletDemo3</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ServletDemo3</servlet-name>
    <url-pattern>/demo3</url-pattern>
</servlet-mapping>

<!-- 欢迎页面 -->
<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

 

servlet继承关系:

servlet --> GenericServlet --> HttpServlet --> 自定义servlet类

 

自动创建servlet,配置url:/demo4  http://loacalhost:8080/demo4

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletDemo4
 */
@WebServlet("/demo4")
public class ServletDemo4 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletDemo4() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("post");
		doGet(request, response);
	}

}

servlet映射细节:

一个servlet类可以对应多个url,如下都能找到ServletDemo3这个类:

http://localhost:8080/demo3  http://localhost:8080/demo33  http://localhost:8080/demo333都可

  <servlet-mapping>
    <servlet-name>ServletDemo3</servlet-name>
    <url-pattern>/demo3</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ServletDemo3</servlet-name>
    <url-pattern>/demo33</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ServletDemo3</servlet-name>
    <url-pattern>/demo333</url-pattern>
  </servlet-mapping>

url通配:*.;/*,/action/*。三样都写上容易出问题。

  <servlet-mapping>
    <servlet-name>ServletDemo3</servlet-name>
    <url-pattern>*.do</url-pattern><!-- 表示所有以.do结尾的都能找到ServletDemo3类,http://localhost:8080/xxx.do -->
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ServletDemo3</servlet-name>
    <url-pattern>/*</url-pattern><!-- 表示以/结尾都能找到ServletDemo3类,http://localhost:8080/ooo -->
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ServletDemo3</servlet-name>
    <url-pattern>/action/*</url-pattern><!-- 表示以/action/*结尾都能找到ServletDemo3类,http://localhost:8080/action/ooo -->
  </servlet-mapping>

url优先级:绝对匹配 --> /开头匹配 --> 扩展名方式匹配

如果url-parttern的值是/,表示执行默认映射。所有资源都是servlet。

http://loacalhost:8080/learn/index.html在tomcat的配置文件web.xml中,有个默认配置通配/,用于找到项目中符合的页面

判断:

对于如下的一些映射关系
1.servlet1 --> /abc/*
2.servlet2 --> /*
3.servlet3 --> /abc
4.servlet4 --> *.do

当请求url为/abc/a.html时候,
/abc/*和/*都匹配,哪个servlet响应。
实例化/abc/*(路径更深)1

当请求url为/abc时,/abc/*和/abc都匹配,哪个servlet响应。
实例化/abc(绝对)3

当请求url为/abc/a.do时候,/abc/*和*.do都匹配,哪个servlet响应。
实例化/abc/* 1

当请求url为/a.do时候,/*和*.do都匹配,哪个servlet响应
实例化/*(斜杠优先)2

当请求url为/xxx/yyy/a.do时候,/*和*.do都匹配,哪个servlet响应。
实例化/*(斜杠优先)2

 

以上是关于Servlet学习的主要内容,如果未能解决你的问题,请参考以下文章

Servlet学习(代码)

IOS开发-OC学习-常用功能代码片段整理

filter与servlet

java SpringRetry学习的代码片段

python 机器学习有用的代码片段

学习笔记:python3,代码片段(2017)