Rectangle 27 51

You need to put the JSP file in /index.jsp instead of in /WEB-INF/jsp/index.jsp. This way the whole servlet is superflous by the way.

WebContent
 |-- META-INF
 |-- WEB-INF
 |    `-- web.xml
 `-- index.jsp

If you're absolutely positive that you need to invoke a servlet this strange way, then you should map it on an URL pattern of /index.jsp instead of /index. You only need to change it to get the request dispatcher from request instead of from config and get rid of the whole init() method.

In case you actually intend to have a "home page servlet" (and thus not a welcome file which has an entirely different purpose; namely the default file which sould be served when a folder is being requested, which is thus not specifically the root folder), then you should be mapping the servlet on the empty string URL pattern.

<servlet-mapping>
    <servlet-name>index</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>

You have it in /WEB-INF/jsp/index.jsp folder. You do not have it in root folder as /index.jsp. The anwer is to put it in the root folder, the same level as /WEB-INF folder.

I tried to change it but still the same problem. Resource is not available

jsp - How to configure welcome file list in web.xml - Stack Overflow

jsp servlets web.xml welcome-file
Rectangle 27 61

url-pattern is used in web.xml to map your servlet to specific URL. Please see below xml code, similar code you may find in your web.xml configuration file.

<servlet>
    <servlet-name>AddPhotoServlet</servlet-name>  //servlet name
    <servlet-class>upload.AddPhotoServlet</servlet-class>  //servlet class
</servlet>
 <servlet-mapping>
    <servlet-name>AddPhotoServlet</servlet-name>   //servlet name
    <url-pattern>/AddPhotoServlet</url-pattern>  //how it should appear
</servlet-mapping>

If you change url-pattern of AddPhotoServlet from /AddPhotoServlet to /MyUrl. Then, AddPhotoServlet servlet can be accessible by using /MyUrl. Good for the security reason, where you want to hide your actual page URL.

url-pattern
  • A string beginning with a '/' character and ending with a '/*' suffix is used for path mapping.
  • A string beginning with a '*.' prefix is used as an extension mapping.
  • A string containing only the '/' character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.

Just to add, if OP is using a Web Application servlet that supports Servlet 3.0, then there's no need to configure the servlet in the web.xml. There's a good explanation in our Servlets Wiki

/AddPhotoServlet
http://AddPhotoServlet.company.com/
http://company.com/AddPhotoServlet

java - What is url-pattern in web.xml and how to configure servlet - S...

java servlets web.xml url-pattern servlet-mapping
Rectangle 27 23

Servlet-mapping has two child tags, url-pattern and servlet-name. url-pattern specifies the type of urls for which, the servlet given in servlet-name should be called. Be aware that, the container will use case-sensitive for string comparisons for servlet matching.

