#2
林月儿2018-10-06 21:20
|
找了一个简单的代码测试一下
程序代码:
package Hello;
// 导入必需的 java 库
import *;
import javax.servlet.*;
import javax.servlet.http.*;
// 扩展 HttpServlet 类
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException
{
// 执行必需的初始化
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// 设置响应内容类型
response.setContentType("text/html");
// 实际的逻辑是在这里
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// 什么也不做
}
}
// 导入必需的 java 库
import *;
import javax.servlet.*;
import javax.servlet.http.*;
// 扩展 HttpServlet 类
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException
{
// 执行必需的初始化
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// 设置响应内容类型
response.setContentType("text/html");
// 实际的逻辑是在这里
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// 什么也不做
}
}
web.xml配置
程序代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>Hello.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/Hello/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>Hello.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/Hello/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
嗯,tomcat服务器已经设置好了classpath环境变量:Servlet-api.jar。
HTTP Status 404 – Not Found
Type Status Report
Message /WebDemo/Hello/HelloWorld
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.12
在eclipse和浏览器中进的8080都是这个显示
只有本站会员才能查看附件,请 登录
网上搜了一些方法,似乎都不顶用....
有大佬帮忙支支招吗??