Cannot access servlet using Eclipse and Tomcat 9 - java

I created a new dynamic web project called TestWeb in Eclipse. I added a single index.html to the WebContent folder and created a single web servlet, making the servlet available at /Test.
I can access the index.html file at http://localhost:8080/TestWeb, however, I cannot access the servlet at http://localhost:8080/TestWeb/Test. Please assist.
Here is the code for the Servlet:
#WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
public Test() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
}
Here is 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="4.0">
<display-name>TestWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Firstly check your web.xml (deployment descriptor) file, make sure it has correct xml schema defined in it and if everything is good there then change you annotation like this
#WebServlet("/Test/*")

I soled the issue by renaming the project. My project name consisted of two words and one space. "Password Services". Once I renamed the project to "Server" I could reach the servlet.

Related

how to load servlet from main method

I have Main class, there I doing api request and integrate with database.
I also create servlet where I want to get data from clients and put it in database.
it is the main method in Main class:
public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(8888); // Server port
System.out.println("Server started. Listening to the port 8888");
initProviderList();
initNewsAppDB();
Thread newFeedsUpdate = new Thread(new NewFeedsUpdate(providerList));
newFeedsUpdate.start();
}
it is the servlet:
#WebServlet(name = "GetClientTokenServlet")
public class GetClientTokenServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String token = request.getParameter("token");
System.out.println(token);
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>GetClientTokenServlet</servlet-name>
<servlet-class>GetClientTokenServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetClientTokenServlet</servlet-name>
<url-pattern>/GetClientTokenServlet</url-pattern>
</servlet-mapping>
</web-app>
how I can setup the GetClientTokenServlet (to be able listen to clients calls) into main method?
In most cases web applications in Java don't have main methods. The main method is implemented by a servlet container, such as Tomcat, and that's what you actually run. The servlet container discovers your application's classes and web.xml through some method, often by finding them in a WAR file that you have dropped in a directory defined by the servlet container, such as Tomcat's webapps directory. The servlet container then instantiates the servlets identified in your web.xml file.
That said, there are some web servers that you can instantiate as components within your own application. A server that is commonly used for this purpose is Jetty. Jetty is a web server that passes incoming requests to "handlers" that you define. You can have Jetty load your entire web application from your WAR file and instantiate the servlets defined in your web.xml, or you can use ServletHandler to register servlets manually; in this case you don't need web.xml.

URL localhost:8080/<project_name>/Servlet don't works [duplicate]

