当前位置:网站首页>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
边栏推荐
- EditorConfig
- RedHat6之smb服务访问速度慢解决办法记录
- ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
- Write your own redistemplate
- 常用编程记录——parser = argparse.ArgumentParser()
- Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
- Create enterprise mailbox account command
- Dva中在effects中获取state的值
- 2.devops-sonar安装
- Meta annotation (annotation of annotation)
猜你喜欢
图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
Latex快速入门
Configure domestic image accelerator for yarn
Graphic numpy array matrix
Pytorch——数据加载和处理
解决报错:ImportError: IProgress not found. Please update jupyter and ipywidgets
PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
Understand the current commonly used encryption technology system (symmetric, asymmetric, information abstract, digital signature, digital certificate, public key system)
MySQL realizes master-slave replication / master-slave synchronization
图解numpy数组矩阵
随机推荐
MySql基础狂神说
Latex快速入门
Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
线性规划问题中可行解,基本解和基本可行解有什么区别?
2.devops-sonar安装
多线程与高并发(3)——synchronized原理
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Graphic numpy array matrix
在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃的问题
SQL基础:初识数据库与SQL-安装与基本介绍等—阿里云天池
多个一维数组拆分合并为二维数组
Gaussian processes of sklearn
Get the value of state in effects in DVA
Pytorch学习记录(九):Pytorch中卷积神经网络
Remedy after postfix becomes a spam transit station
jdbc入门\获取数据库连接\使用PreparedStatement
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
Anaconda
Dva中在effects中获取state的值
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)