1
votes

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.

4
What behavior do you actually see? Do you receive a 400? For what URL? - Sotirios Delimanolis
For " file:///C:/servlets/MyFirstServlet " URL I am getting Web page is not found . - StrugglingCoder
That is not normal. How do you access the first page on your website? The one rendered by the JSP. - Sotirios Delimanolis
You should run using this Welcome_2.html page on the browser , as you invoke the servlet from here - Santhosh
@user3655102 I am 100% sure that you are submitting the form via server.You are opening the html page in browser and hitting submit thats why file:/// appears. - SpringLearner

4 Answers

7
votes

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 !!

2
votes

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" >
1
votes

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

<url-pattern>/servlets/First</url-pattern>
1
votes

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">