File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Help needed to 'Forward' Servlet to Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Help needed to Watch "Help needed to New topic
Author

Help needed to 'Forward' Servlet to Servlet

Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Dear all,

I am trying hard in my code to call one servlet from another servlet, but I have no idea what's wrong with my code that I kept getting the following error message:

HTTP Status 405 - HTTP method POST is not supported by this URL
--------------------------------------------------------------------------------
type Status report
message HTTP method POST is not supported by this URL (http://localhost:8080/helloservlet/control)
description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).


What I am trying to do is to call a servlet (ControlServlet) from my html form, and then ControlServlet is to call the EasyQnServlet when a user selects the 'Easy Question' radio button on my webpage. If the user selects 'Hard Question' radio button, the ControlServlet should call the HardQnServlet.

In a nutshell, the path is like this: HTML form > ControlServlet > EasyQnServlet / HardQnServlet

I am trying to use the forward function in my ControlServlet code (shown below):



My web.xml code:


I use doPOST in both my EasyQnServlet & HardQnServlet.
In my html form, I use <form method = "POST" action="control">

(Using doGET in all servlets give me the same error as well)

Can anyone kindly advise me on how can I solve this problem?

An advance thanks to all.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

Look at your servlet. Really look at it. Hint: line 3.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
It is exactly the same as what I read from a 'forward servlet' tutorial, except for the name of the servlet. As for the web.xml, I wrote it myself.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

You're not looking hard enough at line 3. If it matches the tutorial, throw the tutorial away as it is useless.

Here's another hint: Java is case-sensitive
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:You're not looking hard enough at line 3. If it matches the tutorial, throw the tutorial away as it is useless.

Here's another hint: Java is case-sensitive


Yeah Bear Bibeault, you are right, Java is case-sensitive. I have edited the doPOST to doPost. The previous error is resolved but another error surfaces - Error 404 (no details shown). Is my forward servlet code in the core correct?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

What happened when you tried to hit the servlets directly using the browser?
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:What happened when you tried to hit the servlets directly using the browser?


Everything was working fine when I call the EasyQnServlet directly from my webpage without the ControlServlet 'in between'. However, Error 404 (no details shown) surfaces when I add and call ControlServlet, which has the code attached above. I have no idea if it is the coding in the ControlServlet, or the mapping in the web.xml that is wrong.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

Bear Bibeault wrote:What happened when you tried to hit the servlets directly using the browser?
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:What happened when you tried to hit the servlets directly using the browser?


When I tried to invoke the ControlServlet with http://localhost:8080/servlet/ControlServlet, Error 404 appears with no details shown.
The same thing happened when I invoked EasyQnServlet with http://localhost:8080/servlet/EasyQnServlet.

But is it supposed to show something since my servlets has to receive users inputs (get.Parameter) before it can output anything?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

Apple Khoa wrote:
When I tried to invoke the ControlServlet with http://localhost:8080/servlet/ControlServlet, Error 404 appears with no details shown.

Not surprising as that doesn't match any of your servlet mappings. You can't just make up URLs. You have to use the actual mappings.
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:Not surprising as that doesn't match any of your servlet mappings. You can't just make up URLs. You have to use the actual mappings.


Actual mappings? Is it like this (my modified web.xml):



Error displayed when I invoke the servlets using http://localhost:8080/servlet/EasyQnServlet:

HTTP Status 404 - /servlet/EasyQnServlet
--------------------------------------------------------------------------------
type Status report
message /servlet/EasyQnServlet
description The requested resource (/servlet/EasyQnServlet) is not available.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

I do not see the string "/servlet" in your mappings. Why is it in your URL if it's not in your mapping?
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:I do not see the string "/servlet" in your mappings. Why is it in your URL if it's not in your mapping?


I learnt from websites that I can invoke servlet from browser using http://machine-name:port/servlet/servlet-name. Inserting "/servlet" in my mappings doesn't help too, producing the same error.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

You learned wrongly.

You use the mapping. Forget about the silly "/servlets" string.
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:You learned wrongly.

You use the mapping. Forget about the silly "/servlets" string.


What's wrong with my mapping?


along with my ControlServlet code:


The html page, servlets are all in the same folder (Apache tomcat > webapps > helloservlet)

HTML in Apache tomcat > webapps > helloservlet
Servlets classes in Apache tomcat > webapps > helloservlet > WEB-INF > classes > simuleqn
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3829

What happens if you ignore the "servlet" String from the URL in the browser?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed to 'Forward' Servlet to Servlet