404 error from Servlet using Eclipse & Tomcat 6 - eclipse

I've looked at all the other questions on this matter (and there is a LOT of them), and they all end up being a typo with the address, or a misconfigured servlet mapping, or similar. I've tried all of this stuff and I'm still coming back with a 404 error whenever I try to access a page from my servlet.
Here's the servlet code (basic hello world example taken from mkyong's site)
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
And here's my servlet definition and mapping in the deployment descriptor
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/testservlet</url-pattern>
</servlet-mapping>
Here's the error I'm getting
HTTP Status 404 - /testserver/testservlet
type Status report
message /testserver/testservlet
description The requested resource (/testserver/testservlet) is not available.
Apache Tomcat/6.0.33
Anybody got any idea what might be causing this?
EDIT: I just tested the JSP and Servlet hello world given here
http://www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/
and it worked fine, so I think the problem is with my servlet. Anybody got any ideas what might be wrong with it?

You need to specify the name of the war file. If you have a war file named 'testme.war', you have to access it like this.
localhost:8080/testme/testservlet

According to your mapping, you should be accessing it through : http://localhost:8080/testservlet
Where does the "testserver" come from ? Do you have any reason to use it ?

Related

spring mvc4 http 404 not found error

I have seen other similar posts here on stackoverflow - mostly the poster is hitting a link different than what the serving method in the RESTController resolves to. I am making sure (explained below) i am not doing that mistake but still its not working for me..
I am writing a Spring MVC4 based REST controller for a Servlet 2.5 container (weblogic 10.3.6) and I was suggested to follow this sample web.xml for my web app project
I have followed this web.xml but I am not sure what the contextAttribute with value (shown below) in this piece does ..
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
When I am running my webapp and hitting it with REST client, I keep getting http 404 not found error. The complete error is posted here
http://paste.ubuntu.com/13966788/
Here's the error snippet
08:44:34.368 [main] DEBUG o.s.web.client.RestTemplate - GET request for "http://localhost:7001/demo-0.0.1-SNAPSHOT/customers/2" resulted in 404 (Not Found); inv
oking error handlerTests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.402 sec <<< FAILURE! - in demo.DemoApplicationTests
contextLoaded(demo.DemoApplicationTests) Time elapsed: 0.042 sec <<< ERROR!
org.springframework.web.client.HttpClientErrorException: 404 Not Found
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:289)
at demo.DemoApplicationTests.contextLoaded(DemoApplicationTests.java:45)
i am not sure what should be my context root, The deployed webapp in the weblogic console shows the value 'demo-0.0.1-SNAPSHOT'
and my REST controller's method's #RequestMapping is /customers/{id}.
#RestController
public class CustomerRestController {
#Autowired
private CustomerRepository customerRepository;
#RequestMapping("/customers/{id}")
CustomerProtos.Customer customer(#PathVariable Integer id) {
return this.customerRepository.findById(id);
}
So I assume my url path should look like
localhost:7001/demo-0.0.1-SNAPSHOT/customers/2
but when I hit this URL (both in browser and in spring based test client)
I keep getting http 404 not found error.
Here's my spring test client..
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public class DemoApplicationTests {
#Configuration
public static class RestClientConfiguration {
#Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
return new RestTemplate(Arrays.asList(hmc));
}
#Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufHttpMessageConverter();
}
}
#Autowired
private RestTemplate restTemplate;
#Test
public void contextLoaded() {
ResponseEntity<CustomerProtos.Customer> customer = restTemplate.getForEntity(
"http://localhost:7001/demo-0.0.1-SNAPSHOT/customers/2", CustomerProtos.Customer.class);
System.out.println("customer retrieved: " + customer.toString());
}
I wonder if the contextAttribute is doing something here. Any ideas ?
The only other thing i am doing different here is instead of httpMessageConverter, I am registering Spring MVC4's ProtoBufHttpMessageConverter. But shouldn't be making a difference in this case.
My code is shared (on github) here https://goo.gl/cVNEPT
Do you have servletMapping defined as in the web.xml?
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Also, have public for your method
#RequestMapping("/customers/{id}")
public CustomerProtos.Customer customer(#PathVariable Integer id)
Do you see the request mapping path being registered in the log?

Mixing REST and JSP in Spring MVC, Cannot find JSP

I'm sure this is a noob question, and I've spent the better part of an hour trawling stackoverflow for an answer but nobody seems to have my case so here we go...
I have a new webapp that uses Spring MVC. Most of the app (99%) is pure REST, so it doesn't have a "view" as such but rather simply sends JSON back down the wire, or sends an alternate HTTP Status for errors etc.
The exception is the login page which needs to be an actual JSP, but somehow the configuration I am using to map my REST controllers is leaving me in a state where normal JSP mappings fail.
Here's what I've got:
In my dispatcher servlet config, the relevant portions are:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
In my attempts to get it working, I have also added a mapping to the "HomeController" which currently just redirects to my login JSP:
<bean name="/" class="com.somepackage.HomeController"/>
Now, in the web.xml I have:
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-dispatcher-servlet.xml
</param-value>
</context-param>
This works fine for my RESTful controllers, which look like this:
#Controller
#RequestMapping(value = "/api/user")
public class BlahBlahController {...
My "HomeController", which just looks like this:
#Controller
#RequestMapping(value = "/")
public class HomeController extends AbstractController {
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("login");
}
}
IS triggered when I hit the "/" url, but I get this error in the logs:
WARNING: No mapping found for HTTP request with URI [/WEB-INF/pages/login.jsp] in DispatcherServlet with name 'spring-dispatcher'
Now I get what it's saying, it doesn't know how to resolve /WEB-INF/pages/login.jsp (this page does exist btw), but I'm stuck as to how I need to alter things to get this to work.
I'm a little confused on how it's supposed to work. Anyone got any clues?
Thanks.
OK.. I found the answer, it's the url-pattern in the dispatcher config.
Instead of:
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
It should be
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I had actually found this answer elsewhere and tried it but "thought" it wasn't working, then realized the reason I thought this was unrelated to the root cause.
No idea why this would work and the other wouldn't.. but one problem at a time...
Put RequestMapping at the method level, I tried at my code and it worked. You don't need to define HomeController bean if you are using #Controller and having a proper "context:component-scan"
#Override
#RequestMapping(value = "/")
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("login");
}
you can also use below code if you just want to redirect a login view for all "/" access.
<mvc:view-controller path="/" view-name="login"/>
Checkout the mvc show case project from github for a helpful reference.

