tomcat HTTP Status 404

來源:互聯網
上載者:User
今天要用到Servlet所以就tomcat了一把 ,^_^,但是在寫了一個簡單的小例子之後啟動tomcat後突然報了 HTTP Status 404這個鬼錯誤 .其實404這種錯誤的原因的種類比較多,在這裡我僅將自己遇到的問題在這裡給大家說一下.
首先我寫了一個servlet,代碼如下:

package com;

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;

/*
 * Create Date:2008-3-31 下午02:36:44
 *
 * Author: chunwei wang
 *
 * Version: V1.0
 *
 * Description:對進行修改的功能進行描述
 *
 * 
 */

public class MyServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        out.println("Hello World");
        response.setContentType("text/html;charset=gb2312");
        String name=request.getParameter("name");
        String age=request.getParameter("age");
        String sex=request.getParameter("sex");
        out.print(name);
        out.print(age);
        out.print(sex);
        System.out.println(name);
        System.out.println(age);
        System.out.println(sex);
    }

    @Override
    protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(arg0, arg1);
    }

}
那個表單頁面代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    </head>

    <body>
        <form name="myForm" action="test2" method="post">
            <h2 align="center">
                使用者資訊
            </h2>
            姓名:
            <input type="text" name="name">
            <br>
            年齡:
            <input type="text" name="age">
            <br>
            性別:
            <input type="text" name="sex">
            <br>
            <input type="submit" value="提交">
            <input type="reset" value="清除">
        </form>
    </body>
</html>

web.xml檔案配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>com.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/test2</url-pattern>
    </servlet-mapping>
</web-app>
其實我的問題主要是出在    <form name="myForm" action="test2" method="post">這個地方,因為我把action的test2前面多加了一個/,所以就報了那種鬼錯誤,我記得以前自己用的時候也加/了 ,當時就沒有那種錯誤,但這次就死活不行,我倒.不管怎麼樣這次就這樣該就OK了.還有我要提示一點就是以後大家在用到Struts的時候表單的FormAction好像要加/.如果到時候在出現類似的錯誤大家注意一下就可以了.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.