-1

I am new to Servlets. I made a simple project with 2 jsp files. and two Servlers jsp1, jsp2, servlet1 and servlet2.

When the code in both jsps' body tags is this (Both points to servlet1 )

<form method="post" action="servlet1">
<input type="submit">
</form>`

and There is a simple Sysout in the servlets' doPost method..

It works perfectly fine in this scenario.

But when I make the second jsp call servlet2. It doesn't work. It gives me this error screen

HTTP Status 404 - /Project/servlet2 type Status report message /Project/servlet2 description The requested resource (/Project/servlet2) is not available. Apache Tomcat/6.0.26

So can't we use two different servlets in same project?

EDIT:

This is the web.xml file. As you can see. All mappings are there.

'

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Project</display-name>
  <welcome-file-list>
    <welcome-file>jsp1.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>servler1</display-name>
    <servlet-name>servler1</servlet-name>
    <servlet-class>servler1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servler1</servlet-name>
    <url-pattern>/servler1</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>servler2</display-name>
    <servlet-name>servler2</servlet-name>
    <servlet-class>servler2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servler2</servlet-name>
    <url-pattern>/servler2</url-pattern>
  </servlet-mapping>
</web-app>

'

Napstablook
  • 594
  • 1
  • 6
  • 24
  • 1
    have you added entry in web.xml – kirti Nov 06 '14 at 07:34
  • 1
    Yes, you can. Where is the code and the mapping? – JB Nizet Nov 06 '14 at 07:34
  • Please check the question. I just edited with web.xml content. Mappings are all fine, I guess. – Napstablook Nov 06 '14 at 07:43
  • @Ramandeep Can you tell me your second servlet name? – Yubaraj Nov 06 '14 at 08:45
  • I guess I found the solution but I would still need some explanation. I made another similar project with servlets in com.Project.servlets package. and both JSPs in a JSP directory in WebContent. Web.xml has default.jsp as welcome page and that page forwards to jsp1 and hence when I call servlet1 from jsp1. It works fine. But when I try for jsp2 by typng the url pointing to jsp2 and calling servler2 from it. It cannot find it as it is searching it in JSP directory only. So when I change the action attribute to "../Servlet2" It works. So it has something to do with JSP forward tag. – Napstablook Nov 06 '14 at 09:55
  • Now I have serched about JSP forward tag. It sounded to me like Server.transfer (in ASP- I used to code in ASP earlier) i.e Client is never informed about the transfer to another page. But I still cannot understand how it impacted here. I would be glad if someone could explain to me. – Napstablook Nov 06 '14 at 09:58

2 Answers2

2

You might not have mapped the second servlet in the WEB.XML. Check for the entry else create your own like ,

<servlet>
    <servlet-name>Second</servlet-name>
    <servlet-class>path.SecondServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Second</servlet-name>
    <url-pattern>/SecondServlet</url-pattern>
</servlet-mapping>

Check what Oracle says before configuring your web.xml

Also read my answer on this How to map a servlet call from a JSP page using form action?

Update :

Your 2.jsp should map the action attribute as ,

<form action="./servler2" method="post">
Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
1

I am agree with the answer of @San Krish. But little more explanation.

As you said:

Second servlet in the project not working.

You need to map every servlet in web.xml file like you are mapping first servlet.

EDITED:

From your edited question. you are using servler1 in your web.xml file but in jsp using servlet1 so this is the main issue. So use servler1 in your jsp file.

Eg:

<form method="post" action="servler1">
    <input type="submit">
</form>`

EDITED1: There may be another reason.

If your second servlet is inside package you must be included package name also in web.xml. Suppose your second servlet servlet2.java inside com.myproject.test package you need to do like following in web.xml file.

<servlet>
    <description></description>
    <display-name>servler2</display-name>
    <servlet-name>servler2</servlet-name>
    <servlet-class>com.myproject.test.servlet2</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servler2</servlet-name>
    <url-pattern>/servler2</url-pattern>
</servlet-mapping>
Yubaraj
  • 3,800
  • 7
  • 39
  • 57