博客
关于我
Servlet 体系结构与 urlpartten 配置_hehe.employment.over.14.1
阅读量:387 次
发布时间:2019-03-05

本文共 1316 字,大约阅读时间需要 4 分钟。

Servlet 开发入门:从基础到实践

Servlet 接口与抽象类结构

Servlet 是 JavaWeb 开发的核心组件,了解它的结构是掌握 Servlet 开发的关键。在 Java EE 规范中,Servlet 接口定义了一个标准的服务逻辑接口,而 GenericServlet 则是这个接口的抽象实现。

GenericServlet 类的设计思想是:将 Servlet 接口中除了 service 方法之外的所有方法进行了默认空实现。这样一来,开发者在定义 Servlet 类时,只需要继承 GenericServlet,并实现 service 方法即可。这使得 Servlet 开发更加灵活和简便。

HttpServlet 是对 Http 协议的一种封装,它为开发者提供了一个更加简化的操作流程。要使用 HttpServlet,开发者只需要:

  • 定义类继承 HttpServlet
  • 复写 doGet() 和 doPost() 方法
  • URL 路径配置

    在 Web 开发中,URL 路径配置是组织 Servlet 资源的重要方式。UrlPartten 提供了灵活的路径配置机制,允许一个 Servlet 定义多个访问路径。

    路径配置规则如下:

  • 单路径访问:如 /xww,访问方式为 http://localhost:8080/xww
  • 多级路径:如 /x/xww,访问方式为 http://localhost:8080/x/xww
  • 扩展名匹配:如 .do,访问方式为 http://localhost:8080/xww.do
  • 通过合理配置 UrlPartten,可以为不同功能模块或资源分配不同的访问路径,实现灵活的 URL 映射。

    示例代码

    以下是一个简单的 Servlet 实现示例:

    package com.xww.web.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet({"/x", "/xw", "/xww"})public class ServletUrlDemo extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        System.out.println("doget....");    }}

    这个示例展示了如何通过 UrlPartten 定义多个访问路径,并通过相应的方法处理不同请求类型。

    转载地址:http://skgwz.baihongyu.com/

    你可能感兴趣的文章
    ViewHolder的改进写法
    查看>>
    Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
    查看>>
    org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
    查看>>
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>
    org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
    查看>>
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>