remove .py from the jython based web application url - java

This is the follow up of this question how to remove .py from the url in jython , I couldnt get it solved till now, and hope to get some more suggestions for it.
Here is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<servlet>
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>NewJythonServlet</servlet-name>
<servlet-class>NewJythonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewJythonServlet</servlet-name>
<url-pattern>/NewJythonServlet</url-pattern>
</servlet-mapping>
</web-app>
any ideas please?

Try:
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>/serv/*</url-pattern>
</servlet-mapping>
I think the url had to start with something that isn't a wildcard

Related

Servlet Wrong response

I have 3 servlets in my web application.
The first servlet is workinh OK.
The second and third servlet which is copying from the first one, doesn't work and doesn't get response.
However, I search on internet and all websites say: the error in web.xml file
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1">
<servlet>
<servlet-name>getmarks</servlet-name>
<servlet-class>getmarks</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Enter</servlet-name>
<servlet-class>Enter</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>getmarks</servlet-name>
<url-pattern>/getmarks</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Enter</servlet-name>
<url-pattern>/Enter</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
Try This Code Web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Your Project Name</display-name>
<servlet>
<servlet-name>ABC</servlet-name>
<servlet-class>getmarks</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>DEF</servlet-name>
<servlet-class>Login</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>GHI</servlet-name>
<servlet-class>Enter</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ABC</servlet-name>
<url-pattern>/getmarks_1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DEF</servlet-name>
<url-pattern>/Login_1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GHI</servlet-name>
<url-pattern>/Enter_1</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
You should put full class names in the <servlet-class> tag.
Also, <web-app> tag is missing some attributes about XML Schema. You can find more information here.
For version 3.1 it should look like this:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
// your mappings
</web-app>
Closing </web-app> tag is also missing.

servlets: display image from outside the project dir

I am developing JAVA web project by using netbeans IDE, I want to display some images from a folder outside the project's directory, after uploading them by my servlets, here is my code:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>views/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>controllers.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>index</servlet-name>
<jsp-file>/views/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>students</servlet-name>
<servlet-class>controllers.StudentController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>students</servlet-name>
<url-pattern>/students</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>books</servlet-name>
<servlet-class>controllers.BookController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>books</servlet-name>
<url-pattern>/books</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>admins</servlet-name>
<servlet-class>controllers.AdminController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>admins</servlet-name>
<url-pattern>/admins</url-pattern>
</servlet-mapping>
<context-param>
<param-name>uploads</param-name>
<param-value>C:/Users/mohammad/Documents/NetBeansProjects/uploads</param-value>
</context-param>
</web-app>
img in the jsp:
<img src="C:/Users/mohammad/Documents/NetBeansProjects/uploads/name.jpg"/>
servlet:
I am sending it inside a json object:
object.add("<img src=\""+getServletContext().getInitParameter("uploads")+"/"+book.getImage()+"\"/>");
I am sure that the image is exist in that folder, but it couldn't loaded, what is the problem here?
You should add filter on web.XML file of image extensions that actually specify that what kind of external resources can be passed through front controller.. Will u please post your web.xml
U may try this link Upload an image to a Path set in web.xml using primefaces

404 Not Found error was encountered while trying to use an ErrorDocument to handle the request

I am developing a website and i am finding hosting issues. I have put all my .jsp files in the root directory.
I am able to see the JSPs (accessing directly to them).
But, when i try to perform the same check the WEB-INF/classes, I get the error:
Not Found The requested URL /AC/SearchController was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
This is my Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>AC</display-name>
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AdvisorProfileServlet</servlet-name>
<servlet-class>org.AC.controller.AdvisorProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorProfileServlet</servlet-name>
<url-pattern>/AC/AdvisorProfilePage?aId=*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<servlet-class>org.AC.controller.ForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<url-pattern>/ForgotPasswordAdvisor</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<servlet-class>org.AC.controller.AdvisorMyAccountRequestController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<url-pattern>/AC/requests.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<servlet-class>org.AC.controller.UserForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<url-pattern>/ForgotPasswordUser</url-pattern>
</servlet-mapping>
</web-app>
This might also be a server config, because I created the project on Tomcat server 8, but I guess the server which is now hosting it is Tomcat 7.
How can I resolve this?
Please declare "/AC/SearchController" as url pattern in your web.xml

Couldn't access index.html in my WebContent folder

I have tried to find a solution searching on Google, but did not succeed.
This is the structure of my application's folder.
<PROJECT_NAME>
--->WebContent
--->index.html
--->WEB-INF
--->META-INF
I deployed my application in Tomcat server.
My web.xml config file is the following:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>fp</display-name>
<servlet>
<servlet-name>FpServlet</servlet-name>
<servlet-class>com.fp.FpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FpServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
I try to access localhost:8080/project_name/,and everything works fine, but when I try to access the index.html file like this localhost:8080/project_name/index.html, it doesn't show the index.html content. It shows my root project content. (localhost:8080/project_name/)
How can I get the index file content posted in the browser?
Any help will be very appreciated.
Please note that the folder is named "WEB-INF", not "WEB.INF".
You can use default servlet to serve your static resources. Here is how this can be used with tomcat:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

Ambiguity in servlet-mapping

I have two servlets 'ExtensionServlet' and 'PatternServlet' and a static html page. The HTML code is given below.
<html>
<head>
<title>
Resolve servlet ambiguity
</title>
</head>
<body>
<form action="servlets/form.col" method="POST">
<input type="submit" value="Goto Servlet">
</form>
</body>
</html>
The Deployment descriptor is as follows.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<servlet>
<servlet-name>Extension Servlet</servlet-name>
<servlet-class>ExtensionServlet</servlet-class>
<servlet-name>Pattern Servlet</servlet-name>
<servlet-class>PatternServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Extension Servlet</servlet-name>
<url-pattern>*.col</url-pattern>
<servlet-name>Pattern Servlet</servlet-name>
<url-pattern>/servlets/*</url-pattern>
</servlet-mapping>
</web-app>
When I click the button in the HTML page it leads to "PatternServlet". If I rearrange the servlet order in the deployment descriptor as below, it goes to "ExtensionServlet"
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<servlet>
<servlet-name>Pattern Servlet</servlet-name>
<servlet-class>PatternServlet</servlet-class>
<servlet-name>Extension Servlet</servlet-name>
<servlet-class>ExtensionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Pattern Servlet</servlet-name>
<url-pattern>/servlets/*</url-pattern>
<servlet-name>Extension Servlet</servlet-name>
<url-pattern>*.col</url-pattern>
</servlet-mapping>
</web-app>
Can anyone please explain this behavior?
Tomcat 6.0.20
JVM 1.6.0_15-b03
Is your schema a shortening of the following?
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<servlet>
<servlet-name>Pattern Servlet</servlet-name>
<servlet-class>PatternServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Extension Servlet</servlet-name>
<servlet-class>ExtensionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Pattern Servlet</servlet-name>
<url-pattern>/servlets/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Extension Servlet</servlet-name>
<url-pattern>*.col</url-pattern>
</servlet-mapping>
</web-app>
In your version I think you are defining only one servlet.
When tomcat encounters several definitions that match a request (like in your case), it uses the first one.
When Tomcat receives a request, it selects one single servlet for execution using the configured servlet mappings. If the requested URL matches more than one mapping, the best match is selected, ignoring the others.
By specification, path-prefix pattern mappings are preferred over extenstion mappings. This means that, in your example, the mapping on "/servlets/" should win over the one on ".col", and the request should always cause the execution of Pattern servlet. The unexpected behaviour you have when you rearrange the declarations is because your descriptor is not correct, as already pointed out by other answers. Try using a correct descriptor, like the one suggested by David Rabinowitz.
That doesn't look like a legal deployment descriptor to me. Doesn't the schema require something like this:
<servlet>
<servlet-name>Extension Servlet</servlet-name>
<servlet-class>ExtensionServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Pattern Servlet</servlet-name>
<servlet-class>PatternServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Extension Servlet</servlet-name>
<url-pattern>*.col</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Pattern Servlet</servlet-name>
<url-pattern>/servlets/*</url-pattern>
</servlet-mapping>

Categories