现在把我的源代码,目录结构,web.xml都列出来,看看是什么问题。
目录结构:web页面:input.htm,buy.jsp,sell.jsp,change.jsp,cancel.jsp 都处于%TOMCAT_HOME%/mcv/ 目录下。
servlet:Controller.class 处于 %TOMCAT_HOME%/mcv/WEB-INF/classes/ 下
web.xml配置:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>controller</servlet-name>
<servlet-class>Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
_________________________________________________________________________
input.htm内容:
<body>
<form name=input method=post action=controller>
<input type=text name=id>
<input type=hidden name=buy value=buy.jsp>
<input type=submit name=action value=buy>
<input type=hidden name=sell value=sell.jsp>
<input type=submit name=action value=sell>
<input type=hidden name=change value=change.jsp>
<input type=submit name=action value=change>
<input type=hidden name=cancel value=cancel.jsp>
<input type=submit name=action value=cancel>
</form>
</body>
(其实实现的目的是点击那个按钮,就会自动转到相应的页面,工作流由Controller.class统一控制。)
_________________________________________________________________________
Controller.java 内容:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Controller extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException) {
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException) {
RequestDispatcher rd=null;
String action=request.getParameter("action");
String page=request.getParameter(action);
rd=request.getRequestDispatcher(page);
if(rd!=null) {
rd.forward(request,response);
}
}
}
_________________________________________________________________________
请帮我看看我的目录结构,web.xml配置,action设置哪里出现问题,因为提交后出现
The requested resource (/mcv/controller) is not available.
错误信息。
_________________________________________________________________________
解答:
<url-pattern>*.jsp</url-pattern>
这句改为:
<url-pattern>/controller</url-pattern>
你要向servlet提交请求,url-pattern配置了你以什么样的地址能访问到这个servlet,和你想要跳转的jsp页面无关,应该在程序中控制
<url-pattern>的内容是你JSP页面中标签<FORM>中ACTION所填的内容!
通过它就可以找到 <servlet-name>controller</servlet-name>