• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code.

Your action URL is wrong. It must start with the context path. For example:
 
Dester Raja
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I changed my jsp code . I am still getting the error . Please help..
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the HTML at the browser. Does the form tag look correct?
 
Dester Raja
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried using action="custTest/LoginServlet" removing the "/" before custTest in action..This does not help either.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you place the servlet's class file in the correct location?
 
Dester Raja
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,. the servlet class is inside the build directory in the same package structure as defined in the web.xml...
 
Greenhorn
Posts: 12
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this

 
Dester Raja
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using any IDE ?

 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .
 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dester Raja
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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





 
reply
    Bookmark Topic Watch Topic
  • New Topic