0

I cannot figure out why my servlet isn't mapped corectly.

This is part of the web.xml:

<servlet>
  <servlet-name>InsertServlet</servlet-name>
  <servlet-class>servlets.InsertServlet</servlet-class> 
</servlet> 

<servlet-mapping>
   <servlet-name>InsertServlet</servlet-name>
   <url-pattern>/insert</url-pattern> 
</servlet-mapping>

This will generate: localhost:8080/GestiuneSimpozioane/jsp/insert (Because the form where I send the data is located in a jsp folder) Instead I need: localhost:8080/GestiuneSimpozioane/insert

How shall I modify the mapping ? Thanks!

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
user1391078
  • 53
  • 1
  • 2
  • 9

2 Answers2

0

What do you mean, "that's what it will generate"?

Use an absolute, not relative, path in the form. You should probably use JSTL's <c:url> tag to automagically include the context etc., too.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

Your problem is not in the servlet mapping. Your problem is how you specified the servlet URL in the HTML form. Given the symptoms you have used

<form action="insert">

Relative URLs in HTML (i.e. ones not starting with a scheme or a /) will be resolved relative to the URL of the requested HTML page (as appears in the browser's address bar). This is in your case apparently in the /jsp subfolder. The browser will then of course interpret the URL being in the /jsp folder.

You need to specify a domain-relative URL instead.

<form action="${pageContext.request.contextPath}/insert">

The ${pageContext.request.contextPath} will dynamically print the context path and hence the HTML will in your case be generated as follows:

<form action="/GestiuneSimpozioane/insert">

You can also achieve this with the <base> tag. See also this related answer.

If you really need the servlet to listen on /jsp/insert instead for some reason, then just alter the <url-pattern> accordingly. Don't forget to take this into account in the HTML <form> as well.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555