当前位置:网站首页>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 .
边栏推荐
- 杰理之电话本获取【篇】
- Redis getting started complete tutorial: client management
- How to find file accessed / created just feed minutes ago
- Metaforce force meta universe fossage 2.0 smart contract system development (source code deployment)
- 从控制理论的角度谈数据分析
- 2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
- Statistics of radar data in nuscenes data set
- Redis入门完整教程:AOF持久化
- 杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
- 2022年信息安全工程师考试大纲
猜你喜欢
Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
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
Redis introduction complete tutorial: client case analysis
Redis入门完整教程:RDB持久化
C language exercises_ one
Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
Hash table and full comments
Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!
Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions
What management points should be paid attention to when implementing MES management system
随机推荐
Electrical engineering and automation
A complete tutorial for getting started with redis: RDB persistence
【Socket】①Socket技术概述
Es6中Promise的使用
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
Left path cloud recursion + dynamic planning
Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
Redis Getting started tutoriel complet: positionnement et optimisation des problèmes
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication
LeetCode 77:组合
Remember the problem analysis of oom caused by a Jap query
Kubernetes源码分析(二)----资源Resource
Oracle connection pool is not used for a long time, and the connection fails
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
wireshark安装
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
Have fun | latest progress of "spacecraft program" activities
oracle连接池长时间不使用连接失效问题
mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA
[software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose