本帖最后由 冰深 于 2012-11-20 09:28 编辑
我在学习JavaWeb开发的时候尝试写Servlet的时候报404错误是怎么回事?
就是老师举的第一个例子,手写一个Servlet,我的代码:- package cn.tender;
- import java.io.*;
- import javax.servlet.*;
- public class FirstServlet extends GenericServlet
- {
- public void service(ServletRequest req,ServletResponse res)throws ServletException, java.io.IOException
- {
- OutputStream out=res.getOutputStream();
- out.write("Hello My First Servlet Demo!哈哈~".getBytes());
- }
- }
复制代码 然后编译之后成功生成了class文件,又写下web.xml配置文件- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <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>FirstServlet</servlet-name>
- <servlet-class>cn.tender.FirstServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>FirstServlet</servlet-name>
- <url-pattern>/FirstServlet</url-pattern>
- </servlet-mapping>
- </web-app>
复制代码 启动Tomcat6之后访问该页面http://192.168.1.100:8080/serverlet/FirstServlet 结果就是报错:- HTTP Status 404 - Servlet FirstServlet is not available
- type Status report
- message Servlet FirstServlet is not available
- description The requested resource is not available.
- Apache Tomcat/6.0.36
复制代码 然后我怀疑是web.xml文件写错了,于是又找到jsp来测试,将XML文件添加一个节点:- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
复制代码 访问这个jsp文件结果成功了!
jsp代码index.jsp代码:- <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>我的第一个JSP页面。</title>
- </head>
- <body>
- Hello world!
- <%
- String name = "Servlet木块测试";
- out.println(name);
- %>
- </body>
- </html>
复制代码 访问http://192.168.1.100:8080/serverlet/ ,按这个推测应该是XML文件没有写错啊,运行成功有结果!这是什么原因呢?难道是Tomcat需要一些额外的配置吗(因为教程里没看到tomcat安装,所以是自己配置的)?
|