我们在使用Eclipse+Servlet+Jsp做简单的例子通过超链接调用 Servlet报:
Caused by: java.lang.IllegalArgumentException: The servlets named [AcceptLink] and [com.dc.AcceptLink] are both mapped to the url-pattern [/AcceptLink] which is not permitted
at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:293)
at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2458)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2133)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2094)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2086)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2086)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1303)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:888)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:388)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
... 10 more
//--------文件名:link.jsp--------------------
<%@ page language="java" import="java.util.*" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Servlet 接收链接传递参数示例</title>
</head>
<body>
<font size="2">
单击下面的链接:<br>
<a href="AcceptLink?name=Bill">调用 Servlet,并传递参数</a>
</font>
</body>
</html>
AcceptLink 这个 Servlet 的具体代码如下。
//--------文件名:AcceptLink.java--------------------
package com.dc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AcceptLink
*/
@WebServlet("/AcceptLink" )
public class AcceptLink extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
out.println("<font size='2'>");
out.println("链接传递过来的参数为:<br>");
out.println("参数名称:name<br>");
out.println("参数值:" + name + "<br>");
out.print("</font>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlet 的配置信息如下:
<servlet>
<servlet-name>AcceptLink</servlet-name>
<servlet-class>com.dc.AcceptLink</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AcceptLink</servlet-name>
<url-pattern>/AcceptLink</url-pattern>
</servlet-mapping>
解决方法及原因:
@WebServlet告知容器,HelloServlet这个Servlet的名称是Hello,这是由name属性指定的,而如果客户端请求的URL是/hello.view,则由具Hello名称的Servlet来处理,这是由urlPatterns属性来指定的。在Java EE相关应用程序中使用标注时,可以记得的是,没有设置的属性通常会有默认值。