web.xml
<url-pattern>/status/*</url-pattern>
http://server.com/server/status/synopsis               = Matches
http://server.com/server/status/complete?date=today    = Matches
http://server.com/server/status                        = Matches
http://server.com/server/server1/status                = Does not match
url-pattern
<url-pattern>*.map</url-pattern>
http://server.com/server/US/Oregon/Portland.map    = Matches
 http://server.com/server/US/server/Seattle.map     = Matches
 http://server.com/server/Paris.France.map          = Matches
 http://server.com/server/US/Oregon/Portland.MAP    = Does not match, the extension is uppercase
 http://example.com/examples/interface/description/mail.mapi  =Does not match, the extension is mapi rather than map`

Third specification of url-mapping,A mapping that contains the pattern <url-pattern>/</url-pattern> matches a request if no other pattern matches. This is the default mapping. The servlet mapped to this pattern is called the default servlet.

The default mapping is often directed to the first page of an application. Explicitly providing a default mapping also ensures that malformed URL requests into the application return are handled by the application rather than returning an error.

The servlet-mapping element below maps the server servlet instance to the default mapping.

<servlet-mapping>
  <servlet-name>server</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

For the context that contains this element, any request that is not handled by another mapping is forwarded to the server servlet.

  • The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  • The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the / character as a path separator. The longest match determines the servlet selected.
  • If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last . character.
  • If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a default servlet is defined for the application, it will be used.

java - What is url-pattern in web.xml and how to configure servlet - S...

java servlets web.xml url-pattern servlet-mapping
Rectangle 27 17

I guess what you want is your index servlet to act as the welcome page, so change to:

<welcome-file-list>
   <welcome-file>index</welcome-file>
</welcome-file-list>

So that the index servlet will be used. Note, you'll need a servlet spec 2.4 container to be able to do this.

jsp - How to configure welcome file list in web.xml - Stack Overflow

jsp servlets web.xml welcome-file
Rectangle 27 9

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
            <executions>
                <execution>
                    <id>prepare-war</id>
                    <phase>prepare-package</phase>
                    <configuration>
                        <archiveClasses>false</archiveClasses>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix />
                            </manifest>
                            <manifestEntries>
                                <Implementation-Build>${build.number}</Implementation-Build>
                                <Implementation-Title>${project.name}</Implementation-Title>
                                <Built-By>${user.name}</Built-By>
                                <Built-OS>${os.name}</Built-OS>
                                <Build-Date>${build.date}</Build-Date>
                            </manifestEntries>
                        </archive>
                        <webResources>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                <directory>./target/dist</directory>
                            </resource>
                        </webResources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Please notice that the <failOnMissingWebXml>false</failOnMissingWebXml> section has been moved up to the plugin configuration rather than the execution.

java - Why is maven-war-plugin failing for web.xml missing if I config...

java maven maven-3 war maven-war-plugin
Rectangle 27 40

Now you can configure it in your web.xml:

<session-config>
    <cookie-config>
        <name>MY_JSESSIONID_YAHOOOOOO</name>
    </cookie-config>
</session-config>

java - Changing cookie JSESSIONID name - Stack Overflow

java tomcat jsessionid
Rectangle 27 6

The execution ID in the POM is prepare-war. Maven runs its own default execution of the war plugin for projects with packing type war. The default execution has ID default-war. As the POM is currently configured, the war goal is running twice.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.4:war (default-war) on project com.specktro.orchid.operations.portal.frontend: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

You may see the execution ID that fails in parenthesis (default-war). If you change the execution ID to default-war your problem will go away, AND you will no longer have two executions of the war goal running.

java - Why is maven-war-plugin failing for web.xml missing if I config...

java maven maven-3 war maven-war-plugin
Rectangle 27 37

With the web.xml configured they way you have in the question, in particular:

DispatcherServlet
/tasklist/
/tasklist/some-thing.html
/tasklist/WEB-INF/views/index.jsp

Because of this, when your controller returns a view that points to a .jsp, instead of allowing your server container to service the request, the DispatcherServlet jumps in and starts looking for a controller that can service this request, it doesn't find any and hence the 404.

The simplest way to solve is to have your servlet url mapping as follows:

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Notice the missing *. This tells the container that any request that does not have a path info in it (urls without a .xxx at the end), should be sent to the DispatcherServlet. With this configuration, when a xxx.jsp request is received, the DispatcherServlet is not consulted, and your servlet container's default servlet will service the request and present the jsp as expected.

Hope this helps, I realize your earlier comments state that the problem has been resolved, but the solution CAN NOT be just adding method=RequestMethod.GET to the RequestMethod.

I think Spring's documentation is partly responsible for this confusion. They use this web.xml piece <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/example/*</url-pattern> </servlet-mapping> as an example, without any warning that the resolved views might go back to the dispatcher again if servlet mapping is not carefully configured. Most people would just remove the example part and that's how they got this strange problem.

You're a god and I am not and I just thought that you should know.

You are too kind @asteri

what if I want .json to be served by the dispatcher servlet. do I have to explicitly map the url pattern?

@TharinduVishwanath yes, you will need a url pattern if you want .json requests to be served by dispatcher servlet

java - No mapping found for HTTP request with URI Spring MVC - Stack O...

java spring model-view-controller spring-mvc
Rectangle 27 37

With the web.xml configured they way you have in the question, in particular:

DispatcherServlet
/tasklist/
/tasklist/some-thing.html
/tasklist/WEB-INF/views/index.jsp

Because of this, when your controller returns a view that points to a .jsp, instead of allowing your server container to service the request, the DispatcherServlet jumps in and starts looking for a controller that can service this request, it doesn't find any and hence the 404.

The simplest way to solve is to have your servlet url mapping as follows:

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Notice the missing *. This tells the container that any request that does not have a path info in it (urls without a .xxx at the end), should be sent to the DispatcherServlet. With this configuration, when a xxx.jsp request is received, the DispatcherServlet is not consulted, and your servlet container's default servlet will service the request and present the jsp as expected.

Hope this helps, I realize your earlier comments state that the problem has been resolved, but the solution CAN NOT be just adding method=RequestMethod.GET to the RequestMethod.

I think Spring's documentation is partly responsible for this confusion. They use this web.xml piece <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/example/*</url-pattern> </servlet-mapping> as an example, without any warning that the resolved views might go back to the dispatcher again if servlet mapping is not carefully configured. Most people would just remove the example part and that's how they got this strange problem.

You're a god and I am not and I just thought that you should know.

You are too kind @asteri

what if I want .json to be served by the dispatcher servlet. do I have to explicitly map the url pattern?

@TharinduVishwanath yes, you will need a url pattern if you want .json requests to be served by dispatcher servlet

java - No mapping found for HTTP request with URI Spring MVC - Stack O...

java spring model-view-controller spring-mvc
Rectangle 27 9

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
    <init-param>
        <param-name>cacheControl</param-name>
        <param-value>max-age=3600,public</param-value>
    </init-param>
</servlet>

With wicket the static resources are actually served with WicketServlet, so this does not help :(

If you're not using Wicket, like me, but you are using Jetty 8 or later, change org.mortbay.jetty.servlet.DefaultServlet to org.eclipse.jetty.servlet.DefaultServlet

configuration - How to configure cache for static resources in web.xml...

configuration caching jetty embedded-jetty cache-control
Rectangle 27 13

Only if you have two config xml files. One with Services / DAOs and another with Controller. If you have configured everything in one spring config file you don't need the ContextLoaderListener, just the dispatcher servlet is sufficient.

It is recommended to split the config into two and use the ContextLoaderListener to create the root application context and the dispatcher servlet to create the web layer application context.

java - Adding ContextLoaderListener to web.xml in Spring MVC - Stack O...

java spring spring-mvc
Rectangle 27 1

You probably need to use Guice as a service locator in TestServlet.

TestServlet(Foo foo){ ... } // please inject foo!

    TestServlet()
    {
        this( MyGuiceServletConfig.injector.getInstance(Foo.class) );
    }

--

public class MyGuiceServletConfig extends GuiceServletContextListener {

    public static final Injector = Guice.createInjector(new MyServletModule());

    @Override
    protected Injector getInjector() {
      return injector; 
    }
}

(they say DI frameworks are not intrusive :)

Great answer! I was kind of hoping not to do this sort of thing, but thankyou for the details of how to :)

java - Add Guice dependency Injection to Servlets configured in web.xm...

java servlets dependency-injection guice
Rectangle 27 1

Do you have your web.xml configured?

Should have something like

<servlet-mapping>
   <servlet-name>servletInterface</servlet-name>
   <url-pattern>/servletInterface</url-pattern>
</servlet-mapping>

in it.

Also, you should probably extend HttpServlet instead of Servlet.

this solution is not working. It gives an error and server stops

Provide more information about the error and your server configuration and how you're making the call.

<servlet-class> needs to be the fully qualified name of your class (as @OTM said, "com.shaby.newservletdemo.servletInterface"), but since that throws the ClassNotFoundException that suggests there is a problem with your packaging. What do your pom.xml files look like? Alternativelly, you could try annotating the class with @WebServlet and leave out the web.xml configuration.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

java - Running servlet shows error - Stack Overflow

java servlets http-status-code-404
Rectangle 27 2

Datasource is usually defined and configured in the server configuration files. It can be defined in application (via web.xml or annotations), but I'd avoid that since it's less flexible and hardcodes database details in the application itself.

Datasource is made available to the application via JNDI name, let's say jdbc/myDS.

In the persistence.xml you can provide datasource JNDI name that will be used by your JPA application. The JNDI name can be either in the global name (in that case it must match the JNDI name defined in the server - so in our case jdbc/myDS) or as resource reference name (e.g. java:comp/env/jdbc/myDSReference).

If you use resource reference, it provides you with better flexibility as it dont have to match JNDI name defined in the server, however you will have to define that reference in your application and provide binding for it.

There are several ways to define reference and binding:

Annotations Use @Resource annotation in a servlet to define reference name and binding via lookup attribute:

@Resource(type=DataSource.class, name="jdbc/myDSReference", lookup="jdbc/myDS")
web.xml
<resource-ref>

The ibm-mmm-bnd.xml file (where mmm can be web in case of web module, or ejb-jar in case of ejb module) is a binding file, which can provide mapping between your references and global JNDI names defined on the server. You can use it instead of the lookup attribute (you have to use it in pre Java EE 6 applications, since there was no lookup attribute then).

That binding can also be defined and changed via WebSphere web administrative console during or after application installation or via wsadmin scripting.

In case of EJB project - if you want to use references, you will have to defined them as a resource reference for any given bean which will access the EntityManager. Again either via annotation or deployment descriptor.

jpa - Relationship between persistence.xml datasource and web.xml / ib...

jpa web.xml websphere-8 persistence.xml
Rectangle 27 38

In Spring 3.0.x add the following to your servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation. You need to add the mvc namespace as well but just google for that if you don't know how! ;)

<mvc:default-servlet-handler/>

when I add this line I get: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 31 in XML document from class path resource [META-INF/spring/application-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 35; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:default-servlet-handler'.

Be sure to tackle with the order of the handler when you have some other view resolvers also.

How to handle static content in Spring MVC? - Stack Overflow

spring-mvc
Rectangle 27 38

In Spring 3.0.x add the following to your servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation. You need to add the mvc namespace as well but just google for that if you don't know how! ;)

<mvc:default-servlet-handler/>

when I add this line I get: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 31 in XML document from class path resource [META-INF/spring/application-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 35; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:default-servlet-handler'.

Be sure to tackle with the order of the handler when you have some other view resolvers also.

How to handle static content in Spring MVC? - Stack Overflow

spring-mvc
Rectangle 27 38

In Spring 3.0.x add the following to your servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation. You need to add the mvc namespace as well but just google for that if you don't know how! ;)

<mvc:default-servlet-handler/>

when I add this line I get: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 31 in XML document from class path resource [META-INF/spring/application-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 35; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:default-servlet-handler'.

Be sure to tackle with the order of the handler when you have some other view resolvers also.

How to handle static content in Spring MVC? - Stack Overflow

spring-mvc
Rectangle 27 3

I'm using the CharacterEncodingFilter, configured in web.xml. Maybe that helps.

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

This just filters character in request, not in response - I'm allready using this one

@Hurda: With forceEncoding=true it filters the response too, but it wouldn't help in this case.

Best and quicker answer so far. I also was already declaring and using this filter, but with forceEncoding=false. I just set it to false and "charset=UTF-8" is successfully added to Content-Type header.

java - Who sets response content-type in Spring MVC (@ResponseBody) - ...

java web-applications spring-mvc character-encoding
Rectangle 27 3

I'm using the CharacterEncodingFilter, configured in web.xml. Maybe that helps.

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

This just filters character in request, not in response - I'm allready using this one

@Hurda: With forceEncoding=true it filters the response too, but it wouldn't help in this case.

Best and quicker answer so far. I also was already declaring and using this filter, but with forceEncoding=false. I just set it to false and "charset=UTF-8" is successfully added to Content-Type header.

java - Who sets response content-type in Spring MVC (@ResponseBody) - ...

java web-applications spring-mvc character-encoding
Rectangle 27 3

Assuming you have configured Spring properly in web.xml and applicationContext.xml. Make the following entry in faces-config.xml

<application>
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

Your sample code given above seems fine. What will happen with above entry is Managed Property will be first looked in beans managed by JSF if not found will be searched in beans managed by Spring. Your spring bean should have proper annotations marked and name given in @ManagedProperty should match with default/name given to bean.

As mentioned by @Boni that is not required it is auto injected. I have used settings as you desire.

A side note: Since you are opting for view scope please have a look at this link The benefits and pitfalls of @ViewScoped

java - JSF 2 inject Spring bean/service with @ManagedProperty and no x...

java spring jsf jsf-2 annotations