当前位置:网站首页>Servlet基础
Servlet基础
2022-04-22 15:52:00 【追梦梓辰】
Servlet基础
Servlet基础学习文档地址:https://pan.baidu.com/s/10hQtJ8b5SPPenEBzZ6A47w 提取码:54lt
1:Servlet的生命周期

代码:
@WebServlet("/serverss")
public class serverss implements 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 arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("初始化了");
}
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//服务
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
}
}
2:Servlet的生命周期的各个阶段:

ServeltConfig接口
在Servlet初始化过程中获取配置信息
一个Servlet只有一个ServletConfig对象
ServeltConfig接口的常用方法

@WebServlet("/CHeck")
public class CHeck extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用dopost
doPost(request, response);
}
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取username
String uname=request.getParameter("uname");
//调用biz层 根据用户名查找数据库是否存在这个用户
String name="admin";
String result="";
if(uname.equals(name)) {
result="该用户已存在";
}else {
result="可以注册";
}
//响应给用户
PrintWriter out=response.getWriter();
out.println(result);
}
}
ServeltConText接口
获取Servlet上下文
ServeltConText接口的常用方法

GenericServlet抽象类
提供了Servlet与ServletConfig接口的默认实现方法
GenericServlet的常用方法

HttpServlet继承于GenericServlet
处理HTTP协议的请求和响应
HttpServlet的方法

那我们如何在项目中部署呢?
如下图:

版权声明
本文为[追梦梓辰]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_63719049/article/details/124331845
边栏推荐
- 太卷了~(2022版)大厂面经 + 详细笔记帮你搞定面试
- [in-depth understanding of tcallusdb technology] sample code for reading all data in the list - [list table]
- [deeply understand tcallusdb technology] update data example code - [generic table]
- 【基于合泰HT32F52352+oled温湿度显示】
- 企业级知识管理(KM)建设方法及过程
- Wireguard article series (6): netmaker installation
- 存在安全隐患 东风日产召回部分逍客
- Do you know how to generate random numbers? (over 3000 words)
- [milestone] wireguard series articles (7): creating a full mesh network using wireguard and netmaker
- 「译文」如何在YAML中输入多行字符串?
猜你喜欢

这个陀螺仪的精度太高了,还是建议禁止使用吧。

Vscode handles code merge conflicts

Computer Vision L7 -- Self-supervised Learning

CASIA webface of dataset: a detailed introduction to the introduction, installation and use of CASIA webface dataset
![[in depth understanding of tcallusdb technology] asynchronous scanning data example code - [generic table]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcallusdb technology] asynchronous scanning data example code - [generic table]

MySQL operator explanation and regular expression query
![[in depth understanding of tcallusdb technology] sample code for reading data - [generic table]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcallusdb technology] sample code for reading data - [generic table]
![[in depth understanding of tcallusdb technology] sample code of batch reading data - [generic table]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcallusdb technology] sample code of batch reading data - [generic table]

性能飙升66%的秘密:AMD 2.5万元768MB 3D缓存霄龙首次开盖

一个页面向同一个地方提交两个form表单
随机推荐
The translation uses Prometheus and grafana to realize SLO
Huawei cloud media Zha Yong: Huawei cloud's technical practice in the field of video AI transcoding
建筑业未来的发展方向:数字化工厂管理系统
[in depth understanding of tcallusdb technology] sample code for reading data according to some key field values - [generic table]
理想与集度的技术之争:激光雷达究竟装哪儿更安全?
AI智能视频技术如何应用在文物古迹建筑日常养护监管场景中?
短链接生成器,adf.ly、shorte.st、ouo.io、adfoc.us哪个更好,有哪些区别
jsquery.xxx.js和jsquery.xxx.min.js的区别
Will NFT impact the native culture of the Internet?
SAP UI5 应用开发教程之七十一 - SAP UI5 页面的嵌套路由试读版
Frequently asked questions about recent BSN development
[in depth understanding of tcallusdb technology] sample code of batch reading data - [generic table]
[in depth understanding of tcallusdb technology] example code for deleting data - [generic table]
CommitFailedException异常,原因以及解决方案
电脑任务栏卡住
這個API Hub厲害了,收錄了釘釘企業微信等開放Api,還能直接調試 !
Redis optimization series (I) building redis master-slave based on docker
腾讯云堡垒机开启OTP认证
推进制造业数字化转型的几个建议
How can AI intelligent video technology be applied to the scene of daily maintenance and supervision of cultural relics and historic buildings?