How to find my servlet for web.xml? - java

I am using wildfly, JBOSS
And I made a Servlet...called ServletImg
My folder structure looks like this:
WebContent/WEB-INF/classes/ServletImg.java
Now i am trying to add him into the web.xml file...like this:
<servlet>
<servlet-name>ServletImg</servlet-name>
<servlet-class>ServletImg</servlet-class> <!-- Here is the problem -->
</servlet>
<servlet-mapping>
<servlet-name>ServletImg</servlet-name>
<url-pattern>/ServletImg</url-pattern>
</servlet-mapping>
Error msg:
servlet.class must be valid fully qualified class name
Any suggestions on resolving the issue?

The point is, everything you need to know is in the question:
Use a fully qualified name.
To solve your problem, all you need to do is:
Create a package to put your servlet in,
Use the name my.package.ServletImg in the web.xml.
It isn't a good practice to put Java classes in the default package, and can be confusing sometimes to servers, as you just faced it

The problem is very clearly given in the error that you've mentioned.
You need to mention the Servlet class name with the full package structure.
For example, check this!
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>examples.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Observe that the tag < servlet-class> is a fully qualified Class name, that includes the package in which the class has been declared.

Create .java classes in src folder not in WebContent/WEB-INF/classes/
and next if you are creating java dynamic web project then select Dynamic Web module version 2.5 so that you can find web.xml file in Path /ProjectName/WebContent/WEB-INF/web.xml

Related

Spring MVC base/root URL is the same with war name

I created a basic rest service using Spring MVC with xml config. My app name is: myservice. When i create a war from it, I receive a war named myervice-1.0.0. In conclusion, I have to access my application through http://localhost:8080/myservice-1.0.0/resource. I'd like to be just 'myservice', like project name. What can I do? Thanks a lot. I'm using tomcat + gradle.
web.xml:
<servlet>
<servlet-name>webappservice</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webappservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
You can rename the war name. If you are using maven as build tool. Then you can use below solution.
<build>
<finalName>myservice</finalName>
. . .
</build>
It will better if you can share some configuration details like pom.xml,web.xml etc.
If you double click on the server definition in STS, you will see a "modules" tab. From there, you can edit the "Path" and set it to whatever you want. When you select "Run on Server", STS has to define a context path and simply defaults it to last element of the default package. If I recall correctly, by default Tomcat uses the file name name of the zipped or exploded .war. In either case, you can over-ride it.
see more about it here

How to setup Jersey on Jetty

Here are my relevant files.
POM file: http://pastebin.com/ZPTWifys
web.xml file: http://pastebin.com/7kd8Et6Y
JAX-RS resource: http://pastebin.com/ddwDEwNK
I do a mvn clean install - works fine. I point my browser to localhost:8080/firstjersey. I get the "Hello World" index page. Then I try pointing my browser to localhost:8080/firstjersey/resources/test, and I get nothing. I try localhost:8080/resources/test, and get nothing. The problem is that I am simply unable to access my resource. I don't know if it's even being scanned by the servlet container. But everything in my configuration looks right. What's wrong?
I appreciate all help in advance.
Your servlet mapping should include a wildcard:
<servlet-mapping>
<servlet-name>My Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>

loading struts-config.xml from web.xml

When I'm writting
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
This tag in web.xml that time I was getting the error
word config notspelled correctly
I know this is a logical name, so you can use any word, but that word
config is underlined in red. Because of that, whenever I run a struts application, I mention the path in a form tag of jsp: something like /book. This book path is also in
the struts-config.xml file.
So when I run this project, I'm getting the error
requested resources not available(/book)
That means struts-config.xml is not loaded correctly. I have imported jar also.
Try this
<init-param>
<param-name>config</param-name>
<param-value>classpath:struts-config.xml</param-value>
</init-param>
Copy struts-config.xml to WEB-INF/classes folder
Also you should have all struts jar files into your WEB-INF/lib
I don't think so it is necessary to move struts-config.xml file somewhere.
Actually word "config" is for initialize parameter(struts-config.xml) with name config.
config with orange color(not red)underline is only for dictionary check nothing else.
So error occurs because
In your struts-config.xml has mess or misconfigured.if possible please add you struts-config.xml with this thread so we can check what actually a reason.
Put it inside servlet action with servlet-class ActionServlet. Worked in my case, have similar problem, but I've just wanted to change folder from classes to my default.
<init-param> <param-name>StrutsConfig</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param>

