servlet web.xml mapping - servlets

I am working on simple web application using Servlet & Jsp. But i have
one problem where i am working on web.xml. Can i use same url pattern
for many servlet class for example
code from jsp page
<form action="Answer" method="get">
<input id="foo" type="text" name="question"/>
<input type="reset" value="Clear" />
<br/>
<input type="submit" value="Submit"/>
<input type="submit" value="Back"/>
</form>
Code from web.xml
<servlet>
<servlet-name>Answer</servlet-name>
<servlet-class>RemoveAbbr</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Answer</servlet-name>
<url-pattern>/Answer</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Answer</servlet-name>
<servlet-class>Preprocess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Answer</servlet-name>
<url-pattern>/Answer</url-pattern>
</servlet-mapping>
So, My question is what are true conditions from below options 1) if
i click on Submit button can it map to RemoveAbbr 2) if i click on
Back button can it map to Preprocess 3) creates conflict because same
url pattern(Form Action from jsp) can not used for different servlet
class name.

Every <servlet> needs to have a unique <servlet-name>, so you will need to rename one. Also, the <url-pattern>'s should be different so the servlet container will know how to handle the requests.

You need to use a unique <servlet-name>. For example:
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.myapp.FirstServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>com.myapp.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/first.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/second.do</url-pattern>
</servlet-mapping>
Check this tutorial, there I found an example to edit web.xml file and add the servlets. Also the source code is available, so it was useful for me.

Related

Web XML and URL pattern mapping for servlets

I have a form action mapped to "loginURL" and my servlet url pattern
mapped as "/loginURL" However on click of submit, my application URL
has "/login". Why? And i get a 404 as expected. I am learning HttpServlets and that is when I hit this issue.
Web.xml
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.learning.request.dispatch.LoginRequestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginURL</url-pattern>
</servlet-mapping>
<servlet>
HTML Form
<form action="loginURL" method="post">
Name:<input type="text" name="userName"/><br/><br/>
Password:<input type="password" name="password"/><br/><br/>
<input type="submit" value="login"/>
</form>
Sometimes 404 will throw due to the incorrect syntax in web.xmI. Do check the syntax!

how can i change my url-pattern in servlet mapping without facing 404 [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 5 years ago.
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
when I use /go as url-pattern ,it is working .
when I alter it to some other name it is not,like/servletgo.
HOw can I alter it .xml file?
I got the answer..
whatever the 'action' we wrote in html file is to be write in url-pattern which is under .xml file.
for example:
my html code:
<html>
<body>
<form action="welcome" method="post" enctype="multipart/form-data">
Select File:<input type="file" name="fname"/><br/>
<input type="submit" value="upload"/>
</form>
</body>
</html>
</html>
and my servlet code:
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/welcome</url-pattern> //we have to use same pattern what we noted in html action.If we change the action name in html,then only we can change the url- pattern.
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>

why should we include <servlet-name> in deployment descriptor file

i have tried without giving servlet-name tag in my web.XML but it did not worked for me.
we are adding servlet-class & url tags in deployment descriptor file here what is the exact use of adding servlet-name tag in deployment descriptor file (web.XML).
Cant we run without servlet-name tag in our web.xml
The <servlet-name> is a unique identifier for a single Servlet. Some deployment descriptor elements some times need to refer to a specific servlet. They do that through the value of the <servlet-name>. For example, you might have multiple <servlet-mapping> elements, one for each extension. You might also have a Filter which you want applied only to a specific servlet.
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.pack.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.pack.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<servlet-name>myServlet</servlet-name>
</filter-mapping>

web app web.xml error [duplicate]

This question already has answers here:
Getting error: The content of element type "web-app" must match,
(3 answers)
Closed 5 years ago.
I am getting an error in my GWT application being developed in Eclipse. It's in the web.xml file. Here's the error:
The content of element type "web-app" must match "(icon?,display- name?,description?,distributable?,context-
param*,filter*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-
file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config?,security-
role*,env-entry*,ejb-ref*,ejb-local-ref*)".
I have seen numerous posts about this and the problem is the order of the elements of the file, but that fix doesn't work for me (I have also tried putting all the <servlet-mapping> tags right after the corresponding <servlet>, it did not work either)
My web.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>com.yachtcloser.server.DispatchServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.yachtcloser.server.UploadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.yachtcloser.server.DownloadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.yachtcloser.server.LoginServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/dispatch.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Yc.html</welcome-file>
</welcome-file-list>
</web-app>
Are there any other ways of tracking this error; other files that are linked to this?
well, as per new format of DTD web-app tag might contains following tags.
<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, filter*, filter-mapping*, listener*, servlet*,
servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,
error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,
login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>
above mentioned icon, display-name, description, distributable....etc are in the same order as they have mentioned in the DTD file.
e.g. if you put description tag before display-name it gives error.
I deleted the file and pasted the text from the old one into a new file with the same name and now there's no errors.
Just for a reference: A SelectAll->Cut->Save->Paste->Save also fixes the problem. Probably there is a line ending character issue.
I followed the suggestion for "copy all" - "cut" - "paste" - "save" and this seemed to clear up the message. I found that in the "pasted" version all tabs had been converted to spaces.
So it seems that the web.xml validator in Eclipse does not like tabs.
The error itself gives you the clue. The order of the elements in your web.xml should follow the order specified in the error.
<displayname>
</displayname>
<description>
</description>
....... like this the elements should be in order as it says in the error.

Configuring web.xml for webservices and servlet

I am new to Restlets. Trying to configure the web.xml (on JBoss). I have 2 entries, one for a servlet (got nothing to do with webservices) other for webservices, using Restlet. Here are the entries..
<servlet>
<servlet-name>AuthenticationServlet</servlet-name>
<servlet-class>com.safeid.web.server.api.servlet.AuthenticationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AuthenticationServlet</servlet-name>
<url-pattern>/authenticate/*</url-pattern>
</servlet-mapping>
<!-- Start of Entries for the REST Web Services. -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>com.safeid.web.server.SafeIDRouterApplication</param-value>
</context-param>
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>com.noelios.restlet.ext.servlet.ServerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- END of Entries for the REST Web Services.-->
Both don't work together. In the above setup the Restlet works. However when I change the
RestletServlet
/*
to something like
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/credential/*</url-pattern>
</servlet-mapping>
the Restlet stop working and the AuthenticationServlet works fine. What am I missing here?
I had a similar frustration. Perhaps what I found out may help.
I had Router entries in my Application class like this:
router.attach("/users", UsersResource.class);
And things worked fine when my servlet mapping was like this:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
When I changed it to something like this:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/users/*</url-pattern>
</servlet-mapping>
it stopped working.
The problem is that the servlet container "consumes" or removes the part of the URL that it matched. In this case, it removes "/users". So if you were using a url like this:
http://www.mywebsite.com/users
you would have to change it to be:
http://www.mywebsite.com/users/users
Of course, you can make the url-pattern be whatever you want:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
and then you'd access it like this:
http://www.mywebsite.com/rest/users
The url-pattern gets stripped off, and you get whatever is leftover in your Application class for your own routing purposes.
HTH
Looks like you're missing the init-params as in the example below.
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>my.class.that.extends.Application.MyApplication</param-value>
</init-param>
</servlet>
You need a class that extends org.restlet.Application (at least in Restlet 2.0 anyway).

Resources