I used eclipse and tomcat and created a dynamic web project following this tutorial:
https://o7planning.org/en/10169/java-servlet-tutorial
I followed all the steps until running the code for the first time to display 'Hello, World!' on a webpage, but it gave the following error. I have added index.html properly.
What could be the issue? Yesterday I created another dynamic project in the same workspace that the simple project was run it, but it's not working either. Could it be a port issue?
HTTP Status 404 – Not Found
Type Status Report
Message /ServletFirstTutorial/
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.14
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>ServletFirstTutorial</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>
Related
Hello am trying to change the default start page for my dynamic project in eclipse from index.jsp to welcome.jsp. I have gone through some of the answers on web and I changed the welcome-file-list accordingly but still its not working.
my web.xml is like this:
<?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>Decryption</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>welcome.jsp</welcome-file>**
</welcome-file-list>
</web-app>
I have edited the welcome file list and i have added <welcome-file>welcome.jsp</welcome-file> to it. But still its not working. Any help will be appreciated.
The order in which you list your <welcome-file-list> entries is important because the web container looks at this list from top to bottom and stops searching at the first match.
So, in case your web content directory has another file, like index.jsp, that is listed before, welcome.jsp will not be served. So, just moving the entry to the top should solve your problem.
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
<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>
By the way, you could also choose to drop all other entries and just keep the one that points to your index file. Listing all the entries is not mandatory.
As Said by Ravi order of files matter in web.xml
So in case you want welcome.jsp to be shown then keept this entry as first line in
<welcome-file-list> tag.
Also its not mandatory to have all files like index.html,index.htm,index.jsp .... etc under <welcome-file-list> tag . If you know your home page then you can add only one jsp as below.
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
Note:
Also as an advice you should put your jsp's under web-inf folder .
refer URL for details
Does any one tell me that,
when eclipse run how to load index.jsp on server ,
when i run server the index.jsp should be reflect but it not reflecting
You do this using the web.xml file which is stored in the WEB-INF folder
<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" version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
hello
</description>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Here the welcome-file is the file you want to load on the server startup..
Hope this helps
I am trying to deploy my WAR in TOMCAT server, and i am using MAVEN 2. My Project name is
"Jira-Synchronization". My project is a simple Ajax call and returns result to display to the user. I have only one page( that's the scope of my project). This is how i made the Ajax call and web.xml
$.getJSON('/Jira-Synchronization/jirarequest','OK', function(jiraData)
<?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>Jira-Synchronization</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>JiraSyncServlet</servlet-name>
<servlet-class>JiraSyncServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JiraSyncServlet</servlet-name>
<url-pattern>/jirarequest/*</url-pattern>
</servlet-mapping>
if you notice the ajax call is taking the Project name( first i felt weired why it is doing that and to move on with the project i just kept it that way) and every thing worked fine inside eclipse running tomcat(This is the URL i used when running from eclipse http://localhost:8080/Jira-Synchronization/index.html).
When i tried to build it using maven and deploy in tomcat(out side of eclipse) the War is made using this name "Jira-Synchronization-0.0.1-SNAPSHOT. My URL has changed tohttp://localhost:8080/Jira-Synchronization-0.0.1-SNAPSHOT/index.html and now my Ajax call which have my project name in it is also need to be changed. this is how my pom.xml looks like
How should i configure my web.xml or pom.xml so that the ajax request excludes the project name to it?
What worked for me is in the Pom.xml i added this <finalName>Jira-Synchronization</finalName> under the <build> tag and that was able to output the WAR with the desired name as i like
I am trying to demo simple Ajax/Servlet request.For this i have created a new Dynamic web project in eclipse and added a simple Servlet to take the request and present the same back to UI. I have included my Servlet details in web.xml. I am using Tomcat as my server. No Build scripts yet(i felt not needed at this point)
Servlet Code:
response.setContentType("text/html");
PrintWriter out =response.getWriter();
String txt = request.getQueryString();
out.println(txt);
Js Code:
$(function(){
$.get('/jirarequest','OK',function(op){
$('#maindiv').appendTo(op);
});
});
Html Code:
<?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>Jira-Synchronization</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>JiraSyncServlet</servlet-name>
<servlet-class>src.JiraSyncServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JiraSyncServlet</servlet-name>
<url-pattern>/jirarequest/*</url-pattern>
</servlet-mapping>
My Project structure:
My HTML:
I am getting a error in Fire bug saying
"NetworkError: 404 Not Found - http://localhost:8080/jirarequest?OK"
The URL i am using in browser is http://localhost:8080/Jira-Synchronization/index.html. i have double checked all kind of typo. I debugged by putting breaking point but the debugger never hit my Servelt. I am not sure what is wrong? URL is wrong or wiring is wrong or the way it set up something is missing?
Some info:
The 404 error means Tomcat doesn't have any file or servlet or anything exposed for serving at that URL path.
The 500 error means Tomcat has a servlet (or jsp) exposed at that URL path and an unexpected exception was generated while trying to run the java code.
Collection of things we know need to be changed:
$.get('/jirarequest/whatever','OK',function(op){
and
<servlet>
<servlet-name>JiraSyncServlet</servlet-name>
<servlet-class>JiraSyncServlet</servlet-class>
</servlet>
Plus you can test in the browser by typing this URL which should return OK:
http://localhost:8080/Jira-Synchronization/jirarequest/whatever?OK
HTTP Status 404 - /Project/
type Status report
message /Project/
description The requested resource (/Project/) is not available.
Apache Tomcat/7.0.16
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Project</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>
this is the autogenerated web.xml in eclipse
You have not defined a servlet and its mapping in your web.xml so that the server can call its doGet or doPost method when any request comes in for your servlet.
Add this in your web.xml
The comments describe each tag usage. You can remove the part <!-- ..... --> from each tag
<servlet>
<servlet-name>myservlet</servlet-name> <!-- You can give any name -->
<servlet-class>com.abu.MyFirstServlet</servlet-class> Your servlet class's fully qualified name
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name> the name that you mentioned in the above tag
<url-pattern>/index.html</url-pattern> the url for which the request must be sent to the servlet.
</servlet-mapping>
This means that whenever you type http://localhost:8080/YourPorject/index.html then the conatiner will call your servlet's doGet or doPost method. You should have a look at this wiki page for gaining basic knowledge about servlets
you need to put one file in root directory from above list.