This question already has an answer here:
Change root context for a servlet in an IntelliJ IDEA project
(1 answer)
Closed 4 years ago.
I'm trying to run my simple servlet "Hello".
I have installed tomcat 9.0.6
Than i create a new JavaEE web project in Idea (called test)... after i create new package in src (called servlet), after new servlet file "myServlet".
Project Structure
Here is code:
public class myServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Hello my first Servlet</h2>");
out.println("<br/>");
out.println("Time on the server is: " + new java.util.Date());
out.println("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
Than i open web.xml and add next lines for servlet and servlet-mapping:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>servlet.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
After successfully running tomcat ("Artifact is deployed successfully") i try to visit url: http://localhost:8080/test/myServlet Result was page 404... But if i change url to: http://localhost:8080/myServlet result was correct.
Result
What's wrong with url: http://localhost:8080/test/myServlet ???
Need some libraries or what ???
Where is my mistake ???
Also i try with #WebServlet("/myServlet") annotation without servlet and servlet-mapping lines in web.xml - result the same.
You need to define the context path in context.xml - please see this link
http://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Defining_a_context

Output of ServletProgram created using Servlet Interface is not getting as Expected? [duplicate]

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 created a Servlet Program using Servlet Interface and I am just Sending my response to the Browser using response interface method getWriter() which returns PrintWriter reference and I am trying to print the following Information such as Hello and a Date on the Browser.
But,I am getting a following 404 Error.
Can anyone guide me Why?
Code for my Servlet Program.
class FirstAppUsingServlet implements Servlet
{
public void init(ServletConfig config) throws ServletException
{
}
public void destroy()
{
}
public ServletConfig getServletConfig()
{
return null;
}
public String getServletInfo()
{
return null;
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
Date d1=new Date();
System.out.println(d1);
PrintWriter out=response.getWriter();
out.println("Hello Java");
out.println(d1);
}
}
Code for the 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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>FirstServletApp</display-name>
<servlet>
<description></description>
<display-name>FirstAppUsingServlet</display-name>
<servlet-name>FirstAppUsingServlet</servlet-name>
<servlet-class>FirstAppUsingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstAppUsingServlet</servlet-name>
<url-pattern>/FirstAppUsingServlet</url-pattern>
</servlet-mapping>
</web-app>
And the Following error:
http://localhost:8089/FirstServletApp/WEB-INF/classes/FirstAppUsingServlet.java
Do couple of things:
First check whether TOMCAT server started properly or not. For this, check
http://localhost:8080/ from the browser. I am assumming you have started server on 8080 port.
If Server is started properly, then you will see TOMCAT home page. If it is not, then you have to check JAVA_HOME environmental variable setting.
Check if you have any error on console page of your editor.
Check if your URL is correct because you haven't pasted URL here.

404 when dispatching from servlet served with a wildcarded path

tl;dr: Dispatching to an absolute path from a servlet fails when it (the servlet) is bound with a wildcard (/a/*) but not when bound explicitly (/b/b)
I have a Guice webapp with a servlet that dispatches traffic to an HTML file stored in WEB-INF:
#Singleton
public class MyServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/hello.html").forward(request, response);
}
}
It is configured with a GuiceServletContextListener...
public class MyApp extends GuiceServletContextListener {
#Override
protected Injector getInjector() {
return Guice.createInjector(new MyModule());
}
private static final class MyModule extends ServletModule {
#Override
protected void configureServlets() {
serve("/a/*").with(MyServlet.class);
serve("/b/b").with(MyServlet.class);
}
}
}
...and a web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>MyApp</display-name>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>test.MyApp</listener-class>
</listener>
</web-app>
I launch it in Tomcat and go to localhost/b/b and I see my hello.html page. However, when I go to localhost/a/a, I get a HTTP Status 404 - /WEB-INF/hello.html. This seems very strange to me as the two paths, wildcarded or not, are bound to the same servlet, as well as the HTML path being absolute and not really up for interpretation.
Is this the result of a bug, some (to me unknown) behavior I haven't accounted for, or sheer misconfiguration on my part?
Edit:
Based on some further experimentation, include seems to work as expected, which suggests to me that forward has some additional behavior I've missed. Using include will probably hold me over for now, but I'd still like to know what I'm doing wrong.
I found a GitHub issue which seems to match the problems I've encountered, meaning it's a bug in Guice itself.
The bug was originally reported in 2010 and has been treated with what seems like minor interest from contributors and none whatsoever from those in charge, so it'll probably never be fixed.
For future reference, I'm going to attempt to make do with include for now and consider switching to another framework at some point in the future.

Servlet : Not able to run the Hello World Program

Not duplicate of this question
Here is my code
HelloWorld.java
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// do nothing.
}
}
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>Servlet</display-name>
<servlet>
<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>
there is no compilation error, however facing this issue.
Please help!!
I changed url to
http://localhost:8080/Servlet/HelloWorld
and now facing exception
Folder Structure
You should provide the class name with the package name.
However, I would recommend to try using #WebServlet(name = "HelloWorld", urlPatterns = "/helloWorld") above the servlet class.
so if owuld be :
#WebServlet(name = "Welcome", urlPatterns = "/testPage")
public class HelloWorld extends HttpServlet {
//blah blah
}
There are a couple of things to check :
- first that the application is deployed corectly on the server. You should have a .war project that is deployed. You should see a successful deployment on you server logs
- Build your url. This is composed of server IP, in your case local host, than port 8080 is usually the default, than context root (if not defined than it should be the name of the application) and last the servlet name: http://localhost:8080/appname/helloworld
There is also an possible error on your Web.xml. The servlet class tag you defined your class but has no package. Probably you have the class also in a package.
package. HelloWorld
Verify if there is another application working on the same port as Apache Tomcat.
Try to clean your Apache Tomcat server.

Categories

Resources