1

I have a JSP page named Welcome_2.html HTML page directoryand in its form action I have invoked a servlet like this :

<form action="/servlets/MyFirstServlet" method="post" id="form_id">

The servlet "MyFirstServlet" is under servlet directory WEB-INF classes servlets MyFirstServlet

and the jsp is under the folder HTML which is in the same level like WEB-INF

i.e. inside practice I have 3 foldersproject structure HTML META-INF WEB-INF

in web.xml I have the following snippet

    <servlet>
    <servlet-name>MyFirstServlet</servlet-name>
    <servlet-class>servlets.MyFirstServlet</servlet-class>
</servlet>  
<servlet-mapping>
    <servlet-name>MyFirstServlet</servlet-name>
    <url-pattern>/servlets/MyFirstServlet</url-pattern>
</servlet-mapping>

Why the servlet is not being invoked? I am clicking on the HTML page on my browser and trying to invoke the servlet ... I am just a beginner pardon me for my poor intellect.

StrugglingCoder
  • 4,781
  • 16
  • 69
  • 103

4 Answers4

7

Change your jsp form to ,

<form action="/servlets/MyFirstServlet" method="post" id="form_id">

to match the url pattern in your web.xml

<servlet-mapping>
    <servlet-name>MyFirstServlet</servlet-name>
    <url-pattern>/servlets/MyFirstServlet</url-pattern>
</servlet-mapping>

This line <url-pattern>/servlets/MyFirstServlet</url-pattern> refers that url's matching the pattern will invoke the MyFirstServlet

Read the Oracle Tutorial before you configure your web.xml elements

Hope this helps !!

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • I changed accordingly ( and edited my post also,please see). But still it is shown web page not found .... and the URL is getting loaded as file:///C:/servlets/MyFirstServlet .... Webpage is not found. – StrugglingCoder May 20 '14 at 06:03
  • Can you post your project structure , instead of explaining it by words ? – Santhosh May 20 '14 at 06:08
  • @user3655102 I am just thinking out loud... if you can tell the first URL you are accessing that might help. i think you are just opening yout HTML by clicking on your HTML file instead of http://{serverURL}:{port}/{appliucation}/{myhtml.html} – ppuskar May 20 '14 at 07:01
2

If you use tomcat 7 , you don't need to care about that. For example :

In your servlet :

@WebServlet("/myFirstServlet")  

public class LoginPage extends HttpServlet {

   // your code 

}

In your html :

<!-- here you write myFirstServlet in the action tag -->
<form id="somethingGoesHere" action="myFirstServlet" method="post" >
James
  • 85
  • 1
  • 1
  • 8
  • Are you sure that `tomcat` supports annotations despite the servlets version – Santhosh May 20 '14 at 06:11
  • @sankrish: Yes, Tomcat 7 supports annotations . Read here `http://stackoverflow.com/questions/6535676/webservlet-annotation-with-tomcat7` – James May 20 '14 at 06:15
1

As your form action is "/servlets/First" so your url pattern should be

<url-pattern>/servlets/First</url-pattern>
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • 1
    This will also work but it makes more sense to change the form action instead of the mapping because on actual projects changing the servlet mapping can affect more components – RossBille May 20 '14 at 06:03
1

Unless your app is deployed as ROOT.war, all your URLs will be relative to http://myserver/webapp. So my guess is that you should rather use relative URLs. As your JSP is in HTML, you would need to write:

<form action="../servlets/MyFirstServlet" method="post" id="form_id">
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97