拜托高手来看看,第一次写JSP+Servlet的程序,也不知道哪里出错的,代码都贴出来。
**************************下面是web.xml文件**********************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
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">  <filter>
<filter-name>EncodeFilter</filter-name>
<filter-class>org.cumt.Student.Filter.EncodeFilter</filter-class>
<init-param>
<param-name>Encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
  </filter>
  <filter-mapping>
<filter-name>EncodeFilter</filter-name>
<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet>
    <servlet-name>NextServlet</servlet-name>
    <servlet-class>org.cumt.Student.Control.NextServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>NextServlet</servlet-name>
    <url-pattern>/next</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
    <servlet-name>NextServlet</servlet-name>
    <url-pattern>/show.jsp</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>show.jsp</welcome-file>
  </welcome-file-list>
</web-app>
*************************下面是NextServlet的代码**************************
package org.cumt.Student.Control;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.cumt.Student.Model.Action.AddAction;
import org.cumt.Student.Model.Action.EditAction;
import org.cumt.Student.Model.Action.IAction;
import org.cumt.Student.Model.Action.SaveAction;
import org.cumt.Student.Model.Action.ShowAction;@SuppressWarnings("serial")
public class NextServlet extends HttpServlet {
public void init() throws ServletException {
this.getServletContext().setAttribute("show", new ShowAction("/show.jsp"));
this.getServletContext().setAttribute("insert", new AddAction("/InsertData.htm"));
this.getServletContext().setAttribute("add", new AddAction("/next?cmd=show"));
this.getServletContext().setAttribute("edit",new EditAction("/EditData.jsp"));
this.getServletContext().setAttribute("save", new SaveAction("/next?cmd=show"));
} public void destroy() {
this.getServletContext().removeAttribute("show");
this.getServletContext().removeAttribute("insert");
this.getServletContext().removeAttribute("add");
this.getServletContext().removeAttribute("edit");
this.getServletContext().removeAttribute("save");
super.destroy(); 
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd=request.getParameter("cmd");
String next=null;
if(cmd!=null){
IAction action=(IAction) this.getServletContext().getAttribute(cmd);
next=action.execute(request);
}else{
next="/error.jsp";
}
this.getServletContext().getRequestDispatcher(next).forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}

解决方案 »

  1.   

    把<url-pattern>中的内容都改成*试试
      

  2.   

    servlet mapping怎么两个?应该一个name,一个mapping
      <servlet>
        <servlet-name>NextServlet </servlet-name>
        <servlet-class>org.cumt.Student.Control.NextServlet </servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>NextServlet </servlet-name>
        <url-pattern>/next </url-pattern>
      </servlet-mapping><!--  <servlet-mapping>
        <servlet-name>NextServlet </servlet-name>
        <url-pattern>/show.jsp </url-pattern>
      </servlet-mapping>  去掉这个-->访问的时候这样:
    http://localhost:8080/yourapp/next
      
      

  3.   

    原来不是Servlet出错,是Filter的问题,我把web.xml中的Filter相并的内容删除后就没事了,但是也找不到Filter哪里错的,高手们帮我看看。
    **************************下面是Filter的Java代码****************************
    package org.cumt.Student.Filter;import java.io.IOException;import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;public class EncodeFilter implements Filter{
    protected FilterConfig config=null;
    protected String encodeStr;


    public void init(FilterConfig filterConfig) throws ServletException {
    this.config=filterConfig;
    this.encodeStr=config.getInitParameter("Encode");
    }

    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {

    if(encodeStr!=null){
    request.setCharacterEncoding(encodeStr);
    response.setContentType("text/html; charset="+encodeStr);
    } chain.doFilter(request, response);
    }

    public void destroy() {
    this.encodeStr=null;
    this.config=null;
    }
    }
      

  4.   

    我用的过滤器,你可以试试
    package com.controller;import java.io.IOException;
    import javax.servlet.*;public class SetCharacterEncodingFilter implements Filter { protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true; // destroy方法
    public void destroy() {
    this.encoding = null;
    this.filterConfig = null; } // 选择设置使用的字符编码
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null)
    request.setCharacterEncoding(encoding);
    } // Pass control on to the next filter
    chain.doFilter(request, response); } // 将这个filter放置在服务器中
    public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
    String value = filterConfig.getInitParameter("ignore");
    if (value == null)
    this.ignore = true;
    else if (value.equalsIgnoreCase("true"))
    this.ignore = true;
    else if (value.equalsIgnoreCase("yes"))
    this.ignore = true;
    else
    this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }
    }