GWT: Servlet URL mapping gives a 404 error

I have read the other GWT Servlet questions, but I'm having trouble solving my problem still. My package is called Maps, and it has a service named MyService (which was set up according to a GWT Tutorial). The web.xml file includes the following:
<servlet>
<servlet-name>MyServiceImpl</servlet-name>
<servlet-class>com.xerox.maps.maps.server.MyServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServiceImpl</servlet-name>
<url-pattern>/Maps/service</url-pattern>
</servlet-mapping>
In MyService, I have the line:
#RemoteServiceRelativePath("service")
public interface MyService extends RemoteService { ...
However, when I try to make an RPC call, there is an error thrown. The details of the error say that it is a 404 HTTP error. How can I fix this, to make sure that the mapping is correct?
Edit 7.27
MyService.java contains the annotation:
#RemoteServiceRelativePath("service")
And web.xml contains:
<servlet-name>MyServiceImpl</servlet-name>
<url-pattern>/com.x.maps.Maps/service</url-pattern>
If I follow the XHR with FireBug, it shows me that there is a call to com.x.maps.Maps
404 Not found is thrown usually when service endpoint path is inferred wrongly by GWT. Try removing #RemoteServiceRelativePath("service") and recompile and check, If that does not work find out the URL endpoint of the service manually (by hitting likely paths from a browser till the error changes to 500 internal error) and then give the correct path as argument to #RemoteServiceRelativePath("correct/path"). Few trials I would try right away is #RemoteServiceRelativePath("/Maps/service") and #RemoteServiceRelativePath("Maps/service") without the slash
According to this tutorial:
https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC
The servlet-mapping should be composed of the module "rename-to" and the service "RemoteServiceRelativePath". So, if you have, in your *.gwt.xml file, the following line:
<module rename-to='XXX'>
And in your *Service.java file you have the following line:
#RemoteServiceRelativePath("YYY")
Then, in your "web.xml" file, you should have the following lines:
<servlet-mapping>
<servlet-name>...servlet-name>
<url-pattern>/XXX/YYY</url-pattern>
</servlet-mapping>
New answer after all the comments :
Cool, you have made progress!
You are hitting this URL -
http://127.0.0.1:8888/com.x.maps.maps.Maps
With this POST data I assume - /%7C98544A4AED8C7D42E80C55859E9CEC4C%7Ccom.x.maps.maps.client.MyService%7CreadFile%7Cjava.lang.String/2004016611%7CPrinterList.xls%7C1%7C2%7C3%7C4%7C1%7C5%7C6%7C
This is where the problem is, your servlet is mapped to respond to XHR requests coming to <url-pattern>/Maps/service</url-pattern> but you are hitting /com.x.maps.maps.Maps instead. Hence you are getting the 404 path not found status code.
Alter the url-pattern on the server-side web.xml to match what the browser is making,
OR
Alter the GWT code using the RemoteServiceRelativePath annotation to make the request to /Maps/service instead of to /com.x.maps.maps.Maps
I have had the same problem but I solved it changing the url-pattern of the Servlet in the web.xml
Try to put in your web.xml the path to the directory where your GWT javascript module is generated, behind WEB-INF/deploy. in my case:
<url-pattern>/gwtmodulemain/selection</url-pattern>
You can also rename your module name in your gwt.xml file:
<module rename-to='gwtmodulemain'>
so you can refer your module from your HTML in this way:
<script type="text/javascript" language="javascript" src="gwtmodulemain/gwtmodulemain.nocache.js"></script>
Good luck!

coding a server on using restlet 2.0

I am coding a server for a project of mine using restlet 2.0. I have a java class which starts the server (starting it on a port and all those stuff). I am stuck at a point where i need to map the uri's of different services i intend to offer. If i were to include uri mapping part in a servlet how do i go about it. what are the changes i need to make in the web.xml. i have found very little documentation regarding this.
Any help appreciated
In fact, the routing configuration must be done in your Restlet application class. You need to override the createInboundRoot method to attach your resources to paths, as described below:
public class MyRestletApplication extends Application {
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/path/{id}", MyServerResource.class);
(...)
return router;
}
}
The configured resources can be then reached through the configured Restlet server. For example, with the address http://localhost:8082/path/12 if you implement your Restlet server as following:
public static void main(String[] args) {
try {
Server server = new Server(Protocol.HTTP, 8182);
server.setNext(new MyRestletApplication());
server.start();
(...)
} catch(Exception ex) {}
}
Restlet also provides a servlet adapter with its org.restlet.ext.servlet extension. The latter allows using the ServerServlet servlet in order to access the configured resources. When configuring this servlet you need to specify the application class to use (the application contains the paths for your resources) through the org.restlet.application context parameter. The servlet can be configured as every servlet and be mapped on the /* pattern, as described below:
<web-app>
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>org.restlet.example.MyApplication</param-value>
</context-param>
<servlet>
<servlet-name>ServerServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServerServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
In this case, your RESTful application implemented with Restlet will be accessed through a servlet container. In this case, your application needs to be packaged as a Java EE web application and will be reached with address: http://localhost:8080/mywebapp/path/12.
Hope it'll help you.
Thierry

Shorten path of REST service in JBoss Seam application

I'm pretty new to JBoss and Seam. My project has a REST service of the style
#Path("/media")
#Name("mediaService")
public class MediaService {
#GET()
#Path("/test")
public Response getTest() throws Exception {
String result = "this works";
ResponseBuilder builder = Response.ok(result);
return builder.build();
}
}
I can reach this at http://localhost:8080/application/resource/rest/media/test. However, I don't like this URL at all and would prefer something much shorter like http://localhost:8080/application/test.
Can you please point me in the right direction on how to configure the application correctly? (Developing using Eclipse)
web.xml will contain seam resource servlet mapping , this should be modified to /*, and if you have more configuration to the path it will be in components.xml ,if it is resteasy seam is configured to use, it will look like the following
<resteasy:application resource-path-prefix="/rest"/>