let jsp file go to servlet

I've got a problem with my .jsp file after it does something it must go to my login.java (servlet) I thought this was possible by doing:
login.java servlet in the web-inf/ serverlet folder ( that i created )
and in my jsp page doing this ${pageContext.request.contextPath}/servlets/login as url.
The problem then is that I get an error "The requested resource () is not available." with code 404.
Is it not possible to let a jsp go to an servlet via url?
Kind regards,
You need to create the servlet as a normal Java class in a package in the Java source code folder, not in a manually created folder in the web folder. You also need to map the servlet on an URL pattern.
Create a package com.example (or something else, but it must be in a package) in the Java source code folder and then create the following class the usual Java way:
package com.example;
// ...
#WebServlet("/login")
public class Login extends HttpServlet {
// ...
}
If you're using an IDE like Eclipse or Netbeans, then it will be automatically compiled into the right folder and after deployment it will be available on the URL /login, relative to the context path.
<form action="${pageContext.request.contextPath}/login" method="post">
If you're still not using a Servlet 3.0 compatible container (Tomcat 7, Glassfish 3, etc), then you need to remove the #WebServlet annotation and register the servlet the old fashioned way in web.xml as follows:
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.example.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
See also:
Our servlets wiki page - contains some Hello World examples
You cannot put .java files into your compiled application. Check this tutorial: http://netbeans.org/kb/docs/javaee/ecommerce/page-views-controller.html for example of jsp+servlet developing with NetbeansIDE.
And answer of the question: yes, it is entirely possible.

Jersey hello world gives 404

I have the following code in my java class
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
#Path("/hello")
public class Hello {
//This method is called is TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello(){
return "Hello World";
}
//this method is called if TEXT_XML is requested
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return "<?xml version=\"1.0\"?>"+"<Hello> Hello World"+"</hello>";
}
//this method is called if HTML is requested
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello(){
return "<html>"+"<title>"+"hello jersey"+"</title>"+"<body><h1>"+"hello World!!"+"</body></h1>"+"</html>";
}
}
I compiled it and exported it as a .WAR file , when I type
http://127.0.0.1/test_server/hello
I get a 404 . I tried it in WTP, cURL they all return 404.. I'm using tomcat 7.0.26
Note: I am running Tomcat on port 80, and other servlets respond as expected.
web.xml config
<display-name>Jersey_Test</display-name>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.example.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/*</url-pattern>
The following URL gives me Http status 500
http://localhost/Jersey_Test/rest/hello
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
That can happen if you haven't registered the JAX-RS servlet implementation in your web.xml at all. Jersey requires the following configuration:
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.example.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The com.sun.jersey.config.property.packages initialization parameter value must point to the package where all your services are. Your code snippet is however missing the package declaration. I'm not sure if this is omitted for brevity or not, but packageless classes are invisible to classes which are by itself in a package (such as the Tomcat and Jersey engines itself). The above web.xml example assumes that you've a
package com.example.service;
on your webservice classes. Fix or change it accordingly.
Note that the URL-pattern of /* thus means that ALL requests will be passed through Jersey. If you need to deploy other servlets, JSP or static content in the same webapp as well, you might want to specify a more specific URL pattern. E.g.
<url-pattern>/rest/*</url-pattern>
You'll only need to change your request URL to http://localhost/test_server/rest/hello.
Looks like you are registering your servlet in the wrong place. Double check that the root URL of your servlet, and make sure that matches what you are hitting.
Have you tried hitting?:
http://127.0.0.1/hello
Remember that /hello will go after whatever the base URL of your servlet is. Try looking a it in a debugger to see where it mounts.
The problem has been fixed and this is how I did it.
I removed the jersey .jar files from the build path and replaced them in the WEB-INF\lib folder and everything worked.
For me, there was a jar file added in Build path -> Libraries for which the actual jar was missing from the file system.
I removed the entry from build path and added the dependency in pom.xml.
worked like a charm.

Categories

Resources