当前位置:网站首页>Initial deployment of Servlet
Initial deployment of Servlet
2022-06-11 10:03:00 【Fly upward】
Catalog
3.1 install Smart Tomcat plug-in unit
3.2 To configure Smart Tomcat plug-in unit
4.5 appear " Can't access this website "
1. servlet Main work
servlet Is a technology to realize dynamic pages , It's a group. Tomcat For programmers API, Help group programmers develop a simple and efficient web app. Without concern Socket,HTTP Form of agreement , Multithreading concurrency and other technical details , To reduce the web app The development threshold of , Improved development efficiency rate .
2. first Servlet Program
2.1 Create project

2) Select the directory where the project will be stored

3) Project creation completed

2.2 Introduce dependencies

<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>When it was first copied , It is marked in red , That's because of our IDEA Not downloaded yet .jar package . This requires us to manually click IDEA On the top right of Maven Refresh icon for , Just wait for the download .

2.3 Create directory

however , These directories are not enough , We need to manually add some necessary directories .
1)webapp


<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>, If in IDEA in , The following code appears in red , Do the following .

2.4 Write code
stay java Create a class in the directory HelloServlet, The code is as follows
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// This line of code needs to be commented out , Cannot reference a parent class's doGet
//super.doGet(req, resp);
// This is to let the server print in its own console
System.out.println("hello world");
// You can also print on the page hello world character string , Put it in http Responsive body in . The browser will put body The content of
// Show on page
// If nothing is set in the response object , This is the time when Blank page
resp.getWriter().write("hello world" + System.currentTimeMillis());
}
}In the above code :
a) The created class needs to inherit from HttpServletb) This class needs to use @WebServlet The annotation is associated with the previous HTTP The path ofc) This class needs to implement doXXX Method
2.5 packaged applications
Current code , It can't run alone ( No, main Method ) Current code , It can't run alone ( No, Main Method )
Need to put the current code , pack , And then deploy to Tomcat. On , from Tomcat To make the call , Need to put the current code , pack , And then deploy to Tomcat. On , from Tomcat To make the call .
Before packing , First in pom.xml Add a <packaging>war</packaging> , Because we packed war package , If not , Will default to jar package .
jar and war All are java The compressed package format used by the publisher .war It's for Tomcat Specialized in . There will not only be some .class, You can also include configuration files , And the second 3 Tripartite jar , also html, Css, js....
Add location as follows


When the console appears BUILD SUCCESS, And the directory list appears servlet_hello.war It indicates that the packaging is successful

2.6 The deployment process

After the open hold war Package copy to Tomcat Of webapps Under the table of contents . Then start Tomcat , Tomcat Will automatically put war Packet decompression .

2.7 Verification procedure
Enter the URL of the direct deployment through the browser , You can get a response http://127.0.0.1:8080/servlet_hello/hello, as follows

Be careful : URL Medium PATH Divided into two parts , among servlet_hello by Context Path, hello by Servlet Path

2.8 summary
These seven steps , This is for a new project , After the project is created , Subsequent modification code , The first three steps need not be repeated . Directly from 4—7 Just operate .
3. Easier deployment
3.1 install Smart Tomcat plug-in unit

3.2 To configure Smart Tomcat plug-in unit

Click the green triangle , IDEA Will automatically compile , Deploy

start-up Tomcat The process of , The font is red , It is not an error that makes it red .

4. Common error problems
4.1 appear 404

2) Write less. Servlet Path namely hello

3)Servlet Path It's written and URL No match

Error is as follows

4)web.xml Write the wrong
4.2 appear 405
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
// Be annotated out , So it didn't come true doGet Method
//@Override
//protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { }
}
4.3 appear 500
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServlet Requestreq, HttpServlet Responseresp) throws Servlet Exception, IOException {
String s = null;
resp.getWriter().write(s.length());
}
}
4.4 appear " Blank page "
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// This line of code needs to be commented out , Cannot reference a parent class's doGet
//super.doGet(req, resp);
// This is to let the server print in its own console
System.out.println("hello world");
// You can also print on the page hello world character string , Put it in http Responsive body in . The browser will put body The content of
// Show on page
// If nothing is set in the response object , This is the time when Blank page
//resp.getWriter().write("hello world" + System.currentTimeMillis());
}
}

4.5 appear " Can't access this website "
// hold
@WebServlet("/hello")
// It has been written. , Just missing one /
@WebServlet("hello")
5. summary
边栏推荐
- What are the differences between Parker pilot solenoid valve and direct acting solenoid valve?
- [torch]: parallel training and can dynamically set the batch size of the first GPU
- Product list display
- Cisp-pte XSS Foundation
- 转载:LinearLayout+Fragment实现下导航栏效果
- Finish C language
- FPGA基础架构【参考ug998】
- 图表背后的秘密 | 技术指标讲解:唐奇安通道
- MySQL basic learning notes 03
- 【Objective-C】结构体和类的区别
猜你喜欢

Function and function of wandfluh proportional valve

ESP8266_ SNTP(Simple Network Time Protocol)

UGUI

「INS-30131」 安装程序验证所需的初始设置失败

Leetcode brushing questions - hand tearing binary tree

卸载grid时运行脚本报错Can‘t locate Env.pm in @INC

Reconstruction des composants Oracle XDB

MySQL basic learning notes 03

ugui图片墙

RAC expdp export error: ora-31693, ora-31617, ora-19505
随机推荐
Simple recommendation based on Euclidean distance
图表背后的秘密 | 技术指标讲解:唐奇安通道
Interview review - closure
BeanFactory 与FactoryBean的区别
ESP8266_GET请求天气预报、json解析
How much do you know about software compatibility testing? How to select a software compatibility testing organization?
rpc的正确打开方式|读懂Go原生net/rpc包
ZigBee模块通信协议的树形拓扑组网结构
Ora-00059 exceeds DB_ Files limit
ESP8266_ SNTP(Simple Network Time Protocol)
[high concurrency] the interviewer of ant financial asked me about thread pool!!
记录一下昨天的尴尬
完结C语言
WordPress网站备份
科技云报道:Web3.0浪潮下的隐私计算
ESP8266_ Access to the core suite of Baidu Internet of things and use mqtt protocol for communication
Q1 revenue exceeded expectations. Why did man Bang win headwind growth?
浅谈wandfluh比例阀的功能与作用
General idea of interface tuning
宝塔面板备份与恢复数据