Jason Crosby
Jason Crosby

Reputation: 3573

Eclipse Web Project Request URL

I am trying to create a new dynamic web project in eclipse. I add a servlet and some basic html code to get it up and running. However I have one problem. When I make a get request via jquery using the url "/news" I get a 404 not found error.

$.get( // resulting url http://localhost:8080/news
  "/news",
  function(data) {
    alert('page content: ' + data);
  }
, "html");

When I check the full request URL I see http://localhost:8080/news. Makes sense because the project name should be in the url as well. So when I pass "project-name/news" into the get request I still get a 404 not found error.

$.get( // resulting url http://localhost:8080/project-name/project-name/news
  "project-name/news",
  function(data) {
    alert('page content: ' + data);
  }
, "html");

And this time the full request url shows http://localhost:8080/project-name/project-name/news... When I manually make a request to http://localhost:8080/project-name/news it works just fine. So why is eclipse adding the project name to the url?

Servlet declarations

<servlet>
  <servlet-name>NewsServlet</servlet-name>
  <servlet-class>com.crosbygames.servlet.NewsServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>NewsServlet</servlet-name>
  <url-pattern>/news</url-pattern>
</servlet-mapping>

Upvotes: 0

Views: 1017

Answers (1)

sendon1982
sendon1982

Reputation: 11244

When you start with "/news" in the request URL, it will start as http://localhost:8080/news.

But if you use "news" (no slash "/"), it will start as http://localhost:8080/<content path>/news.

In your case, you are using project-name/news in your request URL, then resulting URL will be http://localhost:8080/project-name/project-name/news where project-name is your context path in your example.

Upvotes: 1

Related Questions