当前位置:网站首页>Serevlt初识
Serevlt初识
2022-06-25 12:19:00 【Bo0o2】
一 servelt是什么
setvelt是一种实现动态页面技术,是一组Tomcat提供给程序员的API,帮助程序员简单高效的开发
二 Servlet 运行原理
在 Servlet 的代码中我们并没有写 main 方法, 那么对应的 doGet 代码是如何被调用的呢? 响应又是如何返回给浏览器的?
Tomcat 的定位
我们自己的实现是在 Tomcat 基础上运行的。
当浏览器给服务器发送请求的时候, Tomcat 作为 HTTP 服务器, 就可以接收到这个请求.
HTTP 协议作为一个应用层协议, 需要底层协议栈来支持工作. 如下图所示:
三 HttpServlet
我们写 Servlet 代码的时候, 首先第一步就是先创建类, 继承自 HttpServlet, 并重写其中的某些方法
代码实例处理get请求:
创建doget方法
@WebServlet("/method")
public class method extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("GET response");
}
}

在浏览器中输入url地址,这样浏览器就给服务器发送了一个get请求
代码实例处理post请求:
先创建一个静态的html
<button onclick="sendPost()">发送 POST 请求</button>
<script> function sendPost() {
ajax({
method: 'POST', url: 'method', callback: function (body, status) {
console.log(body); } }) } </script>
接着创建post方法:
在method中创建dopost方法:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
resp.getWriter().write("POST 响应");
}
在浏览器中单击提交“post请求‘’
四 访问出错怎么办?
初学 Servlet, 遇到的这类问题会非常多. 我们不光要学习 Servlet 代码的基本写法, 也要学习排查错误的
思路.
程序猿调试 BUG 如同医生诊病.
一个有经验的程序猿和一个新手程序猿相比, 最大的优势往往不是代码写的多好, 而是调试效率有
多高. 同一个问题可能新手花了几天都无法解决的, 但是有经验的程序猿可能几分钟就搞定了.
熟悉 HTTP 协议能够让我们调试问题事半功倍.
4xx 的状态码表示路径不存在, 往往需要检查 URL 是否正确, 和代码中设定的 Context Path 以及
Servlet Path 是否一致.
5xx 的状态码表示服务器出现错误, 往往需要观察页面提示的内容和 Tomcat 自身的日志, 观察是否
存在报错.
出现连接失败往往意味着 Tomcat 没有正确启动, 也需要观察 Tomcat 的自身日志是否有错误提示.
空白页面这种情况则需要我们使用抓包工具来分析 HTTP 请求响应的具体交互过程.
边栏推荐
- 更新pip&下载jupyter lab
- [visio] solving the fuzzy problem of parallelogram in word
- Micro engine remote attachment 7 Niu cloud upload
- 为何数据库也云原生了?
- JS SMS countdown implementation (simple code)
- 架构师需要具备的能力
- Elemntui's select+tree implements the search function
- Laravel echart statistical chart line chart
- Ramda rejects objects with null and empty object values in the data
- Optimal solution for cold start
猜你喜欢

Drawing cubes with Visio

3+1 guarantee: how is the stability of the highly available system refined?

地理空间搜索:kd树的实现原理

三入职场!你可以从我身上学到这些(附毕业Vlog)

Another night when visdom crashed
![[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1](/img/36/167397ce61240036c865dd99463f1b.jpg)
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1

Guess Tongyuan B

Go novice exploration road 2

Talk about 11 key techniques of high availability

深圳民太安智能二面_秋招第一份offer
随机推荐
Go defer little knowledge
顺序表的折半查找法
最大数[抽象排序之抽象规则]
Jupyter notebook theme font setting and automatic code completion
Three jobs! You can learn this from me (attached with graduation vlog)
JS enter three integers a, B and C, and sort them from large to small (two methods)
2021-09-30
Parse JSON format data and save it to entity class
2021-10-21
(5) Pyqt5 ---- another method of connecting signals and slots
三入职场!你可以从我身上学到这些(附毕业Vlog)
Common software numerical filtering methods (I) have been applied
torch.tensor拼接与list(tensors)
(6) Pyqt5--- > window jump (registration login function)
(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
Micro engine remote attachment 7 Niu cloud upload
Singleton mode in PHP to reduce memory consumption
Select randomly by weight [prefix and + dichotomy + random target]
20220620 interview reply
Connect with the flight book and obtain the user information according to the userid