1、先創建一個Java_web項目
如果你沒有下載過Tomcat服務器,不會配置,建議看一下我得這一篇博客:https://www.cnblogs.com/kongbursi-2292702937/p/11746773.html
我得項目名稱為day02
上面信息填完之后點next,別點finish
再點next
之后finish就可以,創建完之后如下:
之后創建一個Java類hello_world.java,繼承於GenericServlet
文件內容如下:
import java.io.IOException; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class hello_world extends GenericServlet { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub res.getOutputStream().write("hello_world".getBytes()); } }
之后配置web.xml文件,以使得外界可以訪問到這個文件
這里給出文件內容:不需要全部一樣,只要web.xml文件紅色框內一樣就可以
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>day02</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>hello_world</servlet-name> <!-- 就是一個名字,沒實際意義 --> <servlet-class>day02.hello_world</servlet-class> <!-- 給出Java文件位置 --> </servlet> <servlet-mapping> <servlet-name>hello_world</servlet-name> <url-pattern>/hello_world</url-pattern> <!-- 你要映射的地址 --> </servlet-mapping> </web-app>
之后創建一個Tomcat服務器
點了之后默認就運行了。可以右鍵點擊停止或啟動:
啟動Tomcat服務器之后,打開網頁訪問127.0.0.1:8080/day02/hello_world
day02就是:
hello_world就是:
最后記得要把day02發布到服務器上面
完結!!