今天用MyEclipse2015写了一个JavaWeb的一个接口,可是运行起来总是出现404.Serverlet代码如下:
package com.upload;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Upload extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
在Web.xml配置接口:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebLoad</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>upload123</servlet-name>
<servlet-class>com.upload.TestUrl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload123</servlet-name>
<url-pattern>/upload123</url-pattern>
</servlet-mapping>
</web-app>
运行的时候总是出现500,说类没没找到。反复检查了很多遍配置都没发现有错的,纠结~~~后来发现项目的WEB-INF下的Classes中没有生成.class文件。恍然大悟,我勒个去,原来是之前在项目中跑Android工程出错了,把project->Build Automatically去掉了,导致项目没有编译java类。把这个勾选上就好了。
另外:如果不想配置Web.xml,可以在Serverlet中添加Web容器,如下:
package com.upload;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/load12345")
public class Upload extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}