我的JSP页面访问不到servlet,访问就报404错误,说页面不存在
hello.jsp<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
hello
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
<form method="post" action="/testServlet">
<br><br>
<input type="submit" name="Submit" value="Submit"></form>
</body>
</html>
serveltpackage jspservlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class TestServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";   public void init() throws ServletException {
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    String userName= request.getParameter("user");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>TestServlet</title></head>");
    out.println("<body bgcolor=\"#ffffff\">");
    out.println("<p>The servlet has received a GET. This is the reply.</p>");
    out.println("<i>"+userName+"</i><br>");
    out.println("</body></html>");  }    public void destroy() {
  }
}web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <servlet>
    <servlet-name>testservlet</servlet-name>
    <servlet-class>jspservlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>testservlet</servlet-name>
    <url-pattern>/testservlet</url-pattern>
  </servlet-mapping>
</web-app>

解决方案 »

  1.   

    也许你的web.xml的servlet url pattern跟页面的action地址没匹配好
      

  2.   

    把form method="post" action="/testServlet">
    改成form method="post" action="/testservlet">
    还是不行,,
      

  3.   

    地址不可能输错啦,我从JSP页面调的servlet
      

  4.   

    没改嘛
    你把web.xml贴出来看看
      

  5.   

    我用
    http://localhost:8080/JspServletapp/hello.jsp
    访问JSP页面
    可是一访问servlet页面地址就变成
    http://localhost:8080/testservlet
    怎么servlet不成servlet上下文中了?
      

  6.   

    JspServletapp/TestServlet
    或者JspServletapp/jspservlet/TestServlet
    或者其他的情况,看web.xml怎么写了不是让你贴web.xml了吗?
      

  7.   

    1. action="testservlet"是相对目录,访问
        http://localhost:8080/JspServletapp/testservlet2. action="/testservlet"是绝对目录, 访问
        http://localhost:8080/testservlet搂住写成了绝对目录所以报错,可以改成相对目录或者
    action="/JspServletapp/testservlet"
    都可以访问
      

  8.   

    <form method="post" action="<%= request.getContextPath() %>/testServlet">