简体   繁体   中英

I can't add a Servlet on my project

I'm trying to add a servlet on my project. But it seems not to work.

The first, I inserted the and tags in web.xml file.

And the I tried to accesss the address "/App/newrmt?~~". But the browser showed 404 error message.

I think the system don't recognize the mapping pattern I described.

Is there anything I should do to add a new servlet and a pattern before insert tags in web.xml files?

It's original web.xml code is below.

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>HttpReceiver</servlet-name>
    <servlet-class>myProject.HttpReceiver</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet> 

<servlet> 
    <servlet-name>RmtlImg</servlet-name> 
    <servlet-class>myProject.ImageServlet</servlet-class> 
    <init-param> 
        <param-name>dir</param-name> 
        <param-value>/APP/WAS/FILES/A/</param-value> 
    </init-param> 
</servlet>


<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HttpReceiver</servlet-name>
    <url-pattern>*.http</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RmtlImg</servlet-name>
    <url-pattern>/rmtlimgdown</url-pattern>
</servlet-mapping>

Then, I changed the code like below

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>HttpReceiver</servlet-name>
    <servlet-class>myProject.HttpReceiver</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet> 

<servlet> 
    <servlet-name>RmtlImg</servlet-name> 
    <servlet-class>myProject.ImageServlet</servlet-class> 
    <init-param> 
        <param-name>dir</param-name> 
        <param-value>/APP/WAS/FILES/A/</param-value> 
    </init-param> 
</servlet>
**<servlet> 
    <servlet-name>NewRmtlImg</servlet-name> 
    <servlet-class>myProject.ImageServlet</servlet-class> 
    <init-param> 
        <param-name>dir</param-name> 
        <param-value>/APP/WAS/FILES/A/</param-value> 
    </init-param> 
</servlet>**

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HttpReceiver</servlet-name>
    <url-pattern>*.http</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RmtlImg</servlet-name>
    <url-pattern>/rmtlimgdown</url-pattern>
</servlet-mapping>
**<servlet-mapping>
    <servlet-name>NewRmtlImg</servlet-name>
    <url-pattern>/newrmt</url-pattern>
</servlet-mapping>**

When I change just pattern in the tag like below, It works well. It means servlet-class itself is well coded. Just the WAS don't understand the pattern.

<servlet-mapping>
    <servlet-name>NewRmtlImg</servlet-name>
    <url-pattern>/newrmt</url-pattern>
</servlet-mapping>

===> 

<servlet-mapping>
    <servlet-name>NewRmtlImg</servlet-name>
    <url-pattern>/rmtlimgdown</url-pattern>
</servlet-mapping>

Anyboday helps me!

When I change just pattern in the tag like below, It works well. It means servlet-class itself is well coded. Just the WAS don't understand the pattern.

It might help you to understand the url-pattern

Servlet Matching Procedure

A request may match more than one servlet-mapping in a given context. The servlet container uses a straightforward matching procedure to determine the best match.

The matching procedure has four simple rules.

  • First, the container prefers an exact path match over a wildcard path match.

  • Second, the container prefers to match the longest pattern .

  • Third, the container prefers path matches over filetype matches.

  • Finally, the pattern <url-pattern>/</url-pattern> always matches any request that no other pattern matches.


Have a look at my post How does a servlets filter identify next destination is another filter or a servlet/jsp? for detailed description.

I solved this problem.

I found out that I need to change the setting file which is "httpd.conf".

The system has been configured several virtual-host with "MatchExpression".

So what I have to do is to add a single of url-pattern in the config file.

sorry for my bad English.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM