Forums Register Login

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

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
Please UseCodeTags when posting code.

Your action URL is wrong. It must start with the context path. For example:
+Pie Number of slices to send: Send



I changed my jsp code . I am still getting the error . Please help..
+Pie Number of slices to send: Send
Check the HTML at the browser. Does the form tag look correct?
+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
I also tried using action="custTest/LoginServlet" removing the "/" before custTest in action..This does not help either.
+Pie Number of slices to send: Send
Did you place the servlet's class file in the correct location?
+Pie Number of slices to send: Send
yes,. the servlet class is inside the build directory in the same package structure as defined in the web.xml...
+Pie Number of slices to send: Send
try this

+Pie Number of slices to send: Send
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 ?





+Pie Number of slices to send: Send
Are you using any IDE ?

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send

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 .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

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 ?
+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
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:




+Pie Number of slices to send: Send


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





I want my playground back. Here, I'll give you this tiny ad for it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12082 times.
Similar Threads
Help me please
HTTP Error 404 resource not found
Newbie here tyrint o understand Servlets
Can not run servlet in Eclipse Europa
Servlet not dircting to the next page
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:02:52.