当前位置:网站首页>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 .
边栏推荐
- INS/GPS组合导航类型简介
- 如何分析粉丝兴趣?
- QT常见概念-1
- Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
- Simple bubble sort
- How to write test cases for test coupons?
- 硬件之OC、OD、推挽解释
- Redis getting started complete tutorial: replication configuration
- The panel floating with the mouse in unity can adapt to the size of text content
- PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
猜你喜欢

Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions

PSINS中19维组合导航模块sinsgps详解(时间同步部分)

uniapp的表单验证

IDEA重启后无法创建Servlet文件的解决方案

Cloud Mail . NET Edition

Redis入门完整教程:复制拓扑

HAVE FUN | “飞船计划”活动最新进展

Left value, right value

Redis getting started complete tutorial: client management

Number theory --- fast power, fast power inverse element
随机推荐
Construction of knowledge map of mall commodities
Apifox, is your API interface document rolled up like this?
“零售为王”下的家电产业:什么是行业共识?
ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
Redis getting started complete tutorial: client management
Django数据库(SQlite)基本入门使用教程
Cryptography series: detailed explanation of online certificate status protocol OCSP
Redis入门完整教程:客户端管理
What are the characteristics of the operation and maintenance management system
MySQL is an optimization artifact to improve the efficiency of massive data query
How to design interface test cases? Teach you a few tips to draft easily
Convert widerperson dataset to Yolo format
Code line breaking problem of untiy text box
杰理之电话本获取【篇】
Use of tensorboard
Digital scrolling increases effect
Oracle connection pool is not used for a long time, and the connection fails
Examples of how to use dates in Oracle
Leetcode 77: combination