当前位置:网站首页>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 .
边栏推荐
- Software testing -- common assertions of JMeter interface testing
- Unity custom webgl packaging template
- Oracle connection pool is not used for a long time, and the connection fails
- Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
- How-PIL-to-Tensor
- Django database (SQLite) basic introductory tutorial
- Unity使用MaskableGraphic画一条带箭头的线
- Unity uses maskablegraphic to draw a line with an arrow
- Redis getting started complete tutorial: replication configuration
- Code line breaking problem of untiy text box
猜你喜欢
Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
Redis Getting started tutoriel complet: positionnement et optimisation des problèmes
What are the applications and benefits of MES management system
巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...
Es6中Promise的使用
Cloud Mail . NET Edition
Django database (SQLite) basic introductory tutorial
leetcode-02(链表题)
随机推荐
Es6中Promise的使用
Leetcode 77: combination
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
LeetCode 77:组合
杰理之RTC 时钟开发【篇】
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
Oauth2协议中如何对accessToken进行校验
硬件之OC、OD、推挽解释
Cloud Mail . NET Edition
wireshark安装
Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions
你知道电子招标最突出的5大好处有哪些吗?
Django database (SQLite) basic introductory tutorial
QT常见概念-1
Summary of research status of inertial navigation calibration at home and abroad (abridged version)
Contribution of Writing Series
The panel floating with the mouse in unity can adapt to the size of text content
Redis getting started complete tutorial: replication configuration
leetcode-02(链表题)
换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!