当前位置:网站首页>Don't you know the relationship between JSP and servlet?
Don't you know the relationship between JSP and servlet?
2022-07-07 03:07:00 【60 year old procedural ape】
List of articles
What is? Servlet
1、servlet Is running on the Web Small in server Java Program .servlet Usually by HTTP( Hypertext transfer protocol ) Receive and respond come from Web Client requests .
2、Servlet yes JavaEE One of the norms , A specification is an interface .
Servlet Sample code for the program :
public class HelloServlet implements Servlet {
/**
* service Methods are designed to handle requests and responses
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws
ServletException, IOException {
System.out.println("Hello Servlet Was interviewed ");
}
}
web.xml Configuration in :
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- servlet Label to Tomcat To configure Servlet Program -->
<servlet>
<!--servlet-name label Servlet The program has an alias ( It's usually a class name ) -->
<servlet-name>HelloServlet</servlet-name>
<!--servlet-class yes Servlet The full class name of the program -->
<servlet-class>com.atguigu.servlet.HelloServlet</servlet-class>
</servlet>
<!--servlet-mapping Label to servlet Program configuration access address -->
<servlet-mapping>
<!--servlet-name The tag is used to tell the server , Which address do I currently configure Servlet Program usage -->
<servlet-name>HelloServlet</servlet-name>
<!--url-pattern Tag configuration access address <br/> / When the server parses the slash , The address is :http://ip:port/ Project path <br/> /hello The address is :http://ip:port/ Project path /hello <br/> -->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app
Usually in the actual project development , It's all about inheritance HttpServlet class The way to achieve Servlet Program .
Servlet Class code :
public class HelloServlet2 extends HttpServlet {
/**
* doGet() stay get Call when requested
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
System.out.println("HelloServlet2 Of doGet Method ");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
System.out.println("HelloServlet2 Of doPost Method ");
}
}
web.xml Configuration in :
Just configure as above
What is? Jsp
jsp All change is java server pages.Java The server page of .
jsp Its main function is to replace Servlet Program returns html Page data .
because Servlet Program returns html Page data is a very complicated thing . Development costs and maintenance costs are very high .
Servlet Comes back html Code of page data :
public class PringHtml extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
// Through the return flow of the response html The page data
resp.setContentType("text/html; charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write("<!DOCTYPE html>\r\n");
writer.write(" <html lang=\"en\">\r\n");
writer.write(" <head>\r\n");
writer.write(" <meta charset=\"UTF-8\">\r\n");
writer.write(" <title>Title</title>\r\n");
writer.write(" </head>\r\n");
writer.write(" <body>\r\n");
writer.write(" This is a html The page data \r\n");
writer.write(" </body>\r\n");
writer.write("</html>\r\n");
writer.write("\r\n");
}
}
jsp Send back a simple html Page code :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
This is a html The page data
</body>
</html>
jsp and Servlet The connection of
jsp A page is essentially a Servlet Program .
When we first visited jsp On the page .Tomcat The server will help us jsp Page translation becomes a java Source file . And compile it into by .class Bytecode program . We turn on Translated java It's not difficult to find the content of the source file is :

We track the source code and find ,HttpJspBase class . It directly inherits HttpServlet class . in other words .jsp Translated java class , It indirectly inherited HttpServlet class . in other words , The translation is a Servlet Program .

summary : Through translation java Source code, we can get the result :jsp Namely Servlet Program .
边栏推荐
- HAVE FUN | “飞船计划”活动最新进展
- Redis getting started complete tutorial: common exceptions on the client
- 哈希表及完整注释
- Use of promise in ES6
- Dotconnect for DB2 Data Provider
- [secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
- 杰理之关于 DAC 输出功率问题【篇】
- 知识图谱构建全流程
- Software testing -- common assertions of JMeter interface testing
- 你知道电子招标最突出的5大好处有哪些吗?
猜你喜欢

Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush

硬件之OC、OD、推挽解释

Es6中Promise的使用

Cryptography series: detailed explanation of online certificate status protocol OCSP

ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法

mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA

Uniapp adaptation problem

How to design interface test cases? Teach you a few tips to draft easily

Redis入门完整教程:复制配置

Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
随机推荐
Unity webgl adaptive web page size
Have fun | latest progress of "spacecraft program" activities
Dotconnect for DB2 Data Provider
AWS learning notes (I)
【2022国赛模拟】多边形——计算几何、二分答案、倍增
简单冒泡排序
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
MMDetection3D加载毫米波雷达数据
2022年信息安全工程师考试大纲
A complete tutorial for getting started with redis: RDB persistence
Five reasons for clothing enterprises to deploy MES management system
Read fast RCNN in one article
Use of tensorboard
C language exercises_ one
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication
A complete tutorial for getting started with redis: problem location and optimization
Error in fasterxml tostringserializerbase
Redis入门完整教程:复制配置
MySQL - common functions - string functions
Derivative, partial derivative, directional derivative