It's not a secret anymore!*
The moose likes Servlets and the fly likes adding a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "adding a servlet " Watch "adding a servlet " New topic
Author

adding a servlet

abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
i have deployed a servlet a servlet and run it in browser success fully
then i added an html form and it also added successfully
then i tried to connect it with a servlet that should receive data sent by previously added html form

but on clicking submit button this error comes every time
--------------------------------------------------------------------------------------
HTTP Status 404 - /roota/Myname

type Status report

message /roota/Myname

description The requested resource (/roota/Myname) is not available.
-----------------------------------------------------------------------------------------


here is that receiver servlet code




that html file.....




and now web.xml file ......







Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60058
    
  65

Your form action should be server-relative beginning with the context path and then followed by the servlet mapping.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
Your form action should be server-relative beginning with the context path and then followed by the servlet mapping.



i m new with this .....please explain this with an easy example .. if possible
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60058
    
  65

action="/contextpath/servletpath"
Lalit Jugran
Greenhorn

Joined: Jan 05, 2011
Posts: 6
Hi abhay jain,

make following changes to your program

1- in html replace <form method ="get" action="Myname"> with <form method ="get" action="ala">
2- in web.xml replace <servlet-class>Welcomserv</servlet-class> with <servlet-class>Myname </servlet-class>

hope your program will run correctly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60058
    
  65

Lalit Jugran wrote:make following changes to your program .... action="ala"

No. No. No. No. No.

Use the format of the action I advised. Anything else is folly.

abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
sure..... but actually i m still confuse with terms "context path" and "servlet path"


here is the location of files in my PC

source code files are at this path ........


D:\project [no sub directies used]

and files after deployment are at this path ......

D:\Sun\AppServer\domains\domain1\applications\j2ee-modules\wara


wara directory contains contains META-INF, WEB-INF, welcomehtm.html



please help me in creating servletpath and contextpath
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I guess this was the issue with one of your previous posts as well.

your previous post

Here, from your post, your action name and url pattern doesn't match. same was the problem you mentioned in previous posts as well.

Ashwin Sridhar
SCJP | SCWCD | OCA
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
i have tried as you said my friend but unable to achieve any positive result (it may be my mistake in implementation of idea you gave ) but here is the current situation ..please give your opinion on this

html file ......




web.xml file .........


Lalit Jugran
Greenhorn

Joined: Jan 05, 2011
Posts: 6
Hi abhay jain

make changes what I posted in earlier postes. If your program still shows error message, post your folder structures of jsp & classes inside webapps folder.
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
abhay jain wrote:i have deployed a servlet a servlet and run it in browser success fully
then i added an html form and it also added successfully
then i tried to connect it with a servlet that should receive data sent by previously added html form

but on clicking submit button this error comes every time
--------------------------------------------------------------------------------------
HTTP Status 404 - /roota/Myname

type Status report

message /roota/Myname

description The requested resource (/roota/Myname) is not available.
-----------------------------------------------------------------------------------------


here is that receiver servlet code




that html file.....




and now web.xml file ......






Points to Remember regarding Servlets:

1. Servlets (.class) files should always be in a package. That is a undocumented requirement of a dynamic web application .
2. the action attribute of the form tag should always contain the url-pattern you mention in the web.xml. ala in your case.
3. The <servlet-class> tag should have the fully qualified name of your .class file which should be in classes folder.
<servlet-class>[some packagename].Myname</servlet-class>
do the following changes and your problem should get solved. Remember most of the time 404 errors come because of wrong/error-prone web.xml.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Had you made your Action name in html file to be same as url-pattern (case-sensitive), then your problem is solved.

if not, tell more about your folder structure
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
Ashutosh Limaye , Ashwin Sridhar, Lalit Jugran

thanks a lot friends

i have implemented as you all instructed
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60058
    
  65

The context path is the route to the web application. You can obtain it from the request. All URLs for actions or resource like images or script files should start with the context path.

The servlet path is the mapping that you assigned to the servlet in the deployment descriptor.

These are essential concepts for you to understand, and forming URLs any other way is going to be fragile and error-prone.
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
Well, you specifically left me off the list of people whose ideas you implemented.


the only problem is that i am new with Servlet concept, i have never used it in any way..that's why sometimes it takes too much time to implement a simple + effective idea... hope you understand ... no one would like to miss your precious opinion
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60058
    
  65

As someone new to the concepts it's important to learn the best practice ways of doing things. Just getting things to work isn't good enough, and taking shortcuts at the outsert just instills bad habits that are hard to break later.

I'm glad you want to do things the correct way.

In a JSP, another way to make sure that your URLs are correctly formed is to use the <c:url> JSTL tag to create the urls.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: adding a servlet
 
Similar Threads
please any one cleare my error
Problem calling a servlet from JSP
how to give an aliases to newly added servlet
Problem with simple HTML - Servlet login
Can't access HTML form page in browser