Demo simple Ajax ans Servelt implementation fails - java

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

Related

Http status 404 not found error in eclipse

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>

Java EE 404 Error on XHTML but Not HTML

I'm learning Java EE through a simple "Hello World" application, and when I run the program using index.html it works well. However, when I run the same program with index.xhtml, it throws a 404 Error saying "The requested resource is not available."
My folder structure is as follows:
When I run the program using index.html, I use the following URL: http://localhost:8081/index.html, and the page shows up with Hello World. And when I run using index.xhtml, I tried both: http://localhost:8081/example2/index.xhtml and http://localhost:8081/index.xhtml. Both give a 404 Error.
My web.xml is as follows:
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
I've also tried multiple browsers (Chrome and Firefox). Any ideas why I'm getting a 404 on XHTML but not HTML?
You can config the home page in the web.xml with the code below:
<display-name>NameOfProject</display-name>
<!-- Configuration of your home page -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
So you can use both:
http://localhost:8081/NameOfProject/ or
http://localhost:8081/NameOfProject/index.xhtml
The solution was pretty simple. When I checked the Tomcat logs, there was a ClassNotFoundException because Tomcat does not come with the jsf-api and jsf-impl jars. I created a new directory in WEB-INF called lib and put the 2 jars there. Yes, you need both jars. Now the XHTML file is found and the 404 goes away.

index.html ignored or default servlet got priority over index.jsp and index.html? Why?

web.xml
<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>TestFilter1</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>ser1</servlet-name>
<servlet-class>com.gaurav.test.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ser1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.html
<body>
HelLoSTHTML
</body>
index.jsp
<body>
HelLoSTJSP
</body>
Hello.java,implementing javax.servlet.Servlet
service method
PrintWriter out=paramServletResponse.getWriter();
out.println("HelloTextStart");
out.println(config);
out.println("HelloTextEnd");
out.close();
deployed on jboss-5.1.0GA
now cases
**requesting
/TestFilter1/
**
showing
"HelloTextStart
org.apache.catalina.core.StandardWrapperFacade#1a878065 HelloTextEnd "
but not showing
"HelLoSTHTML"
**requesting
/TestFilter1/index.html
** then also
showing
"HelloTextStart
org.apache.catalina.core.StandardWrapperFacade#1a878065 HelloTextEnd "
but not showing
"HelLoSTHTML"
**requesting
/TestFilter1/index.jsp
**
showing
"HelLoSTJSP"
So what is the order of processing/prioritizing this request?
(html,jsp.Servlet ser1)
Your application works as configured. You tell to your webapp :
When you see request coming send them to the servlet class com.gaurav.test.Hello
Conf:
<servlet>
<servlet-name>ser1</servlet-name>
<servlet-class>com.gaurav.test.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ser1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
So this servlet is responding to your request.
I think want you need is to configure the servlet javax.servlet.Servlet
I hope this will help
we can configure the URL-patterns in 3 ways
1) Absolute/Exact Matching (Ex: <url-pattern>/test1</url-pattern>)
2) Extension Matching (Ex: <url-pattern>*.do</url-pattern>)
3) Directory Matching (Ex: <url-pattern>/abc/*</url-pattern>)
<url-pattern>/</url-pattern> means Every Request go to Hello.java
First it will check Exact matching then Extension there after Directory Matching.
Above code for every request is going to Hello.java.
go through this link you will get clarity which one will be given priority in Tomcat 6: index.html or index.jsp?

How to configure the Project ( depends on maven/web.xml/servlet APi) AJAX call not to include the Project name in it?

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

appengine java development server displaying source code

When I access a jsp page like this on an appengine development server:
localhost:8888/index.jsp/
it's displaying the source code of index.jsp in the browser. if you access without the trailing slash (i.e. index.jsp) then it renders jsp but with the trailing slash (i.e. index.jsp/) it displays the source code
Any idea why is this? and how to fix it?
It seems to happen only in development server and not in production. Production gives a 404 Not Found error, which is fine.
I am using SDK 1.6.4
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>RegisterPage</servlet-name>
<jsp-file>/register.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>RegisterPage</servlet-name>
<url-pattern>/signup</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
==========
so...
index.jsp -> renders page
index.jsp/ -> returns source code
register.jsp/ -> returns source code
register.jsp -> renders jsp
signup/ -> renders register.jsp
signup -> renders register.jsp
so it seems like it's the urls with *.jsp/ that have the issue
You should move all the *.jsp files into the /WEB-INF directory, and update your web.xml.
This way the *.jsp files will not be accessible directly, and the source code will be hidden.
<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>RegisterPage</servlet-name>
<jsp-file>/WEB-INF/register.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>RegisterPage</servlet-name>
<url-pattern>/signup</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>IndexPage</servlet-name>
<jsp-file>/WEB-INF/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>IndexPage</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index</welcome-file>
</welcome-file-list>
I have the some problem when i have used "redirect" and apache tomcat 7, because the redirect is not supported in new version of apache. For solve your problem search news in changelog of your version of apache (if you use it) for the tag you used in your page, or publish code of your page to be able to suggest other solutions. May be that you're using deprecated tags. Also, keep in check the file localhost[DATE].log for more details

Categories