废话不多说,servlet代码
SDK lib依赖: D:\apache-tomcat-9.0.13-windows-x64\apache-tomcat-9.0.13\lib
import com.sun.net.httpserver.HttpsServer;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HiWorld extends HttpServlet{
public String msg;
public void init() throws ServletException{
msg = "Hello World, Servlet";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + msg + "</h1>");
}
public void destory(){
}
}
appache 配置:
1 存放lass文件
D:\apache-tomcat-9.0.13-windows-x64\apache-tomcat-9.0.13\webapps\ROOT\WEB-INF\classes
2 配置web.xml文件
<servlet>
<servlet-name>HiWorld</servlet-name>
<servlet-class>HiWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HiWorld</servlet-name>
<url-pattern>/HiWorld</url-pattern>
</servlet-mapping>
url-pattern :网址输入后缀
如果类打包了:
package com.MyHelloWorld;
目录结构:
web xml文件
如果class 打理好了jar
则将jar放入到/lib目录下
D:\apache-tomcat-9.0.13-windows-x64\apache-tomcat-9.0.13\webapps\ROOT\WEB-INF\lib
而web.xml还是按照打包的目录编写
<servlet>
<servlet-name>MyHelloWorld</servlet-name>
<servlet-class>com.MyHelloWorld.MyHelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHelloWorld</servlet-name>
<url-pattern>/Servlets/MyHelloWorld</url-pattern>
</servlet-mapping>