当前位置:网站首页>In depth source code analysis servlet first program
In depth source code analysis servlet first program
2022-04-23 05:56:00 【ShuangTwo】
First of all give Servlet Program , Then let it be realized . Write a simple Servlet Program , Just write two files . One is Servlet Of Java Program , One is configuration web.xml.
Servlet It is inherited from HttpServlet, Need to rewrite doGet() and doPost() Two sides .
HelloServlet
package com.java.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8"); // Set the response type to HTML file
PrintWriter out = resp.getWriter(); // Get the response output stream
out.println("<html>");
out.println("<title>God of Night! </title>");
out.println("<body>");
out.println("<h1>Without a bit of cold, how can the plum blossoms smell so sweet.</h1>");
out.println("</body>");
out.println("</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
Analysis of the code :
First response call setContentType() Method , Set the type of response content .
This setContentType The way is by response Called ,response yes HttpServletResponse Instance object of , Click in HttpServletResponse Can see ,HttpServletResponse Inherited ServletResponse

ServletResponse Interface defined setContentType Method , And later to say getWriter Method .

see setContentType Implementation class of , Find out ServletResponseWrapper Realized ServletResponse All the methods in the interface .

The whole flow chart is as follows :

summary :
ServletResponse Defines all methods of response ,ServletResponseWrapper and HttpServletResponseWrapper Jointly implement these methods , then HttpServletResponse Inherit ServletResponse, all HttpServletResponse You can use it directly ServletResponse All the ways .
About HttpServletRequest Flow chart and source code analysis HttpServletResponse cut from the same cloth .
Another one resp.getWriter(), This method is used to get the response output stream , Send the information data to the front page , If you pass System.out.println(); Information can only be output to the background .
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.java.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/geeks</url-pattern>
</servlet-mapping>
</web-app>
web.xml register servlet, take servlet After the class and package are registered , adopt url-pattern Set up URL Jump to what we wrote servlet Program .
summary :
- To write Servlet Program , Define the content of the page
- register Servlet Program , Definition URL Access address
版权声明
本文为[ShuangTwo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541368040.html
边栏推荐
- JDBC操作事务
- mysql如何将存储的秒转换为日期
- Multithreading and high concurrency (3) -- synchronized principle
- Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
- PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
- 深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
- 数字图像处理基础(冈萨雷斯)二:灰度变换与空间滤波
- Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
- ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
- Remedy after postfix becomes a spam transit station
猜你喜欢

Pytorch learning record (7): skills in processing data and training models

Ora: 28547 connection to server failed probable Oracle net admin error

JDBC连接数据库

Pytorch学习记录(十):数据预处理+Batch Normalization批处理(BN)

Pyqy5 learning (III): qlineedit + qtextedit

编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察

Ptorch learning record (XIII): recurrent neural network
![去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention](/img/4e/1a51636853d11544e6f5c37a588730.png)
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention

容器

JVM series (3) -- memory allocation and recycling strategy
随机推荐
Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
金蝶EAS“总账”系统召唤“反过账”按钮
Remedy after postfix becomes a spam transit station
容器
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
Kingdee EAS "general ledger" system calls "de posting" button
The official website of UMI yarn create @ umijs / UMI app reports an error: the syntax of file name, directory name or volume label is incorrect
PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
Postfix变成垃圾邮件中转站后的补救
给yarn配置国内镜像加速器
异常的处理:抓抛模型
mysql-触发器、存储过程、存储函数
Understand the current commonly used encryption technology system (symmetric, asymmetric, information abstract, digital signature, digital certificate, public key system)
Pytoch -- data loading and processing
K/3 WISE系统考勤客户端日期只能选到2019年问题
多线程与高并发(3)——synchronized原理
filebrowser实现私有网盘
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
开发环境 EAS登录 license 许可修改
Latex quick start