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

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

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.

Cannot access servlet using Eclipse and Tomcat 9

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.

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.

Folder hierarchy java web

I think that my folder hierarchy is wrong.
Should the .html and .jsp file is in WEB-INF?
I tried to move them there, but I received an error 404.
No, everything you put in the WEB-INF folder will NOT be available to the users of your web application. Your hierarchy looks OK.
WEB-INF resources not directly visible for the public. You can use webservlet annotation. For example;
#WebServlet(urlPatterns = {"/adminlogin", "/register"})
or servlet mapping(old-fashioned),
web.xml :
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>yourPackage.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/adminlogin/</url-pattern>
<url-pattern>/logout/</url-pattern>
<url-pattern>/register/</url-pattern>
<url-pattern>/userlogin/</url-pattern>
</servlet-mapping>
LoginController
//post get method
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
if (userPath.equals("/logout")) {
//Your Model
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
} ...
}

Http status 404 -/the requested resource is not available [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 am new to java, I just tried to read initialization parameters from Deployment Descriptor file (web.xml), But got above error?
My web.xml and java file coding coding in snap attached.
My directrory structure is
c:\....tomcat\webapps\dd\web-inf\classes
No error in java class file.
Java file code which is compiled successfully
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet {
String fileName;
public void init(ServletConfig config) throws ServletException {
super.init(config);
fileName = config.getInitParameter("logfilename");
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request,HttpServletResponse
response)throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(fileName);
out.close();
}
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>MyServlet2</servlet-name>
<servlet-class>MyServlet2</servlet-class>
<init-param>
<param-name>logfilename</param-name>
<param-value>value1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet2</servlet-name>
<url-pattern>/mc11</url-pattern>
</servlet-mapping>
</web-app>
Other detail of my directory and error page i think that my web.xml not working
There are two problems I can see at the moment...
Servlet init parameters
You currently have:
//defining param1
param1
value1
That's not how you define the parameter. You should specify a param-name element containing the name of the parameter, rather than using the name of the parameter as an XML element name.
<init-param>
<param-name>logfilename</param-name>
<param-value>...</param-value>
</init-param>
Also note that // isn't how you write comments in XML - if you wanted a comment, you should have:
<!-- Define the first parameter -->
<init-param>
<param-name>logfilename</param-name>
<param-value>...</param-value>
</init-param>
(The param-value element should have been a hint - if you could really just specify your own element, I'd have expected <logfilename>value in here</logfilename> - having the name specified as an element name, but the value specified with a fixed element name of param-value would have been an odd scheme.)
Servlet mapping
Currently your mapping is:
<servlet-name> FormServlet</servlet-name>
<url-pattern>/ss/</url-pattern>
</servlet-mapping>
I suspect that mapping won't match http://localhost:8080/dd/ss/s.html because you don't have any wildcards in there - it you may well find that it matches exactly http://localhost:8080/dd/ss/. It's not clear where the dd part comes in, but I assume that's a separate part of your configuration. You should try:
<!-- I would recommend removing the space from the servlet
- name *everywhere*. -->
<servlet-name>FormServlet</servlet-name>
<url-pattern>/ss/*</url-pattern>
</servlet-mapping>
If that doesn't work for http://localhost:8080/dd/ss/s.html, see whether it maps http://localhost:8080/ss/s.html - it may be that your engine isn't configured the way you expect elsewhere.
There is no problem with code,I try net beans tool and done assignment perfectly with the help of above code.Before that may be problem in tomcat.

Categories

Resources