java servlet not found error 404 in eclipse [duplicate] - java

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I am using Eclipse 4.3 kepler on Ubuntu 14.04 and using Apache tomcat 7 , I have successfully created .jsp and .html files in my project but I cannot Run a servlet, I have all jar files added in library . I have read every article on this error 404 ,which I am getting when I launch the following servlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/taxProcessor")
public class taxProcessor extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public taxProcessor() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String s=request.getParameter("t1");
int i=Integer.parseInt(s);
String s1=request.getParameter("t2");
int a=Integer.parseInt(s1);
int t=0;
if(i>=500000)
{
t=i*(30/100);
}else if(i>=300000)
{
t=i*(20/100);
}else if(i>=200000)
{
t=i*(10/100);
}
if(a>=60)
{
t=t-(i*(10/100));
}
PrintWriter out=response.getWriter();
out.println("Thank you for Visiting");
out.println("Your Tax Laibality is"+t+"\n");
out.close();
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
my web.xml file is
and all what I get is this

Instead of web.servlets.newtax.taxProcessor.java use web.servlets.newtax.taxProcessor in the web.xml
BTW your package name is servlet then how come you are giving web.servlets.newtax in the servlet-class tag
Example if in your eclipse your servlet class is in src/somePackageName/ServletClassName then your web.xml must contain the entries like this
<servlet>
<servlet-name>anyName</servlet-name>
<servlet-class>somePackageName.ServletClassName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anyName</servlet-name>
<url-pattern>/taxProcessor</url-pattern>
</servlet-mapping>
One more thing as you specified the entries in the web.xml you can remove the annotation#WebServlet("/taxProcessor")
Finally run the servlet as http://localhost:8080/YourProjectName/taxProcessor

In web.xml give /newtax/..../taxProcessor .Dont give /newtax/..../taxProcessor.java and Check from browser by giving the url http://localhost:<port-number>/newtax.taxProcessor. Here give port number to your tomcat server port.Default is 8080

Related

servlet reults in page not found error and wrong mapping to the endpoint specified in the annotation [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Change default homepage in root path to servlet with doGet
(2 answers)
Closed 1 year ago.
i am learning how to create servlets. i followed this tutorial
https://medium.com/#prashant.srivastava7744/how-to-create-hello-world-servlet-example-using-eclipse-ide-with-tomcat-7-39fdbdc21573
it shows how to create simple hello world servlet.
i followed the steps mentioned in the tutorial but when i run the code via
right click
run as
run on server
i receive
HTTP Status 404 – nicht gefunden"page not found"
and i noticed that despite the end point which stated in the annotation is
#WebServlet("/HelloWorld")
it gets change to be
http://localhost:8080/ServletHelloWorld/
it should be
http://localhost:8080/tHelloWorld/
please let me know how to fix this error
code:
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloWorld
*/
#WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public HelloWorld() {
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("Hello World!");
out.println("</body></html>");
out.close();
}
}
folder structure:
web.xml:

Issue with servlet defaulting to index servlet. Tomcat 8.0.30 JDK 1.8

So I have a servlet listening on "/ajax/foo", and a servlet listening on "/". When I make a call to /ajax/foo, it initializes that servlet but doesn't call the get or post overridden methods it defaults to "/" for those, even thou I am not extending it, I am extending HttpServlet.
I am also using annotations to setup the servlet paths. I wonder if this can be causing it. I also checked all the methods and even copied over servlet doPost/doGet methods, that are working since I can verify it yet in this servlet still don't get called. Servlet doesn't write anything out, and is being called over ajax (jquery $.post).
Thank you for any help.
package ajax;
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 javax.servlet.http.HttpSession;
#WebServlet("/ajax/foo")
public class FooAjaxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ThreadAdminActionAjaxServlet() {
super();
System.out.println("LOADED");
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("GET")
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("POST");
}
}

not able to resolve #WebServlet("/FileDao") error in servlet

I have following Servlet .
package com.ser1;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FileDao
*/
#WebServlet("/FileDao")
public class FileDao extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public FileDao() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
I just created this new Servlet in eclipse but I am understanding the line #WebServlet("/FileDao"). Can someone please tell what is the use of #WebServlet("/FileDao") and how to resolve the error ?
Here is the error shown by eclipse
#WebServlet("/FileDao") this line is shown as error in eclipse
WebServlet cannot be resolved to a type The attribute value is undefined for the annotation type WebServlet
You need to import proper annotation:
import javax.servlet.annotation.WebServlet
and import servlet3.jar into your project as compile time dependency. Do not copy it to war, otherwise it will break deployment (or do some strange things). Jar can be either copied from your web container (tomcat) or from Oracle.
This annotation is used to define servlet in programmatic way. It is faster and more convinient than writing cca 8 xml tags in web.xml. See oracle tutorial.
You have to assign a server to your project.
Right click on project and select Properties (last one)
Select on left panel "Targeted Runtimes" and select your server.
Click on and re-import the classes if it's needed with Ctrl + Shift + O

