I am new to Servlets. Anyhow, My Server runs just fine, I can access my index.html and everything is working but when I try to run my servlet on the server it gives me 404 error
I tried tp restart server, edit the web.xml file but no luck, there's also some older questions on SO for the same question, but all different from my case. Tried urlPatters{"/SimpleServlet"} also no luck..
Running on a Linux machine with Eclipse Luna and tomcat7.
Here's my servlet
package com.bgt.zaa;
import java.io.IOException;
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 SimpleServlet
*/
#WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello World");
}
}
and here's my xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SinmpleServletProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
The error:
Seems a typo mistake in your URL.
try this
127.0.0.1:8080/SimpleServletProject/SimpleServlet
instead of
127.0.0.1:8080/SinmpleServletProject/SimpleServlet
your 404 URL has SinmPleServletProject. An extra n, I guess.
HTTP 404 means, requested resource is not found. So most probably the Servlet is not started properly (or) the project context root is different.
Related
I'm just a newbie in java, rest-api; so i'm trying to build a simple restful-api project used java, eclipse, and wildfly 10. The project was deployed but i cannot call the API via browser by URL. Do i have mistake or configuration has not been set ? Someone help me please, many thanks.
i am not good at english as well, so apologize if it's hard to understand
package it_kickstart;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/rest")
public class KickStartService {
#GET
#Path("/html")
#Produces("text/html")
public String getHTML() {
return "<p>JAX-WS with JBoss WildFly 10.x</p>";
}
#GET
#Path("/text")
#Produces("text/plain")
public String getText() {
return "JAX-WS with JBoss WildFly 10.x";
}
}
my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Rest_API_1</display-name>
</web-app>
URL used to call : http://localhost:8080/Rest_API_1/rest/html
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 5 years ago.
I am putting my code where I am facing problem.
WriteToPdf1.java
import java.io.*;
import javax.servlet.http.*;
import com.darwinsys.spdf.PDF;
import com.darwinsys.spdf.Page;
import com.darwinsys.spdf.Text;
import com.darwinsys.spdf.MoveTo;
public class ServletPDF1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename='CreatePDFFile.pdf'");
PDF p = new PDF(out);
Page p1 = new Page(p);
p1.add(new MoveTo(p, 200, 700));
p1.add(new Text(p, "Hi This is created PDF file by using Servlet"));
p1.add(new Text(p, "by us...Bhumesh Patel...."));
p.add(p1);
p.setAuthor("Bhumesh Patel....");
p.writePDF();
}
}
index.html
CLICK HERE
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WriteToPdf1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletPdf1</servlet-name>
<servlet-class>ServletPdf1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletPdf1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
</web-app>
Please help I am doing this in eclipse jee mars and tried several options but could not execute it.I have attached all code. plz help.
If you changed the url mapping of a servlet in your web.xml file during runtime of the server, (depending on your server) it will not pick up on the changes. Try cleaning, rebuilding and restarting the server.
I am new to writing a Java Servlet, and am struggling to get a simple servlet to work properly.
The Servlet Test1.java class is:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
#WebServlet("/Test1")
public class Test1 extends HttpServlet {
public Test1() {
super();
// TODO Auto-generated constructor stub
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
The problem is when trying to run this simple servlet the servers shows an error which is :
message : /Projet1/Test
HTTP 404 : the requested resource is not available
Projet1 is the name of the project
for web.xml i"ve removed the declaration of this servelt as i saw in many question , which says that we cant not use the annotation in the servelt and in web.xml at the same time , anyway this is web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<display-name>Projet1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
for the structure and the results : Structure du projet
the result : the result after run
I am running Tomcat v7.0, and have already read similar questions, with responses referring to changing the invoker servlet-mapping section in web.xml ,
i've tried so many solutions pas no changes ! Help please .
I created the following projects in OEPE:
weld001408 (Java EE - Enterprise Application Project)
weld001408utility (Java EE - Uitility Project), belongs to the ear weld001408
weld001408web (Web - Dynamic Web Project), belongs to the ear weld001408
When I created the project, I didn't change the defaults (onley ear-membership) and the target-runtime is Oracle WebLogic Server 12.1.1. And the clean domain, created with the wizard, is added to the workspace.
The Code is the following:
utility-project
class 'weld001408utility/ValueProducer.java'
package weld001408utility;
import javax.enterprise.inject.Produces;
public class ValueProducer {
#Produces
public String stringValue() {
return "someStringValue";
}
}
META-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
web-project
weld001408web/ValueConsumerServlet.java
package weld001408web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.inject.Inject;
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 ValueConsumerServlet
*/
#WebServlet("/ValueConsumerServlet")
public class ValueConsumerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
#Inject
private String injectedValue;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.write("injectedValue: " + injectedValue);
pw.flush();
}
}
WEB-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
The issue
Now when I try to deploy the application in Eclipse, I get always a WELD-001408 error:
<01.03.2013 10:07 Uhr MEZ> <Warning> <Deployer> <BEA-149078> <Stack trace for message 149004
weblogic.management.DeploymentException:
at weblogic.application.internal.BaseDeployment.throwAppException(BaseDeployment.java:123)
at weblogic.application.internal.BaseDeployment.prepare(BaseDeployment.java:239)
at weblogic.application.internal.EarDeployment.prepare(EarDeployment.java:61)
at weblogic.application.internal.DeploymentStateChecker.prepare(DeploymentStateChecker.java:158)
at weblogic.deploy.internal.targetserver.AppContainerInvoker.prepare(AppContainerInvoker.java:60)
Truncated. see log file for complete stacktrace
Caused By: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [String] with qualifiers [#Default] at injection point [[field] #Inject private weld001408web.ValueConsumerServlet.injectedValue]
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:258)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:105)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:125)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:324)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:309)
Truncated. see log file for complete stacktrace
>
But when I export the ear from eclipse and deploy that file over the weblogic administration console, then everything's working fine and the value is correctly injected to the servlet. No problems on deployment, nothing.
There are some patches for weblogic around and I have the latest installed (I checked a lot bugs which could belongs to my problem, and they should all included with 12.1.1.0.2):
WebLogic Server 12.1.1.0.2 PSU Patch for BUG14331523 Thu Oct 11 15:08:09 IST 2012
WebLogic Server 12.1.1.0 Wed Dec 7 08:40:57 PST 2011 1445491 >
I tried the same application on Windows Vista and on a Linux based System with the latest OEPE Versions (all-in-one package), no success...
The question
Has somone the exact same issue? Is it possible for someone to reproduce the problem? And when not, on which configuration with what patches is it working?
I don't know if it's a Oracle Enterprise Pack for Eclipse or an WebLogic-Issue.
Thanks for helping!
I solved the problem as stated below. Added pictures to make it easier.
Goto Eclipse Servers (View), right click on the weblogic server 12.1.3 and click on properties.
In Weblogic --> Publishing, select 'Publish as an exploded archive'
I'm trying to create my first HelloWorld servlet with Eclipse Juno and view it in the J2EE Preview Server.
This is my Servlet class:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloWorld
*/
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
And this is my web.xml automatically generated by eclipse:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>HelloWorld</display-name>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
When I select "Run on Server" > "J2EE Preview" I get this:
Error 404 - Not Found
No context on this server matched or handled this request. Contexts
known to this server are:
HelloWorld(/HelloWorld)
Where am I doing wrong?
In "J2EE Preview" Server, context path is the name of the project. When you start the server, this list all contextpaths available.
For example, if your applications is named "app1", your URL will be "http://localhost:8080/app1/HelloWorld"
I had the same issue, and couldn't make it work.
I created a much simplier application, just a hello world JSP page.
This process is very basic, you create a web dynamic project and create a index.jsp file under the WebContent directory, this should be good enough to start your application by run as -> run in server -> J2EE preview but always get:
404 not found
No context on this server matched or handled this request.
Contexts known to this server are:
test(/test)
I have heard eclipse juno is not as stable as indigo, i just downloaded the indigo Java EE version did exacly the same and it worked properly no issues.
Edit:
I Forgot to mention that you can download another Application Server such as JBoss or Glassfish and try to run on your application on them, that should fix your problem.
You can try to fix this issue by deleting all the files and folders into your workspace, delete the .metadata folder and all the content of it, the start eclipse and try again this may works.
Hope this can help you.
Regards!