Reputation: 1279
I am receiving a 404 error (in the web browser console) upon sending a GET request from an html page via jquery to my java servlet .class file.
I honestly don't know what I should be doing after creating the core files in my IntelliJ maven-webapp project:
pom.xml, MyTestServlet.class, web.xml, index.html, do.js
I am building my project to a folder called "target" via:
Build > Build Artifacts >> All Artifacts > Build
And then, I take all the files in target and upload them to my department's server, but when I navigate to index.html and click the button, it returns a 404 error on the GET request instead of printing out the text specified in the servlet.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by isardar on 7/11/2017.
*
* GOAL FOR THIS TEST:
*
* make a servlet that sends data to client and client should receive/process the data
*
*/
//@WebServlet("/MyTestServlet") <-- idk what i'm doing
public class MyTestServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "some text boiii";
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write(text); // Write response body.
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>SO question 4112686 - Copied & Tweaked Version</title>
<link rel="stylesheet" href="style.css"/>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="do.js"></script>
</head>
<body>
<button id="somebutton">press here</button>
<div id="somediv"></div>
</body>
</html>
/**
* Created by isardar on 7/11/2017.
*/
$(document).on("click", "#somebutton", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
$.get("WEB-INF\\classes\\ServerTest.class", function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
$("#somediv").text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
});
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>MyTestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ServletTest4</groupId>
<artifactId>ServletTest4</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ServletTest4 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- a default maven-web-app dependency for some reason... -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>ServletTest4</finalName>
</build>
</project>
Can someone guide me as to what I should change to make this work, I feel I am quite close to a solution here? Also let me know if there are better ways about going about this problem, I have briefly heard of web servlet 3.0 but could not find any good resources on it for IntellliJ and Maven. Thanks!
Upvotes: 1
Views: 2436
Reputation: 1279
As CrazyCoder pointed out, my problem was at "jquery.get(..." in the "do.js" file. It should be:
$.get("WEB-INF\\classes\\MyTestServer.class"...
Instead of:
$.get("WEB-INF\\classes\\ServerTest.class"...
It's just a simple typo!
Upvotes: 0
Reputation: 1
Its something to do with your servlet not being identified when the server starts. It looks to be an issue related to Servlet may not be in right directory or package. Or you have to also provide package name for your servlet in "web.xml" file
Upvotes: 0
Reputation: 896
After you make sure your application is running under an application server (Tomcat, JBoss, etc). Your requests should be redirected to the correct path, which should be something like: localhost:8080/ProjectName/WebServletName.
You are using the get request to directly access the servlet file, but it works under the HTTP protocol. So it should be accessed in that way
Upvotes: 2