HelloWorldServlet HTTP500问题 30
出现HTTPStatus500-WrappercannotfindservletclassHelloWorldServletoraclassitdependson这个错误...
出现HTTP Status 500 - Wrapper cannot find servlet class HelloWorldServlet or a class
it depends on
这个错误
初学者想写一个HelloWorldServlet 但我感觉没什么地方出错,请大神帮我看看
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>myfirstservlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myfirstservlet</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>");
out.flush();
out.close();
}
private static final long serialVersionUID = 1L;
}
然后我在IE上输入http://127.0.0.1:8080/my/Hello就出现上面那个错误。。 展开
it depends on
这个错误
初学者想写一个HelloWorldServlet 但我感觉没什么地方出错,请大神帮我看看
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>myfirstservlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myfirstservlet</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>");
out.flush();
out.close();
}
private static final long serialVersionUID = 1L;
}
然后我在IE上输入http://127.0.0.1:8080/my/Hello就出现上面那个错误。。 展开
2个回答
展开全部
1、初学者请在自己建立的servler 处理文件上点击 run as -> service run (中文 启动->启动服务器)
这样的好处是 你不用自己猜测地址输入,刚学servlet 地址和正常地址不一样而且你也不会找。
2、你的servlet 是使用doGet 方法处理页面请求的,建议改成 service ,然后在里面 直接写 system.out.printfln("访问成功");(这句是控制台输出 你要打开connect )这句测试通过后再弄显示页面之类的。
3、doGet是处理getHTTP请求用的,你的浏览器弄不好是提交了post,所以使用service 。
4、我这里的测试完全好使 完整代码 给你贴出来。
有图有真相
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>myServletTest</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>
<description></description>
<display-name>Test</display-name>
<servlet-name>Test</servlet-name>
<servlet-class>My.Test</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>jjjjj</display-name>
<servlet-name>jjjjj</servlet-name>
<servlet-class>My.jjjjj</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jjjjj</servlet-name>
<url-pattern>/jjjjj</url-pattern>
</servlet-mapping>
</web-app>
package My;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class jjjjj
*/
public class jjjjj extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>");
out.flush();
out.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询