How getRequestDispatcher will work for servlet in different project in same server?

In servlet we can use requestdispatcher and it will forward from one servlet to another servlet with same session in same project.but if i am using different project getRequestDispatcher is not working.its giving 404 error.because its appending servlet name before the url. how can i achieve getRequestDispatcher in different project in same server?
RequestDispatcher rd = request.getRequestDispatcher("/v1/status1/toreply1");
rd.forward(request, response);
In same project getRequestDispatcher working fine. but in different project its not working ?can anyone explain why its happening ..?
You can achieve getRequestDispatcher in different project on same server.
Check out https://stackoverflow.com/a/19273223/1428052
It works on same server for different project by using
getServletContext().getContext() method.
You can follow below steps for detail implementation.
First you need to make changes in below file
(Windows) C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\context.xml
Set value of crossContext to true.
context.xml
<Context crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
Please note that crossContext="true".
Suppose you have two web applications with name InterServletComm1 and InterServletComm2
having servlets Servlet1 and Servlet1 in each web application respectively. Then the code in each servlets goes as follows:
Servlet1.java
package interServletComm1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 Servlet1
*/
#WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Servlet1() {
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();
request.setAttribute("name", "WebApp1");
ServletContext context = getServletContext().getContext("/InterServletComm2");
RequestDispatcher rd = context.getRequestDispatcher("/Servlet2");
rd.forward(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Servlet2.java
package interServletComm2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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 Servlet2
*/
#WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Servlet2() {
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();
String name = (String) request.getAttribute("name");
pw.println("This is web application 2.");
pw.println("<br>The value received from web application one is: " + name);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Above code sends attribute name from InterServletComm1 and it is received in InterServletComm2.
Please let me know if this answer is not clear.
You have to use the ServletContext of the other web application to get the RequestDispatcher. The other context can be looked up by ServletContext#getContext(String uri):
ServletContext otherContext = request.getServletContext().getContext("/other");
RequestDispatcher rd = otherContext.getRequestDispatcher("/v1/status1/toreply1");

Communication between two web application in same server

All,
I have 2 web applications, Web1 and Web2, deployed on my tomcat server. I want classes in Web1 to call methods on classes in Web2. One way to do this is using webservice. Is there any other way similar to calling a method on class on same web application ?.
Thanks.
Yes. It is possible. It tried for same servlet container by using
getServletContext().getContext() method.
First you need to make changes in below file
(Windows) C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\context.xml
Set value of crossContext to true.
context.xml
<Context crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
Please note that crossContext="true".
Suppose you have two web applications with name InterServletComm1 and InterServletComm2
having servlets Servlet1 and Servlet1 in each web application respectively. Then the code in each servlets goes as follows:
Servlet1.java
package interServletComm1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 Servlet1
*/
#WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Servlet1() {
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();
request.setAttribute("name", "WebApp1");
ServletContext context = getServletContext().getContext("/InterServletComm2");
RequestDispatcher rd = context.getRequestDispatcher("/Servlet2");
rd.forward(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Servlet2.java
package interServletComm2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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 Servlet2
*/
#WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Servlet2() {
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();
String name = (String) request.getAttribute("name");
pw.println("This is web application 2.");
pw.println("<br>The value received from web application one is: " + name);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Above code sends attribute name from InterServletComm1 and it is received in InterServletComm2.
Please let me know if this answer is not clear.
Just searched around some articles and the above scenario is certainly possible using CrossContext switching in Tomcat.
Set the following element in context.xml in <Context crossContext="true">
And then getServletContext().getContext("/Web2");.
Haven't tried yet, though.
Yes you can do it using javax.servlet.ServletContext and javax.servlet.RequestDispatcher API's. Here it is how it can be done from Web1:
ServletContext otherContext = servletContext.getContex("/Web2");
RequestDispatcher dispathcer = otherContext.getRequestDispatcher("/a/b.jsp");
dispatcher.forward(request, response);
//or
dispatcher.include(request, response);
Package the classes you want to share between web applications into a separate jar. Put them under common/lib so that the common classloader will load the classes and would be available to both web applications. Then expose the instance in web2 using jndi, look up them from web1 and invoke the methods.
Pretty much it is not that simple. You can share and import classes from your app1 into app2, but maybe they're all linked with other classes. So the idea is not so good except of small services like beans which are used to make calculations for example. There is a reason ppl are using web services so much ;).

Categories

Resources