当前位置:网站首页>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 请求响应的具体交互过程.
边栏推荐
- visual studio2019链接opencv
- 1251- Client does not support authentication protocol MySql错误解决方案
- 词法陷阱(C)
- Foreach method of array in JS
- Ramda rejects objects with null and empty object values in the data
- Baidu search stability analysis story
- JS array de duplication
- Swagger document generated by node project API in vscode
- 20220620 面试复盘
- Circular exercises of JS
猜你喜欢

The amount is verified, and two zeros are spliced by integers during echo

Laravel excel export

First acquaintance with CANopen

Baidu search stability analysis story

GNSS receiver technology and application review

百度搜索稳定性问题分析的故事

3+1保障:高可用系统稳定性是如何炼成的?

Render values to corresponding text

Common colors for drawing

【AI助力科研】loss曲线傻瓜式绘制
随机推荐
JS array de duplication
3+1 guarantee: how is the stability of the highly available system refined?
MySQL and excel tables importing database data (Excel for MySQL)
Jenkins Pipeline使用
JS picture switching (simple and practical)
20220620 面试复盘
CUDA error: unspecified launch failure
5 kinds of viewer for browser
Micro engine remote attachment 7 Niu cloud upload
Common software numerical filtering methods (I) have been applied
Singleton mode in PHP to reduce memory consumption
[Visio]平行四边形在Word中模糊问题解决
(6) Pyqt5--- > window jump (registration login function)
Oracle trigger error report table or view does not exist
Elemntui's select+tree implements the search function
二叉树之_哈夫曼树_哈弗曼编码
JS function exercises
AI assisted paper drawing of PPT drawing
mysql FIND_ IN_ Set function
Laravel task scheduling