Simple Servlet with Eclipse Juno and J2EE Preview server - eclipse

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!

Related

HTTP 404 error. The requested resource is not available. I am using Jdk 1.8 and eclipse jee mars [duplicate]

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.

Tomcat Servlet: - The requested resource is not available

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 .

Getting error while servlet program HTTP Status 404

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.

Separating Java EE components in different projects

i'm trying to learn how to separate java EE components.
i want to separate my jpa entities, ejbs, and web client into different projects
so i developed four different projects and compiled them in their own jars/wars
DomainEntities - contains my JPA entities
RemoteInterface - contains remote interfaces (i'm trying to separate the implementation)
RemoteEJB - implements the interface from RemoteInterface (project 2)
WebApp Client (i have a servlet that calls RemoteInterface to test if the ejb is working)
DomainEntities' jar structure is
DomainEntities
-META-INF
-MANIFEST.MF
-entities
-Student.class
RemoteEJB's jar structure is
RemoteEJB
-META-INF
-MANIFEST.MF
-persistence.xml
-sample
MyStatelessBean.class
The persistence.xml in RemoteEJB is (based on searching the web):
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="RemoteEJBPU" transaction-type="JTA"><provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/pwucdb</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!--<jar-file>DomainEntities.jar</jar-file>-->
</persistence-unit>
</persistence>
the jta datasource is defined in my glassfish server.
the exclude-unlisted-classes and jar-file tags are based on my research on separating the jpa entities on a different jar. when i uncomment the jar-file tag, the ejb cannot be deployed to the server,so i comment it.
the servlet that calls uses the ejb is:
#WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
#EJB
private MyRemoteInterface myRemoteInterface;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println(myRemoteInterface.getMessage());
out.println("Num of records retrived: " + myRemoteInterface.getStudents().size());
}
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
when i try to access the servlet i am having this warning/error
WARNING: The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
WARNING: A system exception occurred during an invocation on EJB MyStatelessBean method public java.util.List sample.MyStatelessBean.getStudents()
javax.ejb.EJBException
.....
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Error compiling the query [SELECT s FROM Student s]. Unknown entity type [Student].
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1477)
if i comment out exclude-unlisted-classes tag, uncomment the jar-file tag and deploy. i get this server warning:
a jar-file [DomainEntities.jar] specified in persistence.xml is not found
The server also looked for a file called [...\build\DomainEntities_jar].
I recommend to bundle that JAR's and WAR's into an EAR-Archive.
If you use maven you could add this plugin to the EAR's pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<version>6</version>
<encoding>${project.build.sourceEncoding}</encoding>
<defaultLibBundleDir>APP-INF/lib</defaultLibBundleDir>
<fileNameMapping>no-version</fileNameMapping>
<displayName>${project.artifactId}</displayName>
<modules>
<jarModule>
<groupId>${project.groupId}</groupId>
<artifactId>domain</artifactId>
</jarModule>
<ejbModule>
<groupId>${project.groupId}</groupId>
<artifactId>ejb</artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
Thus all libs of the EAR will be copied into /APP-INF/lib.
In this scope its now accessible from the EJB view. Just call it like this:
<jar-file>APP-INF/lib/domain.jar</jar-file>
Note the
<fileNameMapping>no-version</fileNameMapping>
thats really important if you want to change the version of your domain-archive frequently otherwise you have to edit the EJB's persistence.xml all the time ;)
Some additions:
If you bundle everything into an EAR-Archive its not neccessary to use Remote Interfaces anymore. You just need Local Interfaces for that. Thats a good thing because accessing an EJB over Remote Interfaces is much slower than accessing them with Local Interfaces. Thats because Java serializes all objects to send them over the network.

Deployment Error Weld-001408 with WLS12c and OEPE

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'