Web XML and URL pattern mapping for servlets - 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!

Related

Trailing slash at the end of Servlet uri in Websphere Application Server 9 is throwing 404, but it's working fine in WAS 7

In my application I have a servlet which is supposed to get executed after j_security_check in WebSphere Application Server Traditional 9.0.5.4. The Login form is as below with the j_security_check
<form method="POST" action="/myapp/testserv/j_security_check">
<table cellspacing="5" >
<tr><td align="right">User ID:</td><td><input type="text" name="j_username" size="30" value="${username}" id="username"></td></tr>
<tr><td align="right">Password:</td><td><input type="password" name="j_password" size="30" id="password"></td></tr>
<tr><td></td><td><input type="submit" value="SUBMIT"></td></tr>
</table>
</form>
in the Web.xml the servlet is configured as follows
<servlet>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.test.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/testserv</url-pattern>
</servlet-mapping>
After successful login the control is trying to hit the servet http://localhost:18380/myapp/testserv/ with the trailing "/" at then end of the URI and it's giving 404 not found Exception. I tried to add custom properties as per IBM documentation as follows , but no luck with those
com.ibm.ws.webcontainer.invokefilterscompatibility = true
com.ibm.ws.webcontainer.redirectcontextroot = true/false (tried with both true & false) under
Server Types -> Websphere Application Server -> -> Web Container Settings -> Web Container -> Custom properties
Interestingly the same code base is working as expected with trailing "/" in Websphere 7. Can anyone help me out with this issue.
Thanks

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>

Accessing static html file within Spring MVC

I have set up mvc:resources in my mvc--dispatcher-servlet.xml as below
<mvc:resources mapping="/resources/**" location="resources/" />
<mvc:resources mapping="/favicon.ico" location="resources/images/favicon.ico" />
<mvc:resources mapping="/maintenance" location="resources/html/maintenance.html" />
So far it only works for the first two location (resource mappings). I can only access the static html by supplying the full URI
http://mydomain/resources/html/maintenance.html
instead of
http://mydomain/maintenance
UPDATED
I added <http pattern="/maintenance" security="none" /> in my spring-security-context.xml and it gives me new error, HTTP Status 404 - The requested resource is not available.
What did I miss here ?
I just found out the issue is caused by this
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The easy way out for me was to change the maintenance URI to /maintenance.html, thus the request is not 'handled' by the servlet.
If there's anyone can point out a better solution which allows me to access it as /maintenance, I'll accept that as the answer.

servlet web.xml mapping

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.

struts.action.excludePattern not working,

struts.action.excludePattern is not working for me in Struts 2, i had place servlet cal in form action, form will submit on hyperLink click.
struts.xml:
<constant name="struts.action.excludePattern" value="/PunchoutOrder"/>
web.xml
<servlet>
<description></description>
<display-name>PunchoutOrder</display-name>
<servlet-name>PunchoutOrder</servlet-name>
<servlet-class>com.PunchoutOrder</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PunchoutOrder</servlet-name>
<url-pattern>/PunchoutOrder</url-pattern>
</servlet-mapping>
jsp:
<form id="form1" name = "form1" method="post" action="PunchoutOrder">
<input type="image" alt="Submit" src="images/submit.png" onclick="Submit(form1);return false;"/>
Getting below Error:
15:26:37,512 WARN [Dispatcher] Could not find action or result
There is no Action mapped for namespace / and action name PunchoutOrder. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:475)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
Am i missing something...........?
Thank you.....
I was having the same problem and ran into several hints at how to fix it. However the solution I found, at least in Struts 2.2.2 was that I was using the wrong filter in my web.xml. After some digging through Google and SO posts I found that changing from:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
To:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
After changing the filter, my exclusion pattern constant in the struts.xml immediately started functioning as expected.
This is also documented here: https://cwiki.apache.org/confluence/display/S2WIKI/Troubleshooting+guide+migrating+from+Struts+2.0.x+to+2.1.x#TroubleshootingguidemigratingfromStruts2.0.xto2.1.x-FilterMapping%2CdefaultActionextensions%2CandServlets
Although the previous entry says to explicitly declare the action extensions, I found it was unnecessary after fixing my filter declaration.
it seems as if your syntax isn't entirely correct. I fell upon the same issue and correcting the regular expression worked for me. After adding the line
<constant name="struts.action.excludePattern" value="/exclude/.*?"/>
to my struts.xml, everything works as expected. Request within this path are now returned with 404 errors instead of struts error message "There is no Action mapped for namespace ..."
cheers
In web.xml add .extention in servlet url pattern
<servlet-mapping>
<servlet-name>PunchoutOrder</servlet-name>
<url-pattern>/PunchoutOrder.srl</url-pattern>
</servlet-mapping>

Resources