aspose file tools*
The moose likes Servlets and the fly likes HTTP Status 404 Error - error while calling a servlet from a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTP Status 404 Error - error while calling a servlet from a jsp" Watch "HTTP Status 404 Error - error while calling a servlet from a jsp" New topic
Author

HTTP Status 404 Error - error while calling a servlet from a jsp

Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17
JSP Code

<body>

<form action="Test" method="post">
This is the login jsp
<input type="text" name="username" value=""> username
<input type="password" name="password" value="" > password
<input type="submit" name="submit" value="click"></input>


</form>
</body>
---------------------------------------

web.xml


<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.customer.servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>

----------------------------------

*/
public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Inside Post ");
}
}

----------------------------------------------

When i run the jsp , and click on submit button , I am getting the 404 errorr , the url is displayed as "http://localhost:8100/LoginServlet"
But the correct url should be http://localhost:8100/custTest/LoginServlet ...where should I make this change so that i dont get the error and the servlet gets invoked ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Please UseCodeTags when posting code.

Your action URL is wrong. It must start with the context path. For example:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17



I changed my jsp code . I am still getting the error . Please help..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Check the HTML at the browser. Does the form tag look correct?
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17
Yes . Form tage looks like . I am using the JSP editor on my workspace. There are no errors and no warnings on my jsp ..



My project name is custTest . In the server.xml the contextpath = "/custTest" . Where should i check what is my context path ?
Still I am getting the 404 error ?
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17
I also tried using action="custTest/LoginServlet" removing the "/" before custTest in action..This does not help either.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Did you place the servlet's class file in the correct location?
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17
yes,. the servlet class is inside the build directory in the same package structure as defined in the web.xml...
Sanjeev Kumaar
Greenhorn

Joined: Jun 10, 2010
Posts: 12

try this



SCJP 5, SCWCD
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17
Thanks. But I get a new error .

I have been trying hard to call a servlet from a jsp.. I have not been able to do it.. Please help ...
Got the below when i click submit on the jsp page. I find the servlet class in the same pakcage (directory) as the source java file. But the server does not picke up this class file .. did i miss any configuration ?





Sanjeev Kumaar
Greenhorn

Joined: Jun 10, 2010
Posts: 12

Are you using any IDE ?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Sanjeev Kumaar wrote:try this


NO! The server-relative path using the context path is the correct way. Using page-relative addressing is a poor practice that is very fragile.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Dester Raja wrote:yes,. the servlet class is inside the build directory in the same package structure as defined in the web.xml...

Show, don't tell. Where is the file? And be sure to put the action URL back to the correct one that I posted.
Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
Try:

renaming the url mapping from /Test to /LoginServlet in your web.xml.

If you are using the absolute path, you probably need to use the actual servlet name vs. the url mapping because you are outside the context path.

I would recommend using all jsp navigation through servlets (request dispatcher) that way you stay within the context path and can use web.xml mappings.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Ron Ingram wrote::renaming the url mapping from /Test to /LoginServlet in your web.xml.

What would that accomplish?

If you are using the absolute path, you probably need to use the actual servlet name vs. the url mapping because you are outside the context path.

Huh? "outside the context path"? There's no such thing. Anything within the web application is by definition "inside the context path".

The servlet name should not be used; that's what mappings are for.

It's still unclear what the actual issue is because the OP hasn't been very forthcoming with info.
Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
Well, I had a very similar issue before and realized if you navigate from jsp to jsp using href etc, without going through a controller servlet, the web.xml mappings are broken and you must use the absolute path to servlets. In the initial post, the OP is trying to use the web.xml mapping. I wasn't sure if this was the same issue I had, but for lack of better terms, the web.xml controls the context of your web app and if that gets broken and you must use absolute mappings.. that also conflicts with the MVC architecture as some argued to me.. maybe i'm wrong

If the original url mapping set forth in the web.xml file wasn't working, mapping to it using an absolute path will probably not generate the desired results as well.
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17
I wanted to get started with web programming... I tried creating a new workspace, new project , new servlet and a new jsp .. Looks like I am stuck with the same error I got yesterday.
I am getting the error - java.lang.ClassNotFoundException: javax.servlet.Servlet

I pasted all the files that i have on my workspace below . Please help me resolve this error..



Project Name : custASG.

Servlet Code




JSP Code : LoginPage.jsp




Web.xml



Default Output folder of my project : custASG/src

I am able to see the LoginServlet.class very next to the LoginServlet.java . They both are in the same package when I see in the Resource Perspective
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17

I am using tomcat v6.0. the modules that has been configured are

Path : /custASG
Docucument base : custASG

Path : /
Docuemnt base :C:\work\custASG\custASG\web

let me know if you need more info .
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 530
    
    2

Have you checked your war? Did it contain all the files (not what is shown in IDE), the actually deployable war. Are you sure, you have all the classes inside WEB-INF/classes

Also, see what does it print for ${pageContext.request.contextPath}. Do you actually see the servlet being hit when you type http://localhost:8100/custTest/LoginServlet in browser.


Regards
KumarRaja

Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17

the url for my servlet is : http://localhost:8200/custASG/LoginServlet
When i tried accessing this url directly on a web browser , I was gettign the same exception.
Also when i made some changes to jsp file , rebuild my project and reatarted the server, the server is still picking up the old jsp.
any idea why this could hapen and why my servlet is not getting picked up ?
Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17
I deleted the server and created a new one ....I got the jsp changes to work ... whatever changes i make on the jsp , its getting picked up the next time i restart the server ,,but when i click on the submit button on jsp , the url on the browser is

http://localhost:8200/LoginServlet

i tried copying both the below urls to a browser and acessing the servlet.. both cases i got the exception.

http://localhost:8200/LoginServlet
http://localhost:8200/custTest/LoginServlet


Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
In your LoginPage.jsp, your form action should be :


Also, your web.xml had to be corrected. If you are only using LoginPage.jsp, then your <welcome-file-list> needs to include <welcome-file>LoginPage.jsp</welcome-file>

I've re-created your test servlet and tested it to ensure it is functioning properly.. Please review notes.. good luck

web.xml:


LoginServlet:



Jsp Files:



JSP File 2:




Dester Raja
Greenhorn

Joined: May 22, 2011
Posts: 17


Thanks Ron, You are the Man !...
I finally got this to work .....





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTTP Status 404 Error - error while calling a servlet from a jsp